IMPORTAPI Custom Function
The API Connector add-on for Google Sheets activates a custom function called
IMPORTAPI. This function works similarly to Google Sheets functions like
IMPORTJSON, etc. It enables you to call your APIs (in JSON, XML, or CSV format) from directly within a Google Sheets cell, instead of the standard method of requesting APIs via the add-on sidebar.
Example using the IMPORTAPI function with the Genderize API to predict gender for a list of names:
- Before You Begin
- Why Use ImportAPI?
- Comparison: Standard Sidebar vs. ImportAPI
- ImportAPI Syntax
- Example 1: Basic Request
- Example 2: With URL Parameter
- Example 3: Fast Cell-Based Refresh
- Example 4: Reference a Cell
- Example 5: Reference Multiple Cells
- Error Messages
- Usage Notes
BEFORE YOU BEGIN
If you haven’t already, click here to install the API Connector add-on from the Google Marketplace.
WHY USE THE IMPORTAPI FUNCTION?
- copy down an API request to run it against a list of input cells,
- run requests by changing cells instead of going through the sidebar,
- choose a specific cell location for your API response, and
- get a faster potential data refresh rate than you can achieve through scheduling.
IMPORTAPIfunction. If you need to schedule or stack requests, please use the standard sidebar method.
COMPARISON: IMPORTAPI FUNCTION VS. SIDEBAR
Either way, begin by configuring and saving your request in the API Connector sidebar. You can then choose whether to run your API requests through that sidebar or through your sheet using the IMPORTAPI custom function. The following table identifies the key differences between the two methods.
|Run API requests||✓||✓|
|Include headers, JMESPath, and other options||✓||✓|
|Reference cells in requests||✓||✓|
|Schedule data refreshes at hourly, daily, or monthly intervals||✓|
|Run requests while the sheet is closed||✓|
|Process API requests for up to 6 minutes per execution||✓|
|Process API requests for up to 30 seconds only||✓|
|Trigger requests by clicking a checkbox or changing cells||✓|
|Refresh data every few minutes||✓|
|Quick, dynamic, a bit hack-y 🙂 and sometimes error-prone 🙁||✓|
In general, use the standard sidebar for regular, reliable, scheduled reporting. ImportAPI is designed for faster, dynamic API pulls that you use while active in your sheet. You may encounter errors when you use ImportAPI; check the section on Error Messages for details.
The syntax for this feature is
=IMPORTAPI(requestName, url, cell).
requestNameRequired. Refers to the API request name you’ve saved within API Connector. By referencing the request name, the
IMPORTAPIfunction will automatically make an API call that includes that request name’s headers, report style, and other saved options.
urlOptional. Allows you to specify a request URL. If you don’t include this argument in your function, your request will use the request URL from your saved request.
cellOptional. Refers to any cell in your sheet, e.g. $A$1. Google Sheets will refresh data whenever a function changes, so you can force API refreshes by changing the value in $A$1. This needs to be the third argument, so if you apply it without including the optional URL in your function, you should add an empty string into the second position, e.g.
EXAMPLE 1: BASIC REQUEST
If you’ve saved an API request called Binance, you can now run this request with the following custom function:
EXAMPLE 2: WITH URL PARAMETER
You can include a request URL parameter, so the request will use this URL instead of the URL in your saved Binance request. All other settings will be retained:
EXAMPLE 3: FAST CELL-BASED REFRESH
This example function will allow you to refresh your data every time the value in cell $A$1 changes.
If you want to use the same URL from your saved API request, you can leave that parameter out, like this:
Tips for refreshing data
- Fast cell-based refresh: Populate cell $A$1 using one of Google’s automatically-updating functions, e.g.
=GOOGLEFINANCE("CURRENCY:USDEUR"), to force your API request to refresh every time the Google Finance function runs (about every 2 minutes).
Please beware of Google’s quotas and limits as your data will stop refreshing if you run too many requests. It is highly recommended that you wrap any automatically updating IMPORTAPI calls in a SWITCH() or IF() statement to prevent excess recalculation (info). For example, you could make a function like this:
=if(B1="on", IMPORTAPI("Binance"),"switched off"), so the function only runs when you enter the text “on” in cell B1.
- Checkbox-based refresh: Populate cell $A$1 with a checkbox (Insert > Checkbox) so you can update your sheet each time you check or uncheck the box.
EXAMPLE 4: REFERENCE A CELL
Referencing a cell enables you to base your API calls on changing URL inputs. For example, you can enter BTCUSDT into cell A1, and then construct your custom function like this:
Now, every time you edit cell A1 with a new currency pair, your data will automatically refresh.
EXAMPLE 5: REFERENCE MULTIPLE CELLS
Like shown in the first GIF, there are some situations where you may want to “zip” through a list of names or values and perform an API call on each one. Let’s use the genderize API as an example:
In order to accomplish this stacked API output, you will need to go to Output options and then Output Mode. Choose the Append radio button (this removes the headers).
Setup your API call in the adjacent cell and just drag it down to run it for each respective cell. Bam!
Note that “Append” mode does not return the column names, so you will need to manually enter them yourself.
ImportAPI is a quick and convenient way to run dynamic API requests while you are active in the sheet, but is subject to a number of limitations. If you are unable to resolve the following errors, please run your requests through the sidebar instead of through the ImportAPI custom function.
#NAME? along with Unknown function: ‘ImportAPI’
This error occurs because, in some cases, Google doesn’t recognize the IMPORTAPI custom function at all. This is a bug on Google’s side associated with custom functions (info). As of now there is no reliable fix for the above issue, but here are a few things you can try:
- Re-open the sidebar and run your saved request. This often makes Google recognize the custom function.
- based on this this comment, please try clicking Add-ons > API Connector > Help > View in Store. After “View in store”, click Settings (gear icon top-right) > Manage Apps > Options (3 vertical dots) > Toggle “Use in this document”. After toggling off and on it may work again.
- Another option is to copy your requests into a new sheet, it often works in one sheet but not another.
- Refresh your sheet, or wait and try again later.
Internal error executing the custom function
This error comes from Google, and means that your custom function has taken too long (more than 30 seconds) to execute (documentation).
To address, reduce the size of your request or run your request through the sidebar rather than through IMPORTAPI.
Just another Google bug, where the sheet gets stuck with a “Loading…” error. See here and here for relevant discussions. Generally the problem occurs when updating a lot of cells at once. You can usually fill in the missing values by changing the inputs of affected cells one by one to force a recalculation.
Pagination is only available for paid users (when you are a paid user).
Google runs custom functions under the Sheet owner’s account, regardless of who is actually running the function. This means, if you’re combining IMPORTAPI with pro features like pagination or JMESPath, you might receive an authorization error if you’re not the sheet owner. To resolve, you will need to transfer sheet ownership to yourself, or make a copy of the sheet where you are the owner.
Request failed, cannot find function split in object true
This error occurs when you are missing a middle argument in your function. Please make sure you’ve entered your function like this:
Service invoked too many times for one day: urlfetch
Google runs custom functions when the sheet is opened and when the inputs to the function have changed. If you have a large number of IMPORTAPI calls, it’s possible to quickly rack up thousands of requests, thus hitting Google’s 20k/day limit. If you are experiencing this issue, move requests to the standard sidebar wherever possible, and try wrapping IMPORTAPI calls in a SWITCH() or IF() statement to prevent recalculation (source)
You do not have permission to call setValues
This error sometimes occurs when you wrap your IMPORTAPI in other formulas, such that the result would expand outside the immediate cell. As discussed here, the documentation says “if a custom function returns a double array, the results overflow the cell containing the function and fill the cells below and to the right of the cell containing the custom function“. Custom functions are not permitted to set values in adjacent cells.
- If you’d like to avoid printing out header keys from the API response, switch to Append mode (under Output options).
- Google allows custom functions to run for only 30 seconds, while sidebar requests can run for up to 6 minutes (documentation). Therefore, IMPORTAPI is not generally suitable for very large or time-consuming requests.
- Standard API Connector daily request limits do NOT apply, this feature is free and unlimited.