Last active
May 28, 2021 13:30
-
-
Save robert-hoffmann/3495ba27c45b5b8745e845994ac71b1c to your computer and use it in GitHub Desktop.
Get all records from AirTable with simple Fetch/REST (Vanilla JS)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let airtable = { | |
| // Name of the view (change this) | |
| view: "api", | |
| // Database identifier (change this) | |
| database: "appDatabaseIdentifier", | |
| // Auth Key => Use a client with read-only permissions for security (change this) | |
| auth: "keyAuthKey" | |
| }; | |
| function fetchFromAirtable(table) { | |
| let buffer = []; | |
| // set authentification | |
| let requestHeaders = new Headers({ | |
| 'Authorization': `Bearer ${airtable.auth}`, | |
| 'Cookie' : 'brw=brwoHThdjGXOuwBHo' | |
| }); | |
| // create request options | |
| let requestOptions = { | |
| method : 'GET', | |
| headers : requestHeaders, | |
| redirect: 'follow' | |
| }; | |
| // endpoint | |
| let url = `https://api.airtable.com/v0/${airtable.database}/${table}?view=${airtable.view}`; | |
| //* get all records recursively (airtable uses pagination after 100 records) | |
| async function getAllRecords(endpoint) { | |
| // Data is ordered by settings specified in View | |
| let result = await fetch(encodeURI(endpoint), requestOptions).then(response => response.json()); | |
| // i just wan my fields, not all the stuff from AirTable | |
| result.records.forEach(i => { | |
| buffer.push(i.fields); | |
| }); | |
| if (!!result.offset) { | |
| getAllRecords(url.concat(`&offset=${result.offset}`)); | |
| } | |
| return buffer; | |
| } | |
| return getAllRecords(url); | |
| } | |
| // get all records | |
| let result = fetchFromAirtable("someTable"); | |
| console.log(result); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [ | |
| { | |
| "id": 4, | |
| "pcId": 6, | |
| "title": "Jean Bouin", | |
| "parentId": 2, | |
| "position": 0 | |
| }, | |
| { | |
| "id": 6, | |
| "pcId": 7, | |
| "title": "Salle & Bar", | |
| "parentId": 5, | |
| "position": 0 | |
| } | |
| ] |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I dont want to use the bloated AirTable API, or nodeJS, or implement their pagination...
So there you go ;-)
PS:
You don't have to use the urgly URLencoded names of the "table" or "view" like they say in their documentation, using the uniqueId you see in the URL when you access AirTable works just fine.
ex:
http://airtable.com/tblpocFmXXpCxSXXX/viwEFw1hXXXLiGXXX?blocks=hide
So the final API call might look something like this:
https://api.airtable.com/v0/appApac8XUXXXbQcA/tblpocFmXXpCxSXXX?view=viwEFw1hXXXLiGXXX
Personally i create a view called "api", to which i pre apply sorting & stuff, and tell ppl "DONT TOUCH", and use that in all my api calls.