Update your NETLIFY function build
# inside netlify.toml / in your project root
[functions]
node_bundler = "esbuild"
Add node-fetch@2
instead of the default node-fetch
yarn add node-fetch@2
Create the folders and function file.
mkdir netlify
mkdir netlify/functions
touch netlify/functions/proxy.js
atom netlify/functions/proxy.js
Inside your proxy.js, write some standard fetching code
import fetch from 'node-fetch';
exports.handler = async (event) => {
const {email} = event.queryStringParameters;
return fetch("https://api.example.come/v1/search", {
method: "POST",
headers: {Authorization: `Token YOUERTOKEN`,"Content-Type": "application/json"},
body: JSON.stringify({ email }),
})
.then(response => response.json())
.then(data => ({ statusCode: 200, body: JSON.stringify(data) }))
.catch(error => ({ statusCode: 422, body: String(error) }))
}