Write to an Airtable with PHP

  • You are here: Free PHP » Uncategorized » Write to an Airtable with PHP


Lately I've been rediscovering the joy of PHP. Also been helping an MVP get off the ground which uses a lot of nocode/lowcode bits and pieces and miraculously puts them together.

Anyway, I had to write to an Airtable table with PHP and some quick googling didn't find an example to copy-paste so I'm writing this one for posterity.

So I have a table called people which has Name and Email fields. I want to write to it programmatically. Airtable has this pretty cool documentation which uses existing data from your real tables as examples. Search "airtable api" and you'll find your way.

There you'll see things like this:

Here people is the table name and blarghblahbla is the the AirTable app ID (obfuscated). This is generated for you so no need to worry about it, just copy. The question is to turn it into PHP code.

Oh, you'll also need your Airtable key which you get from your account:

Armed with these prerequisites, here's the PHP code that uses cURL to write to the table:

// get this from your account
$airtable_key = 'keyeKanyeOladiOblada';

// URL generated by the docs
$url = 'https://api.airtable.com/v0/blarghblahbla/people';
$headers = [
  'Content-Type: application/json',
  'Authorization: Bearer ' . $airtable_key,    
];

// this is the JSON-encoded record to write to the table
$post = '{"fields": {"Name": "Stoyan", "Email": "ssttoo@ymail.com"}}';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
curl_close($ch);
$json = json_decode($result, true);

Et voilà!

Powered by Gewgley