Help

PATCH multiple records REST API (status code: 422)

Solved
Jump to Solution
1417 2
cancel
Showing results for 
Search instead for 
Did you mean: 
guanxinwang
4 - Data Explorer
4 - Data Explorer

Hi, I am trying to patch multiple records to airtable using Nest.js. I've checked:

- url should be correct: 

https://api.airtable.com/v0/${baseId}/${tableId}

- headers with authorized token:

    const headers = {
      Authorization: `Bearer ${token}`,
      'Content-Type': 'application/json',
      accept: 'application/json',
    };

- body is stringified with records array limited to 10:

    const body = JSON.stringify({
      records: records,
      typecast: true,
    });

my records look like:

records:[
{
id: 'recyhk6cWWQcVrG9s',
createdTime: '2023-01-09T07:07:16.000Z',
fields: {
'Code Specs': 'window.datalayer.push({\n' +
' "event": "select_content",\n' +
' "shipping_method": "${shipping_method}",\n' +
' "payment_method": "${payment_method}"\n' +
'})',
'Code Spec Match': 'false',
Platform: [Array],
'GTM trigger method': 'Datalayer',
'Event Name': [Array],
'Event Parameters': [Array],
Ecommerce: 'false',
KPI: 'select_content',
'Last Update Time': '2023-02-11T01:09:21.000Z',
'Variables Name (from Event Parameters)': [Array],
'Last modified by': [Object],
Created: '2023-01-09T07:07:16.000Z',
'Event (from Event Name)': [Array],
'Event Definition': [Array]
}
},
// in total 8 records
]

My implementation code:

  /**
   * patchAirtable updates one or multiple records in a table.
   *
   * @param promises An array of objects containing the id, field and value of the record(s) to update.
   * @param baseId The id of the base.
   * @param tableId The id of the table.
   * @param viewId The id of the view.
   * @param token The API key for the Airtable API.
   *
   * @returns void
   */
  patchAirtable(
    records: Array<any>,
    baseId: string,
    tableId: string,
    viewId: string,
    token: string,
  ) {
    console.log('patching airtable...');
    const url = `https://api.airtable.com/v0/${baseId}/${tableId}`;
    const headers = {
      Authorization: `Bearer ${token}`,
      'Content-Type': 'application/json',
      accept: 'application/json',
    };
    const body = JSON.stringify({
      records: records,
      typecast: true,
    });
    console.log(records);
    this.http.patch(url, body, { headers }).subscribe(res => console.log(res));
  }

I've asked GPT chatbot, but no lucks. Is there anything I missed. Any help is appreciated. One idea is that there are cells might be empty that isn't showing up in the records' fields.

1 Solution

Accepted Solutions
kuovonne
18 - Pluto
18 - Pluto

Are you including any calculated fields in your patch request? You should only include editable fields for which you have the necessary edit permissions. Try taking out any calculated fields and see if that helps. If you still get the error, try removing all the fields and adding them in one at a time until you find the culprit.

See Solution in Thread

2 Replies 2
kuovonne
18 - Pluto
18 - Pluto

Are you including any calculated fields in your patch request? You should only include editable fields for which you have the necessary edit permissions. Try taking out any calculated fields and see if that helps. If you still get the error, try removing all the fields and adding them in one at a time until you find the culprit.

Thanks, @kuovonne; I never thought if fields were editable or not. Now it works like a charm.