Transfer images from Airtable to WordPress media library using JavaScript APIs

I need help creating a JavaScript script that can pull images from my Airtable database and automatically upload them to WordPress media folder. The goal is to populate empty image fields across different post types in WordPress.

For instance, if I have a user profile for “John Smith” with an empty avatar field, the script should grab the corresponding image from Airtable and fill that field automatically. I’m pretty new to programming and learning as I go, so any guidance would be awesome.

Here’s what I have so far for connecting to Airtable:

const airtableClient = new Airtable({ apiKey: 'YOUR_API_KEY' }).base('YOUR_BASE_ID');

const usersTable = airtableClient('PROFILES');

const fetchAllRecords = async() => {
    const data = await usersTable.select({
        maxRecords: 100,
        view: "Export View"
    }).firstPage();
    console.log(data);
};

fetchAllRecords();

const fetchRecordByID = async(recordId) => {
    try {
        const result = await usersTable.find(recordId);
        console.log(result);
    } catch (error) {
        console.error(error);
    }
};

fetchRecordByID('rec123456');

How do I extend this to download the images and push them to WordPress?

You’ll need to handle the WordPress side to complete this workflow. Your Airtable connection looks solid, but you’re missing the media upload piece.

I dealt with something similar last year migrating thousands of user avatars. The key is using WordPress REST API for media uploads. You’ll need to authenticate with WordPress first, then hit the media endpoint.

Here’s what you need to add after fetching from Airtable:

// Download image from Airtable attachment URL
const downloadImage = async (imageUrl) => {
    const response = await fetch(imageUrl);
    return await response.blob();
};

// Upload to WordPress media library
const uploadToWordPress = async (imageBlob, filename) => {
    const formData = new FormData();
    formData.append('file', imageBlob, filename);
    
    const response = await fetch('https://yoursite.com/wp-json/wp/v2/media', {
        method: 'POST',
        headers: {
            'Authorization': 'Bearer YOUR_JWT_TOKEN'
        },
        body: formData
    });
    
    return await response.json();
};

The tricky part is setting up authentication. You’ll probably want to use JWT authentication plugin for WordPress to handle token generation.

This video walks through the whole Airtable to WordPress sync process step by step:

Once you get the media uploaded, you can update your post meta fields with the new attachment IDs using another REST API call to the posts endpoint.