How to Use Apps Script to Make a Request to a Star Wars API: A Step-by-Step Guide
Learn how to use Google Apps Script to make an API request to the Star Wars API and get data on your favorite characters, planets, and more
Have you ever wanted to get data from an external source and use it in your Google Sheets? Maybe you want to fetch some weather information, stock prices, currency rates, or even some Star Wars data. Well, you're in luck! In this blog post, I'll show you how to make API requests using Google Apps Script within Google Sheets. It's easier than you think!
What is an API?
API stands for Application Programming Interface. It's a way for different applications or services to communicate with each other and exchange data. For example, when you use Google Maps on your phone, it sends an API request to Google's servers and gets back the map data and directions.
An API request usually has two parts: a URL (Uniform Resource Locator) and some parameters. The URL is the address of the service or resource you want to access. The parameters are additional information that specify what kind of data you want and how you want it formatted.
Prerequisites
To follow along with this tutorial, you will need:
- A Google account
- Access to Google Drive
- Basic knowledge of JavaScript
Step 1: Create a new Google Sheet and open the script editor
First, create a new blank Google Sheet and name it "API Requests Example". Then, go to Tools > Script editor... to open the script editor in a new tab.
In the script editor, name your project "API Requests Example" as well.
Step 2: Write a function to call the StarWars API
The Star Wars API is a simple API that returns facts from Star Wars. For example, if you request https://swapi.py4e.com/api/people/2/ you will get data about C-3PO.
To call this API from Google Apps Script, we can use the UrlFetchApp service, which allows us to make HTTP requests. The following function uses UrlFetchApp.fetch() method to get data about the planet of Tatooine:
function getStarWarsData() {
var url = 'https://swapi.py4e.com/api/planets/1';
var response = UrlFetchApp.fetch(url);
var data = JSON.parse(response.getContentText());
console.log(data)
return data;
}
Step 3: Run the Function
Now you can run the function and a dialog box will appear asking for permission to access the external service:
Click “Continue”.
Another dialog box will appear asking you to choose the account to use to access the external service. Choose the account you want to use and click “Allow”.
If the script requires additional permissions, another dialog box may appear asking for those permissions. Follow the same steps to grant those permissions.
Now in the console, you will get some data about Tatooine that will look like:
{ name: 'Tatooine',
rotation_period: '23',
orbital_period: '304',
diameter: '10465',
climate: 'arid',
gravity: '1 standard',
terrain: 'desert',
surface_water: '1',
population: '200000',
residents:
[ 'https://swapi.py4e.com/api/people/1/',
'https://swapi.py4e.com/api/people/2/',
'https://swapi.py4e.com/api/people/4/',
'https://swapi.py4e.com/api/people/6/',
'https://swapi.py4e.com/api/people/7/',
'https://swapi.py4e.com/api/people/8/',
'https://swapi.py4e.com/api/people/9/',
'https://swapi.py4e.com/api/people/11/',
'https://swapi.py4e.com/api/people/43/',
'https://swapi.py4e.com/api/people/62/' ],
films:
[ 'https://swapi.py4e.com/api/films/1/',
'https://swapi.py4e.com/api/films/3/',
'https://swapi.py4e.com/api/films/4/',
'https://swapi.py4e.com/api/films/5/',
'https://swapi.py4e.com/api/films/6/' ],
created: '2014-12-09T13:50:49.641000Z',
edited: '2014-12-20T20:58:18.411000Z',
url: 'https://swapi.py4e.com/api/planets/1/' }
Do you have an 💡?
If you have automation ideas you would like built, reach out to me for a free consultation!
Congratulations! You just made your first API request using Google Apps Script within Google Sheets!
What Else Can You Do?
If you want to play around with this API more check out the documentation to see what other data they are making accessible via API.
There are countless APIs out there that offer all kinds of data and services for free or for a fee. You can explore them on websites like RapidAPI or ProgrammableWeb.
Some examples of APIs you can try are:
- Trivia API: Get random trivia questions and answers.
- NASA API: Get astronomy images, space facts, asteroid