Airtable Examples

The following examples can be used for Airtable Scripting Extensionsarrow-up-right (previously known as Scripting Apps). It will allow you to interact with the Orbiit API from within your Airtable.

Get Members from Orbiit

TODO: Scripting extension

/**
 * Example code to load your Workspace Members to Airtable. Make sure to set
 * the first required variables so it can extract the data. It includes pagination over
 * the endpoint to collect all data.
 */

const tableName = '<YOUR-AIRTABLE-TABLENAME>';
const token = '<YOUR-ORBIIT-TOKEN>';
const workspaceId = '<YOUR-ORBIIT-WORKSPACE-ID>';
const tableName = 'My Orbiit Members Table';

const baseURL = 'https://api.orbiit.ai/v0';
const headers = {
    'Authorization': `Bearer ${token}`, 
    'Content-Type': 'application/json'
};

const url = `${baseURL}/workspaces/${workspaceId}/members`;

// Utility function to help us clear the current Airtable Table.
const clearTable = async (table) => {
    let result = await table.selectRecordsAsync();
    let records = result.records;
    output.markdown(`🗑 Removing **${records.length} records** in ${table.name}`);
    while(records.length > 0) {
      await table.deleteRecordsAsync(records.slice(0,50));
      records = records.slice(50);
    }
}

output.markdown(`# Orbiit Example: Load Members into Airtable`);

const table = base.getTable(tableName);
output.markdown(`Using table: ${table.name}`);

// Clear the table of any data.
await clearTable(table);

/**
 * Recursive function for pagination.
 * @param endpoint 
 */
let globalResults = [];
async function fetchData(endpoint, limit = 10, cursor = null) {
  try {
    let cursorQueryParam = '';
    if (cursor) {
      cursorQueryParam = `&cursor=${cursor}`;
    }
    const response = await remoteFetchAsync(`${endpoint}?limit=${limit}${cursorQueryParam}`, {
        "method": "GET",
        "headers": headers
    });
    const data = await response.json();
    // console.log(`[Intermediate] Retrieved ${data.results.length} results.`);

    // Add the results to the global array.
    globalResults.push(...data.results);
    console.log(`[Intermediate] Currently we have collected ${globalResults.length}/${data.totalCount} results.`);

    if (data.pageInfo.hasNextPage) {
      return fetchData(endpoint, limit, data.pageInfo.endCursor);
    }
  } catch (error) {
    console.error('Error fetching data:', error);
  }
}

const responseData = await fetchData(url, 50);

output.markdown(`👉 Inserting ${globalResults.length} records in ${table.name}`);
for (let member of globalResults) {
    console.log(member)
    const record = {
        "id": member.id,
        "Email": member.email,
        "Created At": member.createdAt,
    }
    if (member?.fields?.first_name) record["First Name"] = member.fields.first_name;
    if (member?.fields?.last_name) record["Last Name"] = member.fields.last_name;
    if (member?.fields?.role) record["Role"] = member.fields.role;
    await table.createRecordAsync(record)
}

Last updated