Help

Re: Using Airtable Library from Google App Script using PAT

Solved
Jump to Solution
2860 2
cancel
Showing results for 
Search instead for 
Did you mean: 
RichardLG
5 - Automation Enthusiast
5 - Automation Enthusiast

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.

1 Solution

Accepted Solutions

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:

Screenshot 2024-02-05 at 10.19.46 PM.png
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

See Solution in Thread

9 Replies 9
TheTimeSavingCo
18 - Pluto
18 - Pluto

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?

Thank you. I have this so far:

// Airtable integration with PAT
const airtable = Airtable.configure({
endpointUrl: 'https://api.airtable.com',
apiKey: ScriptApp.getEnvironment().getVariable('AIRTABLE_PAT')
});
const table = airtable.base('appCf1sGZAwiIBU6k/tbleWwX6yxhZY8aHk').table('tbl3QwmZLBau3rK5P');
 
but Airtable.configure requires the library.
 
Experience? Not much. Enough to do small things with the help of ChatGPT and Bard. Sorry.

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:

Screenshot 2024-02-05 at 10.19.46 PM.png
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

Wow! This is awesome. It works perfectly. Thank you so much and thank you for your patience. Please, oversharing is ideal for me. 🙏

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!


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
 
}

 

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!

Nice! I'm glad you got it sorted!