Airtable API and Scriptable Widget

I found this script in the examples thread and wanted to make a Hello World widget. I set up a base in Airtable got my API key and set up the script to connect to my base but I get nothing in Scriptable. No data retrieved and no error message.

// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: deep-purple; icon-glyph: magic;
let baseURL = "https://api.airtable.com/v0/*********************/Books?maxRecords=100&view=Grid%20view"    
let r = new Request(baseURL);
r.headers = { 'Authorization': 'Bearer key**************'};
let json = await r.loadJSON(); 
let titles = json['records'];
let randomBook = (titles[Math.floor(Math.random()*titles.length)]);
let titleText = randomBook['fields']['Title'];
let bookAuthor = randomBook['fields']['Author'];


let widget = createWidget();

if (config.runsInWidget) {
    let widget = createWidget();
    Script.setWidget(widget);
    Script.complete();
}

function createWidget() {
    let w = new ListWidget();
    let widgetText = w.addText(titleText);
    widgetText.textSize = 15;
    let widgetSubText = w.addText(bookAuthor);
    widgetText.textSize = 13;    
    return w
}

In the cli I can use curl to connect and receive the records from the base but nothing in Scriptable.
What have I got wrong?

I tried this similar script in Scriptable. It is for a test base I have which contains a table called Fruits, where each record has a type and colour.

let baseURL = "https://api.airtable.com/v0/appnPNsDcvlFkTpve/Fruits?maxRecords=3&view=Grid%20view"    
let r = new Request(baseURL);
r.headers = { 'Authorization': 'Bearer key01234567891234'};
let json = await r.loadJSON(); 
json['records'].map(record => console.log(record['fields']['Type']))

This then listed the type of each record as a new entry in the console log.

I would recommend that you start with a really simple case like this and check your base URL and bearer token.

Hope that helps.

Thank you for your reply. I added
json['records'].map(record => console.log(record['fields']['Author']))
and I do see the data in Console Log. It is helpful to see that the API is working and data is received. Now I need to get widget created with data passed to it.

By that, do you mean nothing is displayed when you run this script in Scriptable? Because that would be expected: your script is setup to only display anything when it is called from inside a widget. For it to display anything in Scriptable, you’d need to expressly display the widget:

let widget = createWidget();

if (config.runsInWidget) {
    Script.setWidget(widget);
    Script.complete();
} else {
    widget.presentLarge(); // or any other size, see documentation 
}

(I’m also pretty sure you do not need the Script.complete() part).

This code is working. It is a mashup of a couple of examples I found. Problem now is getting the book cover image to display in the widget. I’m not understanding how addImageAtURL works. The img URL is retrieved but I’m getting error expected type string but got value of type [string]

‘’’
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: deep-purple; icon-glyph: magic;

const DARK_MODE = true
const widgetSize = config.widgetFamily || ‘medium’
const textSize = 12
const spacing = { normal: 8, smaller: 6, vs: 5, widget: 10 }

const fetchData = async (url, type = ‘loadJSON’) => {
const request = new Request(url)
const res = await requesttype
return res
}

let baseURL = "https://api.airtable.com/v0/*******/Books?maxRecords=100&view=Grid%20view"
let r = new Request(baseURL);
r.headers = { ‘Authorization’: 'Bearer keyz
’};
let json = await r.loadJSON();
let titles = json[‘records’];
json[‘records’].map(record => console.log(record[‘fields’][‘Title’]))
json[‘records’].map(record => console.log(record[‘fields’][‘Author’]))
json[‘records’].map(record => console.log(record[‘fields’][‘URL’]))

let randomBook = (titles[Math.floor(Math.random() * titles.length)]);
let titleText = randomBook[‘fields’][‘Title’];
let bookAuthor = randomBook[‘fields’][‘Author’];
let bookCover = randomBook[‘fields’][‘URL’];

let widget = createWidget();

if (config.runsInWidget) {
let widget = createWidget();
Script.setWidget(widget);
Script.complete();
}

function createWidget() {
let w = new ListWidget();
w.backgroundColor = new Color("#9A1B1A");
let widgetText = w.addText(titleText);
widgetText.textSize = 15;
let widgetSubText = w.addText(bookAuthor);
widgetSubText.textSize = 18;
let widgetImage = w.addImageAtURL(bookCover);

return w

}
‘’’