// AI Layoffs for Airtable: each company's layoffs on record, written into a field of your table. // From https://ailayoffs.org/integrations // In your base: Extensions > Add an extension > Scripting, paste this script, then Run. // It asks for the table, the field holding each company's web domain (or name), the text field to write into, // and your AI Layoffs key (a free 30-day key is at https://ailayoffs.org/integrations). // Rows whose result field is already filled are skipped, so running it again only looks up new companies. const settings = input.config({ title: "AI Layoffs: layoffs on record for each company", description: "Writes each company's job cuts, cuts linked to AI and US WARN notices into a text field.", items: [ input.config.table("table", { label: "Table" }), input.config.field("companyField", { parentTable: "table", label: "Company web domain or name" }), input.config.field("resultField", { parentTable: "table", label: "Write the result into (a text field)" }), input.config.text("apiKey", { label: "Your AI Layoffs key" }), ], }); function describe(body) { if (!body.matched) return "Nothing on record"; const s = body.summary || {}; const latest = [s.latest_job_cut, s.latest_warn_notice].filter(Boolean).sort().pop(); return s.job_cuts + " job cuts on record (" + s.job_cuts_linked_to_ai + " linked to AI), " + s.warn_notices + " US WARN notices since 2019" + (latest ? "; latest " + latest : "") + ". " + (body.pages && body.pages[0] ? body.pages[0] : ""); } const { table, companyField, resultField, apiKey } = settings; const query = await table.selectRecordsAsync({ fields: [companyField, resultField] }); const todo = query.records.filter((r) => String(r.getCellValueAsString(companyField) || "").trim() && !String(r.getCellValueAsString(resultField) || "").trim()); output.text("Looking up " + todo.length + " companies."); let updates = []; let done = 0; for (const record of todo) { const value = record.getCellValueAsString(companyField).trim(); const param = /\./.test(value) && !/\s/.test(value) ? "domain" : "name"; const response = await remoteFetchAsync("https://ailayoffs.org/api/v1/company?" + param + "=" + encodeURIComponent(value), { headers: { "X-API-Key": apiKey.trim() } }); const body = await response.json(); if (response.status === 401 || response.status === 403) { output.text(body.error || "The key was refused."); break; } updates.push({ id: record.id, fields: { [resultField.id]: response.ok ? describe(body) : body.error || "Lookup failed" } }); done++; if (updates.length === 50) { await table.updateRecordsAsync(updates); updates = []; } } if (updates.length) await table.updateRecordsAsync(updates); output.text("Done: " + done + " companies looked up.");