IMPORTAPI() Custom Sheets 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:
- Why Use ImportAPI()?
- ImportAPI() Syntax
- Basic Example
- Example with URL Parameter
- Example with URL Parameter and Fast Cell-Based Refresh
- Example Referencing a Cell
- Example Referencing Multiple Cells
- Usage Notes & Caveats
- 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, and
- get a faster potential data refresh rate than you can achieve through scheduling.
IMPORTAPI()function. If you need to schedule or stack requests, please use the standard sidebar method.
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
IMPORTAPI()function 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.
If you’ve saved an API request called Binance, you can now reference this request with the following custom function:
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:
This example function will allow you to refresh your data every time the value in cell $A$1 changes.
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). Just 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).
- 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.
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 REFERENCING 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.
USAGE NOTES & CAVEATS
- 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 JMESPath filtering, 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.
- 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.
- 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 and producing an error message like “Service invoked too many times for one day: urlfetch”. 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: https://stackoverflow.com/questions/63431445/how-to-make-a-custom-function-not-recalculate-in-google-sheet)
- 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 need reliable, scheduled reporting, please use the scheduling feature.