Help

Aritable listRecords API not returning fields data

Topic Labels: API Scripting
Solved
Jump to Solution
305 5
cancel
Showing results for 
Search instead for 
Did you mean: 
Sri1
6 - Interface Innovator
6 - Interface Innovator

Hi

I am trying to use below code (Google Apps Script) to get data from Airtable. The fetch request succeeds but the fields are returned as text [Object]. I am not able to see actual data. However the request works from other HTTP clients like Bruno.

I have observed another issue. The code with small change marked below used to return data perfectly till couple of days ago but suddenly started throwing error 422. Hence I made below change and the result is missing data.

Even GET method results same.

Please suggest how this can be resolved as this is a show stopper.

Currently the result is like below

{ id: 'recrR654MViYPpGG5', createdTime: '2024-01-30T10:38:29.000Z', fields: [Object] }

My code is as below

let invoicenumin = "EI000002"
const scriptProperties = PropertiesService.getScriptProperties();
airdbKey = scriptProperties.getProperty('airdbkey')
const fieldObj = [
"quantity",
"unitprice",
"lineamount",
"itemdescription"
]
let filter = "{invoicenum} = " + '\''+invoicenumin +'\''

const atdata = {
//"records": [{"fields": fieldObj}] /* this used to WORK EARLIER, BUT STOPPED SUDDENLY AND NOW THROWING ERROR 422*/
"fields": fieldObj,
"filterByFormula": filter
}
const options =
{
"contentType": 'application/json',
"headers" : { "Authorization": "Bearer "+airdbKey },
"method" : 'POST',
'payload' : JSON.stringify(atdata)
};
const API_URL ='https://api.airtable.com/v0/<myappid>/Invoiceline/listRecords'
console.log('Options are', options)
const retval = UrlFetchApp.fetch(API_URL,options)

 

1 Solution

Accepted Solutions
Sri1
6 - Interface Innovator
6 - Interface Innovator

I think in the above code

console.log('records:', records)

In the above code if I replace console.log('records:', records) with 

JSON.stringify(records), the issue of not getting actual values but [Object] is resolved
And I think this is the solution even for my existing code which I presented above originally. While it workied earlier without using the JSON.stringify suddenly it needed this, bit strange but any way I got the solution
Thanks

See Solution in Thread

5 Replies 5

Here's some code that works with Google Apps Script that filters for records where the field "Colour" is "Blue", and only returns the values for the fields "Colour" and "Category", and presents the

The API docs for using the query parameters are quite useful, and the API encoder Airtable provides is also super handy for this:
https://airtable.com/developers/web/api/list-records
https://codepen.io/airtable/full/MeXqOg

const api_key = 'YOUR_PAT_HERE'
const url = 'https://api.airtable.com/v0/appz6YcyO3dXfqDWV/tblzlSMWTq31SyL4C?fields%5B%5D=Colour&fields%5B%5D=Category&filterByFormula=%7BColour%7D%3D%22Blue%22'
function get(){
   let records = requestAirtable(url)
   console.log('records:', records)
}

function requestAirtable(url, offset) {
  
  if(offset!== undefined){
    url = url + '?offset=' + offset
  }

  let headers ={
  'Authorization' : 'Bearer ' + api_key,
  'Content-Type' : 'application/json'
  }

  let options={
    headers : headers,
    method : 'GET',
  }

  let response = UrlFetchApp.fetch(url,options).getContentText();
  let result = JSON.parse(response)
  console.log('result: ',result)

  return result.records
}


 

Hi

Thanks for your response. Here what is offset in the above code. If I remove offset and try the code with my airtable details in url along with view name instead of filter by formula (as GET being used) I am getting error 422.

Sri1
6 - Interface Innovator
6 - Interface Innovator

Apologies, I am not getting error 422, but instead of actual data I am getting [Object] when the column is a lookup field, earlier it used return actual value within an array

Sri1
6 - Interface Innovator
6 - Interface Innovator

I think in the above code

console.log('records:', records)

In the above code if I replace console.log('records:', records) with 

JSON.stringify(records), the issue of not getting actual values but [Object] is resolved
And I think this is the solution even for my existing code which I presented above originally. While it workied earlier without using the JSON.stringify suddenly it needed this, bit strange but any way I got the solution
Thanks
danieljarquin
4 - Data Explorer
4 - Data Explorer

It sounds like you're encountering an issue with the Airtable API where the listRecords endpoint is not returning the expected fields data. There could be a few reasons for this issue:

  1. Field Permissions: Make sure that the API key you're using has permission to access the fields you're trying to retrieve. If the fields are restricted, the API may not return their data.

  2. Field Visibility: Check if the fields you're trying to access are visible to the view you're using with the API. If they're hidden in the view, they won't be returned in the API response.

  3. Field Names: Ensure that you're using the correct field names in your API request. Field names are case-sensitive and must match exactly what's defined in your Airtable base.

  4. Authentication: Double-check your authentication method (API key or OAuth token) to ensure it's correctly included in the API request headers