Getting data from any API

Let's learn about the HTTP GET request to retrieve data from any API using Rows.

You’ve got the knowledge and Rows tools to authenticate your requests. But, you’re probably wondering how to get data from an API. Stick with us, as in this lesson we’ll talk about:

  1. Some more basic terms in API documentation
  2. How to use Rows’ GET() function to request data

Some more basic terms in APIs

Again, we’ll need to just quickly make sure that we’re all on the same page regarding some terms ;)

Base URL
The Base URL (also called the Request URL) is the URL that you need to use to access the API. Look for these terms in the API docs.

Endpoint
The Endpoint is where the data that you want to interact with is located. You add it to the base URL after a forward slash /.

Query
This is what you want to get. After the endpoint, you’ll see a ? followed by a lot of key-value pairs, joined with &.

Now, all these three things together create what in Rows we refer to as the action_url:

kb-adv-URL-example

How to know which endpoint you need to access?
Well, that’s up to you 😅. Endpoints are usually grouped according to the data that they interact with and what you can do with it.

If you have a look at the Spotify API:

kb-spotify-endpoints-example

You’ll see that they group them by data (Playlists), and then provide you with the base URL, along with the endpoints (along with a description with what you can do). So for example, if you wanted to get all the playlists in your account you’d just need to send your request to this URL (with some authentication, of course):

https://api.spotify.com/v1/me/playlists

But if you look at how to get a specific playlist, you'll see some curly brackets {}? These mean that you need to provide some parameters to the URL. For example, if you wanted to get all the tracks for a playlist:

https://api.spotify.com/v1/playlists/{playlist_id}/tracks

Then you’d need to find out the Spotify ID of the playlist and replace the {} brackets with that ID. For example:

https://api.spotify.com/v1/playlists/00981234jsadf920lu/tracks

So, now you have all the knowledge you need to start getting data from APIs! Let’s try some out in Rows.

Getting data

You’ll never guess what function you need to use in Rows to get data from any API… it’s called GET()!

kb-function-get

And you already know what you need to put in for the action_url and headers parameters.

Hint

In action_url you would have the base URL, the endpoint you want to use, and possibly your authentication.

In headers you'd put your authentication (if needed).

Let’s put this all into practice by getting some weather data with OpenWeatherAPI.

OpenWeatherAPI Key

You’ll need to create an account and get an API key - it won’t take you longer than two minutes. Or, of course, you can use the API of your choice.

We’ll want to get the current weather for a location, say, Lisbon, Portugal.

  1. Go to OpenWeatherMap’s API documentation.
  2. In Call current weather data for one location, we can see that we need to add a city name and the API key to the query string to get the current weather.

    API documentation screenshot

    kb-openweather-example

  3. In Rows, set up a spreadsheet so that it looks like this:

kb-advanced-weather-newUI

  1. Fill in your spreadsheet with the following info:
CellContent
A2Your API Key
B2Lisbon,Portugal (Or any city,country combination you’d like)

In cell C2, we'll create the action_url with Base URL from the documentation while also using cell references to make it dynamic:

kb-open-weather-example

  1. In cell D2, write =GET(C2).

❇️ Boom! Now you'll see a {data} cell - open the Data Explorer and look at the results!

kb-advanced-weather-data-newUI

Try playing around with a few other cities and countries 😁.

Up next

OK, so you learned some theory as well as successfully used an API call in Rows to get weather data! That’s pretty amazing, right?

So, what we need to do now is learn a little about how to send data to APIs in our next article. Are you ready? Let’s go!