Feb 05, 2024 05:50 AM
Hi, I am having trouble connecting to the Airtable Library from Google App Script. Google App Script is asking me for a Script ID but I don't seem to be able to find one. I am also confused by the replacement of the Airtable API with PAT which I equally am unsure how to use. Any help would be very useful. I am a bit of a novice. Sorry.
Ultimately, I simply want to copy some data from a Google Sheet into one of my tables.
Solved! Go to Solution.
Feb 05, 2024 06:24 AM
Ah, no worries! You mentioned you weren't too sure how to use your PAT, so here's how I would set up my PAT so that you have a reference:
You'll just need to give it write permissions and select the base you want to send data into
Here's a script that will create new records in a base of your choice:
function main(){
var myHeaders = {
"Authorization": "Bearer YOUR_PAT_HERE",
"Content-Type": "application/json"
};
var raw = JSON.stringify({
"records": [
{
"fields": {
"Label": "Test1"
}
}
]
});
var requestOptions = {
method: 'POST',
headers: myHeaders,
payload: raw
};
var response = UrlFetchApp.fetch("https://api.airtable.com/v0/appz6YcyO3dXfqDWV/tblsCy38oC7N7FE4c/", requestOptions);
console.log(response.getContentText());
}
Just replace "YOUR_PAT_HERE" with your PAT, as well as update the URL in the fetch to be your base and table ID, specifically this bit:
https://api.airtable.com/v0/appz6YcyO3dXfqDWV/tblsCy38oC7N7FE4c
You'd update it so that it was https://api.airtable.com/v0/YOUR_BASE_ID/YOUR_TABLE_ID
And you can get your base and table ID from going to the specific table you need, where the URL would be, for example:
https://airtable.com/appz6YcyO3dXfqDWV/tblsCy38oC7N7FE4c/viwFuVZVRlLEUiCpL?blocks=bip4Em7XSFdFnv5yF
And the base ID would be: appz6YcyO3dXfqDWV
And the table ID would be: tblsCy38oC7N7FE4c
I realize that you probably already some if not most of this stuff, but figured I'd err on the side of oversharing in case it helped! Let me know if you could use more help and I'll see what I can do
Feb 05, 2024 05:59 AM - edited Feb 05, 2024 06:00 AM
Hmm, could you provide the script you've got so far? I'll see if I can help
May I also know how much JavaScript experience you have?
Feb 05, 2024 06:04 AM
Thank you. I have this so far:
Feb 05, 2024 06:24 AM
Ah, no worries! You mentioned you weren't too sure how to use your PAT, so here's how I would set up my PAT so that you have a reference:
You'll just need to give it write permissions and select the base you want to send data into
Here's a script that will create new records in a base of your choice:
function main(){
var myHeaders = {
"Authorization": "Bearer YOUR_PAT_HERE",
"Content-Type": "application/json"
};
var raw = JSON.stringify({
"records": [
{
"fields": {
"Label": "Test1"
}
}
]
});
var requestOptions = {
method: 'POST',
headers: myHeaders,
payload: raw
};
var response = UrlFetchApp.fetch("https://api.airtable.com/v0/appz6YcyO3dXfqDWV/tblsCy38oC7N7FE4c/", requestOptions);
console.log(response.getContentText());
}
Just replace "YOUR_PAT_HERE" with your PAT, as well as update the URL in the fetch to be your base and table ID, specifically this bit:
https://api.airtable.com/v0/appz6YcyO3dXfqDWV/tblsCy38oC7N7FE4c
You'd update it so that it was https://api.airtable.com/v0/YOUR_BASE_ID/YOUR_TABLE_ID
And you can get your base and table ID from going to the specific table you need, where the URL would be, for example:
https://airtable.com/appz6YcyO3dXfqDWV/tblsCy38oC7N7FE4c/viwFuVZVRlLEUiCpL?blocks=bip4Em7XSFdFnv5yF
And the base ID would be: appz6YcyO3dXfqDWV
And the table ID would be: tblsCy38oC7N7FE4c
I realize that you probably already some if not most of this stuff, but figured I'd err on the side of oversharing in case it helped! Let me know if you could use more help and I'll see what I can do
Feb 05, 2024 09:03 AM
Wow! This is awesome. It works perfectly. Thank you so much and thank you for your patience. Please, oversharing is ideal for me. 🙏
Feb 06, 2024 04:09 AM
Glad I could help!
Mar 06, 2024 10:06 AM
Hello !
First of all I want to say Thanks you by your efforts solving this issue. I'm having a similar problem trying to get data using google Apps Script from Airtable.
I've already created my PAT and I'm making a quite simple request. This is my code :
function requestAirtable(url, offset) {
const api_key= 'patJt0....RRL'
if(offset!== undefined){
url = url + '?offset=' + offset
}
var headers ={
'Authorization' : 'Bearer ' + api_key,
'Content-Type' : 'application/json'
}
var options={
headers : headers,
method : 'GET'
}
var response = UrlFetchApp.fetch(url,options).getContentText();
var result = JSON.parse(response)
//console.log('result: ',result.records)
return result
}
My problem is that I'm getting this error :
failed for https://api.airtable.com returned code 401. Truncated server response: {"error":{"type":"AUTHENTICATION_REQUIRED","message":"Authentication required"}}
Thanks you in advance!
Mar 07, 2024 01:34 AM
Hmm, your code looks fine actually. Just to confirm, you modified the "api_key" value so that it wasn't visible in this forum, right? FWIW, this works fine for me in Google Apps Script:
const api_key = 'YOUR_PAT_HERE'
const url = 'YOUR_URL_HERE'
function test(){
requestAirtable(url)
}
function requestAirtable(url, offset) {
if(offset!== undefined){
url = url + '?offset=' + offset
}
var headers ={
'Authorization' : 'Bearer ' + api_key,
'Content-Type' : 'application/json'
}
var options={
headers : headers,
method : 'GET'
}
var response = UrlFetchApp.fetch(url,options).getContentText();
var result = JSON.parse(response)
console.log('result: ',result.records)
return result
}
Mar 07, 2024 01:16 PM
The code is working! I made a mistake when generating the token and just copied the first part (before the ".")... Thanks you very much for testing!
Mar 07, 2024 05:08 PM
Nice! I'm glad you got it sorted!