Help

Save the date! Join us on October 16 for our Product Ops launch event. Register here.

How to use await fetch to populate a field for API requests with bearer token?

2831 2
cancel
Showing results for 
Search instead for 
Did you mean: 
BIG
4 - Data Explorer
4 - Data Explorer

Hi guys and gals 🙂

I am a newbie to Airtable, can someone help me out with the script and automation?

I want to populate two fields in the column Notes and Points with the data returning from a web API call using bearer token authorization, but I also need to add the parameters included in Product and Quantity in the API call and filter out specific elements from the json file to be stored in the field Notes and Points.

I have the following columns:

RunWork | Status | Product | Quantity | Notes | Points

RunWork contains the checkbox.

Status is either blank, In Progress, Done

Notes and Points will get populated with json data from an API call.

My logic would be, 

If RunWork is checked,

-> Then change Status to In Progress

-> Then populate Notes and Points using parameters in Product and Quantity

When all fields are filled,

-> then change Status to Done

Any help or code example is greatly appreciated! 

2 Replies 2

Hey @BIG
I'd like to make sure I'm understanding what you're trying to do a bit better.

You have a checkbox field (RunWork) that triggers an automation once it is checked to true.
When that automation runs, the record's Status single-select field is set to In Progress
You the make an API call to another service that is authenticated with a bearer token.

The HTTP request body contains the data from the trigger record's Product and Quantity fields.

The data returned from the API call should then be used to populate the Notes and Points fields on the triggering record.
Finally, the Status single-select field is set to Done.

If my understanding is correct, then I would probably implement something like this:

const config = input.config();
const { recordId } = config;

const yourTableId = "your_table_id";
const table = base.getTable(yourTableId);

const fields = [
    //Notes
    "notes_field_id",
    //Points
    "points_field_id",
    //Quantity
    "quantity_field_id",
    //Product
    "product_field_id",
    //Status
    "status_field_id",
    //RunWork
    "runwork_field_id"
];

const record = await table.selectRecordAsync(recordId, {fields: fields});

await table.updateRecordAsync(recordId, {
    //Status
    "status_field_id": {name: "In Progress"}
});

const body = {
    product: (function () { 
        let value = record.getCellValue(fields[3]);
        return (value ? value : "")
    }),
    quantity: (function () {
        let value = record.getCellValue(fields[2]);
        return (value ? value : "")
    })
};

const apiKey = "your_bearer_token";
const apiUrl = "your_api_endpoint";
const options = {
    method: "GET",
    headers: {
        "Content-Type": "application/json",
        "authorization": `Bearer: ${apiKey}`
    },
    body: JSON.stringify(body)
};

const apiQuery = await fetch(apiUrl, options)
.then(response => {
    if (response.status === 200) {
        return response.json();
    } else {
        console.log(`Request Failed With Status: ${response.status}`)
        return undefined;
    }
});

if (apiQuery) {
    await updateRecordAsync(recordId, {
        //RunWork is reset to false.
        "runwork_field_id": false,
        //Notes
        "notes_field_id": apiQuery.product,
        //Points
        "points_field_id": apiQuery.quantity,
        //Status
        "status_field_id": {name: "Done"}
    });
};

I wrote this rather quickly and am missing a few key details about your schema and implementation to properly write an example, but if my understanding is correct, then that's how I would probably begin to approach it.

Oh my god, you are so helpful! I thank you so much! I will try to implement and test if it works.