Airtable script to fill in missing data

This example shows you how to add a scheduled automation that fills in missing data using aito.ai predictions.

Uploading data from Airtable to aito.ai

aito.ai extension for Airtable is in review, and hopefully live soon. Check back please.

Using predictions in automation scripts

In this example we create an automation that fills in the best product category for the new products in your inventory, based on their brand, name and description.

Here's how our starting point looks like, the last four products don't have the category defined. Also we assume that you already have the data uploaded to aito.ai.

Create a custom automation

Kick off by choosing the automations tab from the right pane, unless you already have that open, and click "Create a custom automation".

Create a trigger

There might be several ways to trigger your automation, but in this example we use "When record matches conditions", and set the rules according to the following picture.

Another typical way is to create a timed event, that triggers e.g. every 15 minutes.

Write some script

For the action, choose "Run script". This will open a full screen editor to write your script in to.

As a first step, add the input variable that the trigger gives to the automation script. This is done on the left side of the editor, and below is an example that gets you going.

Next, let's get scripting! This broken to chewable chunks.

First block gets the chosen record (id) in to a variable, and then gets the matching record from the table.

// Assign input record id to a variable
let inputConfig = input.config();
let recordId = inputConfig.recordID;

// Get one record based on id
let table = base.getTable("product_inventory");
let queryResult = await table.selectRecordsAsync();
let record = queryResult.getRecord(recordId);

Then let's get aito.ai ready for use. First, configure your instance url, as well as give the read-only API key (you'll find these from the Console).

// Configure aito.ai API (using read-only key)
let aitoUrl = "https://public-1.api.aito.ai/api/v1/_predict";
let aitoKey = "bvss2i2dIkaWUfBCdzEO89LpxUkwO3A24hYg8MBq";

Second, we construct a query that will go to aito.ai. Find more examples about queries here, but simply define the table where the training data is, define fields in where-block for inputs and then the prediction target.

// Construct aito.ai _predict query body
let body = {
  "from": "airtable-product-inventory",
  "where": {
    "description": record.getCellValue("description"),
    "name": record.getCellValue('name'),
    "brand": record.getCellValue('brand')
  },
  "predict": "category",
  "limit": 1
}

With this, we are ready to send the query to Aaito.aiito for predictions. Here's how it happens. We naturally recommend catching errors better than here in the simplified examples.

// Make a request to aito.ai HTTP API
let response = await fetch(aitoUrl, {
    method: 'POST',
    body: JSON.stringify(body),
    headers: {
        'Content-Type': 'application/json',
        'x-api-key': aitoKey,
    },
});
let data = await response.json();

One step left! Based on the results from aito.ai, we then update the original record's category, as well as data to "confidence" field, so that it is easier to track the accuracy of predictions later on.

// Update a record in the table based on aito.ai result
await table.updateRecordAsync(recordId, {
    "category": data.hits[0].feature,
    "confidence": data.hits[0].$p,
})
console.log("Record updated!")

Test

Coding done, all is in place! You can test your script with the Test-button in the top right corner, and if all is great, close the full screen editor with "Finish editing". After closing, remember to click "Done" on the script block to finish the entire process.

\

Last updated