Upcoming database upgrades. Airtable functionality will be reduced for ~15 minutes at 06:00 UTC on Feb. 4 / 10:00 pm PT on Feb. 3. Learn more here

Check if a record exists and if not, make one

Topic Labels: API
2331 4
Showing results for 
Search instead for 
Did you mean: 

I am trying to use AirTable as part of a node.js app to lookup users. Here is how it should work:

  • The table has multiple columns, including one for ID.
  • The API looks up the user ID by listing all and filtering by ID.
  • If there is no record found for the user ID, it will create a new record for it.

I tried to attempt this using the code below, and it works fine when a user is found, but when it doesn’t, it outputs a 422 error with the status text “Unprocessable Entity”. I would really appreciate if someone could show me how to do this, or show me what I did wrong in my code. (The code is in Node.js and I used request and axios because I was copying existing code that was in Node.js request and I only know how to use Axios)

        headers: {'Authorization': 'Bearer <TOKEN>'},
        url: `<APPID>/Users?maxRecords=1&filterByFormula=${encodeURI(`{ID}=${<ID OF USER>}`)}`,
        json: true
      },function (error, response, body) {
        if(!body.records[0]) {
          console.log("NOT FOUND")
            method: 'post',
            url: `<APPID>/Users`,
            headers: {'Authorization': 'Bearer <TOKEN>'},
            data: {
              "fields": {
                "<field name redacted>": <number>,
                "<field name redacted>": <text>,
                "<field name redacted>": <number>,
                "<field name redacted>": <number>,
                "<field name redacted>": <number>,
                "<field name redacted>": <number>,
                "<field name redacted>": <number>
          .then(function (response) {
          .catch(function (error) {

        } else {
4 Replies 4

422 errors generally mean either the request was formatted incorrectly or the datatypes you are trying to insert into the base don’t map to your schema.

The only thing I can see that potentially indicates the API might be rejecting the request format is you are missing the "Content-Type": "application/json" header. I’m not sure if Axios adds that header in for you, but doesn’t hurt to be explicit.

If that change doesn’t fix the issue, then I would recommend confirming that all of the fields you are trying to pass numbers to are Number field types in Airtable.

Does your code log “NOT FOUND” to the console when there is no record? If not, the issue may be with your if condition.

In this line you are trying to read the first item in the records array. If a record is found, this condition would be false and code execution continues as you expect. However, if a record is not found (or there is some other problem), testing this conditions involves trying to access a non-existent first item in the array, which would cause an error.

When testing to see if a record exists, I prefer to test for the positive case. I first test to make sure that there is a records array, then I test to make sure that there is at least one record in that array.

if(body.records && body.records.length >= 1) {
} else {
  // insert code to create the record

Thank you for your reply.
The code logs “NOT FOUND” whenever there isn’t a record, so I don’t think that is the problem. I should have clarified that. My bad.

Thanks again for your reply.

I tried adding the “Content-Type” header, and it didn’t change anything. I also double-checked the content types/whether it is a number or not.

If you were to do this, would you have done it in the same method I had? When I tried to use the Airtable.js package, there was an issue as well. Something about the offset.