Getting 422 error when sending POST request to Airtable using Google Apps Script

I’m trying to add records to my Airtable base using Google Apps Script but I keep getting a 422 response code. The same request works perfectly when I test it in Postman.

Here’s my current code:

var payload = {
    "records": [
        {
        "fields": {
            "Company Name": "example",
            "0x9af2bcd5e73da8b71a3fb9083ca6f4d2b8e7c1a6": "5678"
        }
        }
    ]
};

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

function sendToAirtable(){
  var apiUrl = "https://api.airtable.com/v0/yyyyyyyyyyyy/Transaction%20Log?api_key=keyyyyyyyyyyyyy";
  var result = UrlFetchApp.fetch(apiUrl, requestOptions);
  console.log(result.getResponseCode());
};

The function returns 422 and nothing gets added to my Airtable. I checked the Airtable documentation and their example looks like this:

var Airtable = require('airtable');
var base = new Airtable({apiKey: 'YOUR_API_KEY'}).base('yyyyyyyyyyyy');

base('Transaction Log').create([
  {
    "fields": {
      "Company Name": "Mon, 15 Jan 2023 10:30:25 GMT",
      "0xa1b2c3d4e5f6789012345678901234567890abcd": 98765432101234

What could be causing this 422 error in my Google Apps Script implementation?

Your headers are messed up. You need to structure the headers object correctly inside requestOptions. Also, stop putting the API key in the URL - that’s deprecated. Use Bearer auth in the headers instead:

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

Remove the api_key parameter from your URL too. Had this exact issue last month migrating old scripts. The 422 error means invalid request format, which happens when Google Apps Script can’t read your headers properly.

your headers are messed up. put them inside the requestOptions object like this: "headers": {"Content-Type": "application/json", "Authorization": "Bearer YOUR_API_KEY"}. don’t mix them with other options. and move that api key from the url to the authorization header.

Your header structure is wrong. In Google Apps Script, Content-Type goes inside a headers object, not at the root level of requestOptions. That field name “0x9af2bcd5e73da8b71a3fb9083ca6f4d2b8e7c1a6” looks sketchy too - Airtable doesn’t like ethereum addresses or hex strings as field names. Double-check your actual Airtable base to confirm that’s the exact field name. I’ve been burned by this before when my field name didn’t match perfectly - spaces and special characters matter. A 422 error means your JSON is fine but doesn’t meet Airtable’s requirements.