Webflow CMS Scripts

01 - Introduction

Welcome to Webflow CMS Scripting! Over the next few sessions, we are going to go over how you can Create, Update and Delete Webflow Items using Airtable scripts.

//Trigger: When a person clicks the Create/Update Button

let table = base.getTable("YouTube v2");
let record = await input.recordAsync('Select a record to use', table)

let webflowID = record.getCellValue("Webflow ID");
let airtableID = record.getCellValue("recordID");
let api = "";
let webhookURL = "";
let cID ="";

//IF Webflow Item ID exists
if (webflowID){
        //Action: Update the Item in Webflow
        let calltowebflow = await remoteFetchAsync(`https://api.webflow.com/collections/${cID}/items/${webflowID}?live=true`,
        {
            method:'PATCH',
            body:JSON.stringify({
                fields: {
                    "name": record.getCellValue("Title"),
                    "description":record.getCellValue("Video Summary"),
                    "url": record.getCellValue("URL"),
                    "video": record.getCellValue("URL"),
                    "categories": record.getCellValue("Webflow Item ID (from Tools)"),
                    "experience": record.getCellValue("Experience").name,
                    "deep-dive": record.getCellValue("Item ID (from Additional Lessons)")[0],
                    "thumbnail": record.getCellValue("Thumbnail")[0].url,
                    "_archived":false,
                    "_draft":false
            }
            }),
            headers:{
                'Content-type':'application/json',
                'Authorization':`Bearer ${api}`, 
                'accept-version':'1.0.0'
            }
        });

        console.log(calltowebflow);

        //Fire a webhook that converts markdown to HTML
        const markdownWebhook = await fetch (`${webhookURL}?recordID=${airtableID}`)

        output.text('This record has an item ID');

}

//ELSE Webflow Item ID does not exist
else {
    //Action: Create a new Item in Webflow
    let calltowebflow = await remoteFetchAsync(`https://api.webflow.com/collections/${cID}/items?live=true`,
        {
            method:'POST',
            body:JSON.stringify({
                fields: {
                    "name": record.getCellValue("Title"),
                    "description":record.getCellValue("Video Summary"),
                    "url": record.getCellValue("URL"),
                    "video": record.getCellValue("URL"),
                    "categories": record.getCellValue("Webflow Item ID (from Tools)"),
                    "thumbnail": record.getCellValue("Thumbnail")[0].url,
                    "_archived":false,
                    "_draft":false
                }
            }),
            headers:{
                'Content-type':'application/json',
                'Authorization':`Bearer ${api}`, 
                'accept-version':'1.0.0'
            }
        });
    

    let calltoWebflowparsed = await calltowebflow.json();

    console.log (calltoWebflowparsed);

    //Action: Update the Airtable Record w/ Webflow ID and Slug
    await table.updateRecordAsync (record,{
        "Webflow ID":calltoWebflowparsed["_id"],
        "Slug":calltoWebflowparsed["slug"]
    })
    output.text('This record does not have item ID');
}

Copy Script