Making POST requests to Airtable API through Google Apps Script

I’m trying to send data to Airtable using Google Apps Script but keep getting a 422 error. The same request works fine when I test it in Postman, so I think there’s something wrong with how I’m setting up the request in my script.

Here’s what I have so far:

var payload = {
    "records": [
        {
            "fields": {
                "Project Name": "demo",
                "0x9af2c8d12b95e78dc456ff23bc987654321abcde": "5678"
            }
        }
    ]
};

var requestOptions = {
    "method": "post",
    "Content-Type": "application/json",
    "muteHttpExceptions": true,
    "payload": JSON.stringify(payload)
};

function sendDataToAirtable() {
    var apiUrl = "https://api.airtable.com/v0/xxxxxxxxxxxxx/Data%20Storage?api_key=keyxxxxxxxxxx";
    var result = UrlFetchApp.fetch(apiUrl, requestOptions);
    console.log(result.getResponseCode());
}

The console shows 422 as the response code and nothing gets added to my Airtable base. When I use the exact same payload in Postman it works perfectly. I’m pretty sure the API key and base ID are correct since other GET requests work fine.

Any ideas what could be causing this issue with the POST request?

Your headers are set up wrong in requestOptions. You’ve got “Content-Type” as a direct property, but it needs to be inside a “headers” object. I hit this exact same issue with Airtable’s API in GAS a few months back.

Restructure your requestOptions like this:

var requestOptions = {
    "method": "post",
    "headers": {
        "Content-Type": "application/json"
    },
    "muteHttpExceptions": true,
    "payload": JSON.stringify(payload)
};

That 422 error usually means malformed JSON or missing headers. Since your payload works in Postman, it’s definitely headers. GAS is way stricter about header formatting than other HTTP clients. Fixed it for me right away.