You can use Rows to set a GET
request to any API and return the data to a cell. If you're not sure what a GET
request is, check out Getting Data from any API first, otherwise, read on!
GET request examples:
To understand how GET
requests work in Rows, let's first look at a few examples.
GET with no authentication
Here's an example of a simple formula that sends a GET request to the Datamuse API to get all words that rhyme with "rows".=GET("https://api.datamuse.com/words?rel_rhy=data")
In this example, the API doesn't require any authentication but, in reality, most APIs require you to provide some authentication details (which serve the same purpose as a user name and password).
For this reason, The
GET
function takes a second optional parameter where you can specifiy the content of the header which is how authentication details are often provided.
GET with bearer authentication
Here's an example of a GET request with Bearer Authentication:=GET(https://api.twitter.com/2/users/by/username/rowshq,
'{"Authorization": "Bearer AAAAAAAAAAAAAcE3F..."}')
- The authentication details are specified as JSON which is a little tiresome to enter by hand. You can use the supplementary
PAIR2JSON
formula which converts one or more value pairs into JSON for you.
GET with bearer authentication using PAIR2JSON
Here's an example of a GET request with Bearer Authentication that uses the PAIR2JSON function:=GET(https://api.twitter.com/2/users/by/username/rowshq,
PAIR2JSON("Authorization","Bearer AAAAAAAAAAAAAcE3F..."))
- The
PAIR2JSON
function has taken the two string arguments:Authorization
andBearer AAAAAAAAAAAAAcE3F..."
... and converted them into the required JSON format from the first example:{"Authorization": "Bearer AAAAAAAAAAAAAcE3F..."}
GET with bearer authentication using PAIR2JSON and CONCAT
You coulld also put the bearer token in a cell such as A1
and concatenate it with the "Bearer " prefix it like this:=GET(https://api.twitter.com/2/users/by/username/rowshq,
PAIR2JSON("Authorization:",CONCAT("Bearer ",A1))
- For further information the different authentication methods, see Authenticating your API calls.
Creating a GET request:
The easiest way to create a GET
request is through the function wizard which guides you through the options for the GET
function. You can access this wizard from the autocomplete menu when you start to type a formula.
You can also access the function wizard from the main toolbar.
To create a GET request with the function wizard, follow these steps:
- Select a cell where you want to create the
GET
request. - Click the Function Wizard button in the main toolbar.
- In the search box, search for
GET
and click the result. In the options panel, configure the options for the
GET
function.Option Use To Action URL The URL of the API that you want to interact with.
For example, the action URL of Twitter's user information API ishttps://api.twitter.com/2/users/by/username/rowshq
Set headers A JSON object that contains headers that you want to send along with the API request .
For example,{"content-type": "application/json"}
You can use thePAIR2JSON
function to create these headers from cell values. For more information about what headers do, take a look at the Postman documentation.Set response type Set the format of the response that you want to receive from the API.
</li> 1 —Only the numeric status code without any content (such as200
or401
).
</li> 2 — The full JSON of the API reponse which includes the content, the status code, and the response headers.</li></ul>- Click Save Changes in Cell... to apply the
GET
formula.
Your cell should now contain the value{data}
which indicates that the GET function has received some kind of response from the API. - (Optional) Create a data table from the content of the response so you can review the data in your spreadsheet.