Using Google Script to Send Data via Airtable API with POST Method

I’ve written a Google Script that’s intended to send data to Airtable, but I’m encountering an issue. Below is the code I am using:

var payload = {
    "records": [
          {
          "fields": {
              "Contract Address": "test",
              "0x8df3aad3a84da6b69a4da8aec3ea40d9091b2ac4": "1234"
          }
          }
    ]
};

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

function executePost(){
  var endpoint = "https://api.airtable.com/v0/xxxxxxxxxxxxx/Balance%20Tracking?api_key=keyxxxxxxxxxx";
  var result = UrlFetchApp.fetch(endpoint, requestOptions);
  console.log(result.getResponseCode());
};

Upon execution, I receive a response code of 422, which indicates an error, and the information isn’t being posted to Airtable. Interestingly, the payload is functional when used as part of a POST request in Postman. Can anyone identify what might be going wrong?

Additional information: Someone provided sample code from Airtable, which I’m including below for reference:

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

base('Balance Tracking').create([
  {
    "fields": {
      "Contract Address": "Thu, 03 Feb 2022 15:12:37 GMT",
      "0xfecf784f48125ccb7d8855cdda7c5ed6b5024cb3": 12055358359168
    }
  }
]);

I’ve also added a screenshot from Postman to clarify the correct format.

Maybe the issue is in how the data types are defined in ur payload. If numbers or strings aren’t formatted properly, Airtable can throw a 422. Double-check the field types in Airtable and make sure ur data matches those specs. That could be the missing piece of the puzzle!

From my experience, the issue might be linked to the headers setup in your script. You should explicitly set the Authorization header instead of including your API key within the URL. Correcting this often resolves status code 422 errors. Try modifying your script to include:

var requestOptions = {
  "method" : "post",
  "contentType": 'application/json',
  "headers": {
    "Authorization": "Bearer your_api_key_here"
  },
  "muteHttpExceptions": true,
  "payload" : JSON.stringify(payload)
};

Having the API key in the headers is a requirement for many services to authenticate accurately. This solved a similar problem for me when working with Airtable’s API.

I’ve encountered a similar issue before and found it essential to double-check your JSON structure. One common oversight can be the incorrect setup of the access object—make sure there’s no extra nesting or typos in the field names. Another potential pitfall could be the use of special characters, make sure all your field names and keys are URL-friendly. Additionally, verify that your URL endpoint is exactly correct and that you’re connecting to the right Airtable Base and table. One tiny syntax error can lead to unexpected errors. Finally, ensure there’s no trailing slash in your endpoint URL unless explicitly needed by Airtable; sometimes this can impact how URLs are parsed by their service.