Help

How to I insert records into link fields with API?

Topic Labels: API
15874 9
cancel
Showing results for 
Search instead for 
Did you mean: 
bluegreen
4 - Data Explorer
4 - Data Explorer

I’m trying to insert a new record into a table that has a link field (single select). The value I’m trying to insert matches exactly with the value in the linked table but I’m getting a 422 error.

To do this do I need to insert the record ID in the linked table? I don’t have this ID available in my external application.

9 Replies 9
Giovanni_Briggs
6 - Interface Innovator
6 - Interface Innovator

If you are trying to modify a linked record field (or create a new record with a linked record field), you do need to use the foreign record’s ID. If you don’t have those IDs readily available, you can look them up via a GET request and use the filterByFormula query parameter to find the foreign record ID. For example, if you’re foreign record’s primary key is called “Name” with a value “Record1”, you could find the record by doing:
curl -X GET 'https://api.airtable.com/v0/YOUR_BASE_ID/YOUR_FOREIGN_TABLE_NAME?filterByFormula={Name}=%22Record1%22'

This will return a list of records that match that formula. If your primary keys are unique, there will only be one record in the list and you can retrieve it’s record ID.

You could also try and use the typecast parameter when creating the new record. Using typecast will ask the Airtable API to try and infer the intention of the request. You can use the foreign record’s primary key instead of it’s record ID, and the API will attempt to find the corresponding linked record and make the connection:

curl -X POST \
  https://api.airtable.com/v0/YOUR_BASE_ID/YOUR_TABLE_NAME \
  -H 'Content-Type: application/json' \
  -d '{
	"fields": {
		"Linked Field": ["Record1"]
	},
	"typecast": true 
}'

Thanks! I got this working using the typecast parameter.

Richard_Lo1
4 - Data Explorer
4 - Data Explorer

I am trying to create a new record with a linked record field.

I followed your instructions and am setting the linked field to the foreign record ID (recXORCOwTG4hhDBF), but I am still getting a 422 error.

I am able to create the new record without the linked field, so it seems like there is something wrong with the linked field.

Hi @Richard_Lo1

Are you passing that record ID in inside of an array, or just as a string value for the Linked field?

I’m fairly certain that you have to pass an array of string ID values to a Linked field, even if you are only trying to insert 1.

So instead of:

"fields": {
	"Linked Field": "recXORCOwTG4hhDBF"
}

You need:

"fields": {
	"Linked Field": ["recXORCOwTG4hhDBF"]
}

Hey Jeremy_Oglesby,

You’re right! Passing the record ID inside an array worked!

I was trying to pass the string value and was getting an error. Thanks for the quick help!

Patrick_Ford
6 - Interface Innovator
6 - Interface Innovator

Encountered this as well and for those that it might help, the typecast parameter only works for string values. So the primary field you are linking to must be a text field. I tried this with a number field as the primary field and it kept creating new records even though one already existed.

How can I use this in Laravel? Can I use something like this?

$ids = [
“Company_tags” => [$id]
];

                    $response = Http::withHeaders([

                        'Content-Type' => 'application/json',

                        'Authorization' => env('AIRTABLE_API_KEY')

                    ])->post('https://api.airtable.com/v0/'.env('AIRTABLE_BASE_ID').'/'.env('AIRTABLE_CAND_TABLE'), [

                        "typecast" => true,

                        "fields" => [

                            "Company_tags" => [$id],

                        ],

                    ]);

How can I it in Laravel? Can I use it something like this?

                    $response = Http::withHeaders([

                        'Content-Type' => 'application/json',

                        'Authorization' => env('AIRTABLE_API_KEY')

                    ])->post('https://api.airtable.com/v0/'.env('AIRTABLE_BASE_ID').'/'.env('AIRTABLE_CAND_TABLE'), [

                        "typecast" => true,

                        "fields" => [ "Company_tags" => [$id] ]

                    ]);
kennypurnomo
7 - App Architect
7 - App Architect

“Linked Field”: [“Record1”]

If i want to have 3 records inside “Linked Field”, what should i type?