How to I insert records into link fields with API?

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.

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.