Help

Re: Script Help - Writing Console.Log to a field

Solved
Jump to Solution
4296 0
cancel
Showing results for 
Search instead for 
Did you mean: 
Acefast
5 - Automation Enthusiast
5 - Automation Enthusiast

I have managed to create a get request (I’m 90% no code) and then info is coming back but how to then write it to a field?.


let table = base.getTable("TARIC Code")
let url = "https://www.trade-tariff.service.gov.uk/api/v2/commodities/";
let config = input.config();
let response = await fetch(url + config.commodityCode );
console.log(await response.json())

I want to take the result directly into a field to then sift through it for what I need.

1 Solution

Accepted Solutions

I have no idea if you will hit any limits. I pulled 1 code and it gave me 500+ records. The thing is, ‘the whole lot’ is a bunch of things that can be a number of different types of data (text, numbers…) so if you want to put that data into airtable the type of field needs to match the type of data you have.
Since you are going through the effort of writing a script, you should review the data you are getting and put it in the appropriate place in airtable.

It looks like each object has an id, type and attributes (amongst other things). Try just putting in the id and see how that data looks. Then add the type and grow it from there.

See Solution in Thread

13 Replies 13

Hi @Acefast
Here is an example of an API request that writes to the Airtable base

let table = base.getTable('Table 1');

//Fetch url and translate to json
let url = await fetch('https://reqres.in/api/users');
let json = await url.json();

//Name your columns 'First Name', 'Last Name' and 'Email'
//Loop over the json object (key/value pairs)
for (i=0; i < json.data.length; i++) {
    let recordId = await table.createRecordAsync({
        "First Name": json.data[i].first_name,
        "Last Name": json.data[i].last_name,
        "Email": json.data[i].email
    })
}

https://builtonair.com/script/easy-external-api-example/

Thanjs for the coming back quickly mate. I’m now almost there with it but…

let table = base.getTable("TARIC Code");

let url = "https://www.trade-tariff.service.gov.uk/api/v2/commodities/";
let config = input.config();
let response = await fetch(url + config.commodityCode );
console.log(await response.json())

//Name your columns 'First Name', 'Last Name' and 'Email'
//Loop over the json object (key/value pairs)
for (i=0; i < json.data.length; i++) {
let recordId = await table.createRecordAsync({
"Included Attribute Document Codes": json.data[i].included,
    })
}

I’m getting this error…

ERROR

ReferenceError: json is not defined

at main on line 11

Create a variable after the response variable that is

let json =await response.json();

Acefast
5 - Automation Enthusiast
5 - Automation Enthusiast

I’m still getting a final error with this code…


let table = base.getTable("TARIC Code");

let url = "https://www.trade-tariff.service.gov.uk/api/v2/commodities/";
let config = input.config();
let response = await fetch(url + config.commodityCode );
console.log(await response.json());
let json = await response.json();

//Name your columns 'First Name', 'Last Name' and 'Email'
//Loop over the json object (key/value pairs)
for (i=0; i < json.length; i++) {
let recordId = await table.createRecordAsync({
"included Atribute Document Codes": json.data[i].included
    })
}

Error = TypeError: Body stream already read
at main on line 8

Acefast
5 - Automation Enthusiast
5 - Automation Enthusiast

Thanks for the help with this! Saves me hours of trial and error.

Remove the line console.log(await response.json());

You can console.log(json) after you call it to see the JSON fields.

Acefast
5 - Automation Enthusiast
5 - Automation Enthusiast

Screenshot 2022-05-25 at 23.26.04

It kinda works but still wont write to the field for some reason. Could it be those i on line 12?

I am unsure what you want to do with the data once you get it. You need to review the JSON data and determine which parts of it you want to put in a cell.

for (let i=0; i < json.included.length; i++) {
let recordId = await table.createRecordAsync({
"HMRC JSON": json.included[i].attributes.title
    })
}

This will get you the records that have title in the JSON and put them in the field. But looking at the API there is a lot more that can be extracted.

Acefast
5 - Automation Enthusiast
5 - Automation Enthusiast

I was actually looking to pull out the lot. It’s around 9000 words. Too much?