I’m trying to send data from an Airtable automation to my Node.js API endpoint but I’m getting a weird issue. When the script runs, my API receives an OPTIONS request instead of the POST request I’m sending. This causes a 405 error.
The strange part is that my API works perfectly when I test it using Postman with the same data.
Airtable gives me this error:
TypeError: Failed to fetch
at main on line 27
This error might be related to Cross-Origin Resource Sharing (CORS)
My server logs show:
OPTIONS /api/data-receiver 405 Method Not Allowed
Here’s my Airtable automation code:
let contacts = base.getTable('Contacts');
let gridView = contacts.getView('Main View');
let allRecords = await gridView.selectRecordsAsync()
let currentRecord = allRecords.records[0]
var payload = {
customer: {
"name": currentRecord.getCellValueAsString('fullName'),
"surname": currentRecord.getCellValueAsString('familyName'),
"emailAddress": currentRecord.getCellValueAsString('contactEmail'),
}
}
console.log(JSON.stringify(payload))
let apiResponse = await fetch('https://myapi.example.com/api/data-receiver', {
method: 'POST',
body: JSON.stringify(payload),
headers: {
"Content-Type": 'application/json',
"Accept": "application/json",
}
})
let jsonResponse = await apiResponse.json()
console.log("API Response:", jsonResponse)
await contacts.updateRecordAsync(currentRecord.id, {
apiResult: jsonResponse,
});
My Node.js endpoint (api/data-receiver.js):
export default async function handler(request, response) {
const { method } = request;
console.log("HTTP Method:", method)
switch (method) {
case "POST":
console.log("Request body", request.body)
return processData(request, response);
case "OPTIONS":
response.setHeader("Allow", "POST");
response.setHeader("Allow", ["GET", "POST", "PUT", "DELETE", "OPTIONS"]);
response.status(405).end(`Method ${method} Not Allowed`);
}
}
const processData = async (request, response) => {
console.log("Processing request: ", request.body)
response.status(200).json("Success")
}
I added the OPTIONS case recently to debug this issue. Any ideas why Airtable is sending OPTIONS instead of POST?