Help

Re: Is it possible to use Ajax in an Airtable script?

Solved
Jump to Solution
3545 5
cancel
Showing results for 
Search instead for 
Did you mean: 
MrLuke
6 - Interface Innovator
6 - Interface Innovator

I want to make an automation that runs a script, and in that script I'd like to call an ajax function (either with jQuery somehow or with XMLHttpRequest() etc.) to grab some external data from a Google calendar. Is this possible? I've searched around and can't seem to find any information on it and the script editor says the ajax functions aren't defined, so I'm not sure if/how I can include jQuery if that can help.

Thanks

1 Solution

Accepted Solutions
subinbabu
5 - Automation Enthusiast
5 - Automation Enthusiast

Sorry, Luke, it was my mistake; the Airtable script editor does not support the require() function, so you can't use it to include the jQuery library in your script.

Use the fetch() or XMLHttpRequest() JavaScript built-in functions to make your requests.

Here's an example of how you can use the fetch() function to make a GET request to the Google Calendar API to retrieve events for a specific calendar:

 

var calendarId = 'primary';
var apiKey = 'YOUR_API_KEY';

fetch(`https://www.googleapis.com/calendar/v3/calendars/${calendarId}/events?key=${apiKey}`, {
    method: 'GET',
    headers: {
        'Content-Type': 'application/json'
    }
})
.then(response => response.json())
.then(data => {
    // Do something with the data
    console.log(data);
})
.catch(error => {
    console.error(error);
});

 

Thank you.

See Solution in Thread

6 Replies 6
subinbabu
5 - Automation Enthusiast
5 - Automation Enthusiast

Yes, it is possible. However, there are a few things to consider:

Airtable's script editor does not support jQuery by default, so you would need to include the jQuery library in your script to use its AJAX functions. You can include jQuery by adding the following line at the top of your script:

 

var $ = require('https://code.jquery.com/jquery-3.6.0.min.js');

 

I hope it helps. Thank you

I tried this, but all I get when I test the script is:

ERROR

ReferenceError: require is not defined
    at main on line 5

Hey @MrLuke - you can use the fetch method in Automation Run Script Actions to call external APIs.

You cannot require third-party dependencies using `require` in Scripting Extensions or Automation Run Script Actions. You can include npm packages and client-side Javascript with Custom Extensions, though they cannot be triggered by an Automation that you mentioned in your original question.

For your use case, I anticipate auth into Google's APIs may be difficult but probably possible. 

And just since you mentioned Google Calendar specifically, are you aware of the native Google Calendar Sync feature? (no code required)

subinbabu
5 - Automation Enthusiast
5 - Automation Enthusiast

Sorry, Luke, it was my mistake; the Airtable script editor does not support the require() function, so you can't use it to include the jQuery library in your script.

Use the fetch() or XMLHttpRequest() JavaScript built-in functions to make your requests.

Here's an example of how you can use the fetch() function to make a GET request to the Google Calendar API to retrieve events for a specific calendar:

 

var calendarId = 'primary';
var apiKey = 'YOUR_API_KEY';

fetch(`https://www.googleapis.com/calendar/v3/calendars/${calendarId}/events?key=${apiKey}`, {
    method: 'GET',
    headers: {
        'Content-Type': 'application/json'
    }
})
.then(response => response.json())
.then(data => {
    // Do something with the data
    console.log(data);
})
.catch(error => {
    console.error(error);
});

 

Thank you.

Thanks for this! The `fetch` method worked perfectly. I had already tried `XMLHttpRequest()` but that didn't work and was treated like it wasn't supported.

MrLuke
6 - Interface Innovator
6 - Interface Innovator

Thanks, I've got it working with `fetch` now. 🙂 

I have tried to the Google Calendar sync, which is great, but we need some additional flexibility/editing which synced tables don't appear to allow or had some limitations on which a custom script should hopefully avoid.