Airtable is a versatile cloud-based platform that seamlessly blends the structure of a spreadsheet with the robust capabilities of a database.By integrating Airtable’s automation scripts - written in JavaScript - with Ayrshare’s API, you can streamline your social media management directly within Airtable. This integration empowers you to post content, analyze performance metrics, and manage comments on behalf of your users, all from one centralized location.In this guide, we’ll walk you through the steps to set up the integration.
Running Airtable Automation Scripts requires a paid Airtable plan.
Please be sure your Airtable plan includes automations with scripts.
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 to your managed client’s social media accounts. All the below code can be found at GitHub.
Start by getting your free or paid plan API Key in Ayrshare Dashboard. 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.
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.
Select Run Script. You will be brought into the script editor.
Delete the line:
console.log(`Hello, ${base.name}!`);
And copy and paste into the script editor the following code:
const API_KEY = "Your API Key"; // Get a free key at app.ayrshare.comconsole.log(`Starting Post ${base.name}!`);const sendPost = async (data) => { const { post, platforms, imageUrls, profileKeys, scheduleDate, shortenLinks } = data; const body = Object.assign( {}, post && { post }, platforms && { platforms }, profileKeys && { profileKeys: profileKeys.split(",") }, Array.isArray(imageUrls) && imageUrls.length > 0 && { mediaUrls: imageUrls.map((image) => image.url) }, scheduleDate && { scheduleDate }, shortenLinks !== undefined && shortenLinks !== null && { shortenLinks } ); console.log("Posting JSON:", JSON.stringify(body, null, 2)); if (profileKeys) { body.profileKeys = profileKeys.split(","); } const response = await fetch("https://api.ayrshare.com/api/post", { method: "POST", body: JSON.stringify(body), headers: { "Content-Type": "application/json", Authorization: `Bearer ${API_KEY}` } }).then((res) => res.json()); return response;};const table = base.getTable("Posts");const query = await table.selectRecordsAsync();const filteredRecords = query.records.filter((record) => { const status = record.getCellValue("Status"); return status === "pending";});for (let record of filteredRecords) { const post = record.getCellValue("Post"); const images = record.getCellValue("Images"); const platforms = record.getCellValue("Platforms"); const profileKeys = record.getCellValue("Profile Keys"); const scheduleDate = record.getCellValue("Schedule Date"); const shortenLinks = false; const response = await sendPost({ post, platforms: platforms.map((x) => x.name), imageUrls: images, profileKeys, scheduleDate, shortenLinks }); console.log(response); if (response) { let status; if (Array.isArray(response)) { status = response.map((x) => x.status).every((x) => x === "success") ? "success" : "error"; } else { status = response.status; } await table.updateRecordAsync(record, { Status: status }); }}
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.
In the script editor, press >TestThe 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.
Here is a great video tutorial from the team at Automate All The Things. This video walks through how to integrate Ayrshare into a live Airtable project.