Google PageSpeed Insights on Command Line (CLI)
Welcome back to more in adventures in Command Line! Previously, we were looking at running Google Lighthouse programmatically. Today, we’re going to explore a similar exercise- running Google PageSpeed Insights on Windows Command Line (CLI)! Running PageSpeed insights on Command Line was probably the easiest self-coding experience I’ve had to-date. (More on the hard part […]
Welcome back to more in adventures in Command Line! Previously, we were looking at running Google Lighthouse programmatically.
Today, we’re going to explore a similar exercise- running Google PageSpeed Insights on Windows Command Line (CLI)! Running PageSpeed insights on Command Line was probably the easiest self-coding experience I’ve had to-date. (More on the hard part later. 🙂 )
3 Prep Steps for Running PageSpeed Insights on Command Line
Unlike Screaming Frog Command Line automation, there are very few parameters or options to mess with for running data on a single URL. To run a single URL on PageSpeed Insights from Command Line:
- Ensure CURL works in your command line (test CURL https://www.sampledomainhere.com) and check that the HTML source code is returned
- Most recent Windows machines should already have CURL ready out of the box for CLI usage
- Visit the Google PageSpeed Insights API documentation page
- Get an API key (free) and create a project for credentials (also free) if needed
- Copy the code sample into a code editor or text editor
Running Your First URL on PageSpeed Insights CLI
Is CLI for PageSpeed insights easy? Yes!
Effortless? No.
Here’s the out-of-the-box code sample from Google:
curl https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url=https://developers.google.com
If you try to run that out of the box on Windows, you’ll like run into one or more of these errors:
- 429 error: Too many requests / exhausted
- Invalid syntax: Command(s) not recognized
First, you’ll need to append your API key to the end of the URL.
curl https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url=https://www.zldoty.com&key=yourAPIKeyHere
Here’s what did the trick for me: placing escape characters in front of any special operators. Specifically, the ampersand (&) and equals (=) signs have special functions in CMD that need to be escaped with a carrot (^).
curl https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url^=https://www.zldoty.com^&key^=yourAPIKeyHere
Great, CLI Works! What’s next?
If everything works, you should see a flurry of JSON code scroll through the command prompt window – hooray! But here’s the rub, what do you with this information?
Some extra code will help us capture the output into a file:
curl https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url^=https://www.zldoty.com^&key^=yourAPIKeyHere -o fileName.json
Adding -o for output and specifying a file name will populate a table in CLI during processing with download progress stats. After the file has downloaded (sample runs took about 18 seconds), you should be able to see it in the default CLI directory in Windows File Explorer. (Typically C:\Users\youUserName)
Stay tuned for some more updates on how to scale this process across multiple URLs and extracting insights from the JSON file!