Created
May 24, 2022 17:52
-
-
Save rileyrichter/21452e706966d568337da0ff4d62b505 to your computer and use it in GitHub Desktop.
Airtable script to create, update, and delete Webflow items
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const config = input.config({ | |
title: "Script Settings", | |
items: [ | |
input.config.text("webflowAPI", { | |
label: "Webflow API Key", | |
}), | |
input.config.text("collectionID", { | |
label: "Collection ID", | |
}), | |
], | |
}); | |
let table = base.getTable("All Team"); | |
let query = await table.selectRecordsAsync(); | |
let records = query.records; | |
for (let record of records) { | |
let body = { | |
fields: { | |
_archived: false, | |
_draft: false, | |
"e-mail": record.getCellValue("Email"), | |
name: record.getCellValue("Name"), | |
"alt-text": record.getCellValue("Alt Text"), | |
title: record.getCellValue("Title").name, | |
photo: record.getCellValue("Image"), | |
}, | |
}; | |
let updateBody = { | |
fields: { | |
name: record.getCellValue("Name"), | |
slug: record.getCellValue("Slug"), | |
_archived: false, | |
_draft: false, | |
"e-mail": record.getCellValue("Email"), | |
"alt-text": record.getCellValue("Alt Text"), | |
title: record.getCellValue("Title").name, | |
photo: record.getCellValue("Image"), | |
}, | |
}; | |
if (record.getCellValue("Status").name === "New") { | |
let response = await remoteFetchAsync( | |
`https://api.webflow.com/collections/${config.collectionID}/items?live=true`, | |
{ | |
method: "POST", | |
body: JSON.stringify(body), | |
headers: { | |
"Content-Type": "application/json", | |
Authorization: `Bearer ${config.webflowAPI}`, | |
"accept-version": "1.0.0", | |
}, | |
} | |
); | |
let webflowResponse = await response.json(); | |
console.log(webflowResponse); | |
console.log(response); | |
table.updateRecordAsync(record, { | |
"Item ID": webflowResponse._id, | |
"Slug": webflowResponse.slug, | |
"Status": {name: "Live"} | |
}); | |
} else if (record.getCellValue("Status").name === "Update") { | |
let webflowID = record.getCellValue("Item ID"); | |
let response = await remoteFetchAsync( | |
`https://api.webflow.com/collections/${config.collectionID}/items/${webflowID}?live=true`, | |
{ | |
method: "PUT", | |
body: JSON.stringify(updateBody), | |
headers: { | |
"Content-Type": "application/json", | |
Authorization: `Bearer ${config.webflowAPI}`, | |
"accept-version": "1.0.0", | |
}, | |
} | |
); | |
let webflowResponse = await response.json(); | |
console.log(webflowResponse); | |
console.log(response); | |
table.updateRecordAsync(record, { | |
"Status": {name: "Live"} | |
}); | |
} else if (record.getCellValue("Status").name === "Remove") { | |
let webflowID = record.getCellValue("Item ID"); | |
let response = await remoteFetchAsync( | |
`https://api.webflow.com/collections/${config.collectionID}/items/${webflowID}?live=true`, | |
{ | |
method: "DELETE", | |
headers: { | |
"Content-Type": "application/json", | |
Authorization: `Bearer ${config.webflowAPI}`, | |
"accept-version": "1.0.0", | |
}, | |
} | |
); | |
let webflowResponse = await response.json(); | |
console.log(webflowResponse); | |
console.log(response); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment