Help

Save the date! Join us on October 16 for our Product Ops launch event. Register here.

Re: Linked records in API call

Solved
Jump to Solution
118 0
cancel
Showing results for 
Search instead for 
Did you mean: 
k6no
4 - Data Explorer
4 - Data Explorer

Hello,
I'm trying to run a script from BaseA to create a new record in BaseB. The new record in BaseB has a linked field, I followed this convention to reference it: Field_Name: [{id: record_id}]. I get INVALID_RECORD_ID error, though I've double-checked it and the record id is correct. What am I doing wrong?

 

let createRecordInBaseB = await fetch(
  `https://api.airtable.com/v0/${targetBaseId}/${targetTableId}`,
  {
    method: "POST",
    headers: {
      Authorization: `Bearer ${baseARecord.pat}`,
      "Content-Type": "application/json",
    },
    body: JSON.stringify({
      fields: {
        "👀 Projects": [{id: projectRecordId}],
      },
    }),
  }
);

 

  

1 Solution

Accepted Solutions
djseeds
5 - Automation Enthusiast
5 - Automation Enthusiast

To expand on kuovonne's response, here is the updated code that should allow you to create a new record with a linked field:

let createRecordInBaseB = await fetch(
  `https://api.airtable.com/v0/${targetBaseId}/${targetTableId}`,
  {
    method: "POST",
    headers: {
      Authorization: `Bearer ${baseARecord.pat}`,
      "Content-Type": "application/json",
    },
    body: JSON.stringify({
      fields: {
        '👀 Projects': [projectRecordId], // Array of record IDs, not objects
      },
    }),
  }
);

Another option for managing cross-table relationships is to use a GraphQL API like BaseQL alongside Airtable's native API. It can be especially useful when querying records across multiple tables or handling linked records, which can be tedious with the REST API, and you can also add or update records using GraphQL mutations.

Hope this helps!

See Solution in Thread

3 Replies 3

When using the web API, the cell value for a linked record field is an array of record IDs, not an array of objects.

https://airtable.com/developers/web/api/field-model#foreignkey

kuovonne_0-1728659769466.png

 

djseeds
5 - Automation Enthusiast
5 - Automation Enthusiast

To expand on kuovonne's response, here is the updated code that should allow you to create a new record with a linked field:

let createRecordInBaseB = await fetch(
  `https://api.airtable.com/v0/${targetBaseId}/${targetTableId}`,
  {
    method: "POST",
    headers: {
      Authorization: `Bearer ${baseARecord.pat}`,
      "Content-Type": "application/json",
    },
    body: JSON.stringify({
      fields: {
        '👀 Projects': [projectRecordId], // Array of record IDs, not objects
      },
    }),
  }
);

Another option for managing cross-table relationships is to use a GraphQL API like BaseQL alongside Airtable's native API. It can be especially useful when querying records across multiple tables or handling linked records, which can be tedious with the REST API, and you can also add or update records using GraphQL mutations.

Hope this helps!

k6no
4 - Data Explorer
4 - Data Explorer

Thanks for your answers, it works now