Airtable
Posting to Social Media such as Instagram, Twitter, Facebook Pages and Groups, LinkedIn, Google My Business, Reddit, or Telegram from Airtable.

Set Up

This guide shows you how to automatically post to linked social media accounts in Airtable via Ayrshare. You can post to a single company's social accounts or or to your managed client's social media accounts. All the below code can be found at GitHub.
Running Airtable Automation Scripts requires a paid Airtable plan. Please be sure your Airtable plan includes automations with scripts.

Gather Your API Key

Start by getting your free or paid plan API Key at https://app.ayrshare.com in the API Dashboard section. The key will be used in the script below.
If you are on the Ayrshare business plan and want to post on behalf of your clients, gather all your client's Profile Keys either via the /user or /create-profile endpoints or in the Ayrshare Dashboard.
Be sure you have linked a few social accounts in Ayrshare. Please verify at: https://app.ayrshare.com/social-accounts​

Create an Airtable Workspace

In Airtable, create a new workspace with the fields. Please be sure to name cell columns as below:
  • Post as Long Text
  • Platforms as Multi Select with types: facebook, instagram, twitter, linkedin, reddit, and telegram
  • Images as Attachment
  • Profile Keys as Single Line Text
  • Status as Single Line Text.
  • Schedule Date as Date with Local Format, Include Time Field, and Time Format 24 Hours
Airtable Table Example
These fields will be used in the Airtable automation script we are about to build.

Enter in Test Post Data

We need some sample data to test the post. Here is a suggestion:
  • Post: Enter - Happy New Year 2021
  • Platforms: select one or more networks you have linked. Please be sure the name is lowercase.
  • Images : Attach an image. We like this one you can download and attach: https://images.ayrshare.com/imgs/GhostBusters.jpg​
  • Profile Keys : If you are on the Business Plan and want to post to a client's profile, enter their Profile Key. Otherwise, leave blank.
  • Status: Enter - pending. The script only grabs records that are set to pending. Please be sure "pending is lowercase.
  • Schedule Date : Leave blank since we'll just test immediate posting right now. Later you can select a future date to schedule the post.

Build an Automation Script with the Script Editor

We'll now build the Airtable automation script that reads your data from the table, creates a post, and send it to the social networks via Ayrshare. You will be using the Airtable script editor.

Add Trigger

  • In the workspace, click on Automation and then +New automation.
  • Name the automation.
  • Click Choose a Trigger.
  • Select When a Record is Created.
  • Select the table with the above fields.
  • Click Done.

Add Action

  • Start by clicking Add Action.
  • Select Run Script. You will be brought into the script editor.
Delete the line:
1
console.log(`Hello, ${base.name}!`);
Copied!
And copy and paste into the script editor the following code:
1
const API_KEY = 'Your API Key'; // Get a free key at app.ayrshare.com
2
​
3
console.log(`Starting Post ${base.name}!`);
4
​
5
const sendPost = async (data) => {
6
const { post, platforms, imageUrls, profileKeys, scheduleDate, shortenLinks } = data;
7
​
8
const body = Object.assign({},
9
post && { post },
10
platforms && { platforms },
11
profileKeys && { profileKeys: profileKeys.split(",") },
12
Array.isArray(imageUrls) && imageUrls.length > 0 && { mediaUrls: imageUrls.map(image => image.url) },
13
scheduleDate && { scheduleDate },
14
shortenLinks !== undefined && shortenLinks !== null && { shortenLinks }
15
);
16
​
17
console.log("Posting JSON:", JSON.stringify(body, null, 2));
18
​
19
if (profileKeys) {
20
body.profileKeys = profileKeys.split(",");
21
}
22
​
23
const response = await fetch("https://app.ayrshare.com/api/post", {
24
method: 'POST',
25
body: JSON.stringify(body),
26
headers: {
27
'Content-Type': 'application/json',
28
'Authorization': `Bearer ${API_KEY}`
29
}
30
}).then(res => res.json());
31
​
32
return response;
33
};
34
​
35
const table = base.getTable("Posts");
36
const query = await table.selectRecordsAsync();
37
const filteredRecords = query.records.filter(record => {
38
const status = record.getCellValue('Status');
39
return status === "pending";
40
});
41
​
42
for (let record of filteredRecords) {
43
const post = record.getCellValue('Post');
44
const images = record.getCellValue('Images');
45
const platforms = record.getCellValue('Platforms');
46
const profileKeys = record.getCellValue('Profile Keys');
47
const scheduleDate = record.getCellValue('Schedule Date');
48
​
49
const shortenLinks = false;
50
const response = await sendPost(
51
{
52
post,
53
platforms: platforms.map(x => x.name),
54
imageUrls: images,
55
profileKeys,
56
scheduleDate,
57
shortenLinks
58
});
59
​
60
console.log(response);
61
​
62
if (response) {
63
let status;
64
if (Array.isArray(response)) {
65
status = response.map(x => x.status).every(x => x === "success") ? "success" : "error";
66
} else {
67
status = response.status;
68
}
69
​
70
await table.updateRecordAsync(record, {
71
"Status": status
72
});
73
}
74
}
Copied!
This code will read from your table and post to Ayrshare. However, you first need to add in your API Key (gathered from above).
Replace Your API Key with your real API key.

Test the Script

In the script editor, press: >Test
The script will run and output the response from the API call. If everything worked, you'll see a success message returned, the pending field in your records changed to success, and your post on the selected social network.
Once you create a new record in the table, the script will run and process pending records.

Airtable Docs

If you want more information, see the Airtable docs. They have details on how to use the Airtable script editor.

Questions

If you have any questions or comments, please reach out to us via email or chat.
Last modified 1mo ago