Tim Dietrich

Custom Software Developer

Home Services Portfolio Blog About Contact Newsletter

Airtable API: Working With File Attachments

I am working on another Airtable API project, and ran into a problem when trying to use the API to add file attachments.

Here's what my original code looked like...

// Create the attachments array.
$attachments_array = array ( 'url' => 'http://timdietrich.me/application/images/twitter-blue.png' );

// Create the fields array.
$fields_array = array (
	'Name' => 'Test',
	'Attachments' => $attachments_array
);	

The resulting JSON-encoded data that I was attempting to POST looked like this:

{"Name":"Test","Attachments":{"url":"http:\/\/timdietrich.me\/application\/images\/twitter-blue.png"}}

The Airtable API returned an "INVALID_ATTACHMENT_OBJECT" error and stated: Invalid attachment object for column Attachments: "http://timdietrich.me/application/images/twitter-blue.png"

After some troubleshooting, I realized that the attachments array actually needs to be formated as an array of nested arrays. Here's what the updated PHP code looks like:

// Create the attachments array.
// Note that this is an array of arrays.
$attachments_array = 
	array ( 			
		array ( 'url' => 'http://timdietrich.me/application/images/twitter-blue.png' )
	);

// Create the fields array.
$fields_array = array (
	'Name' => 'Test',
	'Attachments' => $attachments_array
);	

The JSON-encoded data now looks like this...

{"Name":"Test","Attachments":[{"url":"http:\/\/timdietrich.me\/application\/images\/twitter-blue.png"}]}

And it works like a charm!