API Connector Documentation
Import GTmetrix Data to Google Sheets
In this guide, we’ll walk through how to pull data from the GTmetrix API directly into Google Sheets, using the API Connector add-on for Sheets.
Contents
- Before You Begin
- Part 1: Get Your GTmetrix API Key
- Part 2: Pull GTmetrix API Data into Sheets
- Part 3: API Documentation
Before You Begin
Click here to install the API Connector add-on from the Google Marketplace.
Part 1: Get Your GTmetrix API Key
- If you haven't already, create an account at https://gtmetrix.com/ and log in
- Click Account from the dropdown Account menu at the top (or navigate directly to https://gtmetrix.com/dashboard/account)
- On the account page, scroll down and click Generate API Key
- Your key will now be displayed. Copy this to your clipboard as we'll use it right away.
- One last step: Because the GMetrix API requires Basic Authentication, we need to encode our API key to base 64. You can do this by entering your API key into this form.
The encoding script runs in your browser, and none of your credentials are seen or stored by this site.
Part 2: Pull GTmetrix API Data into Sheets
GTmetrix uses a credit system, where different API reports require different numbers of credits. You can run a request to any of the endpoints shown in the documentation, but for this example, we'll fetch a Lighthouse page speed report, which costs 1 credit.
Substitute in your own encoded API key where it says your_encoded_key
, and modify the request body as needed using the test parameters provided here.
- Open up Google Sheets and click Extensions > API Connector > Open > Create request.
- In the request form enter the following to set up the test:
- Application:
Custom
- Method:
POST
- Request URL:
https://gtmetrix.com/api/2.0/tests
- Headers:
Authorization
:Basic
your_encoded_key
Content-Type
:application/vnd.api+json
- Request body:
{"data":{"type":"test","attributes":{"url":"https://www.mixedanalytics.com","location":"1","browser":"3","adblock":1,"report":"lighthouse"}}}
- Application:
- Create a new tab and click Set current to use that tab as your data destination.
- Name your request and click Run. A moment later you’ll see response metadata and the report status populate your sheet. This means the report is now in the queue waiting to be run.
- Pay special attention to the value in the
links.self
field as this is the link to the finalized report. We'll create a new request that plugs this URL into the request URL field. Note that it may take a few minutes for this report to be ready.- Application:
Custom
- Method:
GET
- Request URL:
https://gtmetrix.com/api/2.0/tests/ZCNe4eOA
- Headers:
Authorization
:Basic
your_encoded_key
- Application:
- You should now see your Lightspeed report results.
- To automate this further, you may want to reference the cell containing the
links.self
URL, instead of entering it directly. This is convenient as it allows you to dynamically fetch new data without manually updating your request.
Part 3: API Documentation
Official API documentation: https://gtmetrix.com/api/docs/2.0/
Location IDs
id | free? | region | name |
1 | TRUE | North America | Vancouver, Canada |
2 | TRUE | Europe | London, UK |
3 | TRUE | Asia Pacific | Sydney, Australia |
4 | TRUE | North America | San Antonio, TX, USA |
5 | TRUE | Asia Pacific | Mumbai, India |
6 | TRUE | Latin America | São Paulo, Brazil |
7 | TRUE | Asia Pacific | Hong Kong, China |
8 | FALSE | North America | Quebec City, Canada |
9 | FALSE | North America | San Francisco, CA, USA |
10 | FALSE | North America | Cheyenne, WY, USA |
11 | FALSE | North America | Chicago, IL, USA |
12 | FALSE | North America | Danville, VA, USA |
13 | FALSE | Europe | Amsterdam, Netherlands |
14 | FALSE | Europe | Paris, France |
15 | FALSE | Europe | Frankfurt, Germany |
16 | FALSE | Europe | Stockholm, Sweden |
17 | FALSE | Asia Pacific | Singapore |
18 | FALSE | Asia Pacific | Tokyo, Japan |
19 | FALSE | Asia Pacific | Busan, South Korea |
20 | FALSE | Asia Pacific | Chennai, India |
21 | FALSE | Africa | Johannesburg, South Africa |
22 | FALSE | Middle East | Dubai, UAE |
Browser IDs
id | browser | name |
3 | chrome | Chrome (Desktop) |
1 | firefox | Firefox (Desktop) |
Thank you very much. This was great, well explained, it worked very well.
I'm now trying to optimize my spreadsheet referencing cells and keeping a data history of each API run.
Unfortunately I can't find a way I could run your addon a second time appending a new row and referencing it dinamicly for each new future run.
And I couldn't find a way to automaticly download the PDF report.
Hey Rafael, if I've understood what you're saying correctly, I believe you could use the following process:
1) Day 1: Run the test setup request using the
https://gtmetrix.com/api/2.0/tests
endpoint2) Day 1: Fetch the results using the
https://gtmetrix.com/api/2.0/tests/+++Inputs!A1+++
endpoint, referencing the cell containinglinks.self
. Tick the "Add timestamp" option so you can track when it ran (under Output options).3) Day 2: Run the test setup request again
4) Day 2: Run the data fetch request again, but this time edit (and save) it to run in Append mode. Now the results and timestamp will print out underneath the first test results.
5) Day 3 and on as the same as Day 2. Basically only the initial run should be in Overwrite mode, following that all requests should run in Append mode.
API Connector doesn't have any PDF download functionality, but let me know if that answers your question about appending data.