Help! My Airtable script can’t send POST requests to my API
I’m trying to send data from Airtable to my custom API using a script. It works fine in Postman, but when I run it in Airtable, I get a 405 error. The API sees it as an OPTIONS request instead of POST.
Here’s what I’m seeing:
- Airtable error: TypeError: Failed to fetch (mentions CORS)
- API console: OPTIONS /api/webhook 405 Method Not Allowed
My Airtable script looks like this:
let records = await view.selectRecordsAsync();
let data = {
user: {
firstName: records.records[0].getCellValueAsString('firstName'),
lastName: records.records[0].getCellValueAsString('lastName'),
email: records.records[0].getCellValueAsString('email')
}
};
let response = await fetch('https://my-api.example.com/webhook', {
method: 'POST',
body: JSON.stringify(data),
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
});
And my Node.js API handler:
export default async function handler(req, res) {
const { method } = req;
if (method === 'POST') {
return getData(req, res);
}
if (method === 'OPTIONS') {
res.setHeader('Allow', ['POST', 'OPTIONS']);
res.status(405).end(`Method ${method} Not Allowed`);
}
}
Any ideas why this isn’t working? Thanks!
I’ve encountered similar issues when working with Airtable scripts and external APIs. One thing that often gets overlooked is the timeout settings. Airtable scripts have a default timeout of 30 seconds, which might not be enough for some API calls.
Try increasing the timeout in your script:
let response = await fetch('https://my-api.example.com/webhook', {
method: 'POST',
body: JSON.stringify(data),
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
timeout: 60000 // Set timeout to 60 seconds
});
Also, double-check your API’s firewall settings. Sometimes, requests from Airtable’s IP range get blocked. You might need to whitelist Airtable’s IPs in your API’s firewall configuration.
If these don’t work, consider using Airtable’s built-in webhook feature instead of a custom script. It’s often more reliable for sending data to external APIs.
This definitely looks like a CORS (Cross-Origin Resource Sharing) issue. Airtable’s scripting environment has certain restrictions when making external requests. To resolve this, you’ll need to modify your API to properly handle CORS preflight requests.
In your API handler, add a specific case for OPTIONS requests:
if (method === 'OPTIONS') {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'POST, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
res.status(200).end();
return;
}
This should allow the Airtable script to successfully make the POST request. Remember to also add the ‘Access-Control-Allow-Origin’ header to your POST response. If security is a concern, replace ‘*’ with Airtable’s specific domain.
sounds like a CORS issue. yr API needs to allow cross-origin requests from airtable. try adding these headers to yr API response:
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: POST, OPTIONS
Access-Control-Allow-Headers: Content-Type
that might fix it. lmk if u need more help!