Save the date! Join us on October 16 for our Product Ops launch event. Register here.
May 25, 2022 09:08 AM
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.
Solved! Go to Solution.
May 25, 2022 04:21 PM
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.
May 25, 2022 09:43 AM
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
})
}
May 25, 2022 12:06 PM
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…
ReferenceError: json is not defined
at main on line 11
May 25, 2022 12:26 PM
Create a variable after the response variable that is
let json =await response.json();
May 25, 2022 12:41 PM
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
May 25, 2022 12:41 PM
Thanks for the help with this! Saves me hours of trial and error.
May 25, 2022 03:01 PM
Remove the line console.log(await response.json());
You can console.log(json) after you call it to see the JSON fields.
May 25, 2022 03:27 PM
It kinda works but still wont write to the field for some reason. Could it be those i on line 12?
May 25, 2022 04:01 PM
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.
May 25, 2022 04:12 PM
I was actually looking to pull out the lot. It’s around 9000 words. Too much?