Google Mobile-Friendly Test API Tutorial for Python
This post explains how to use Google’s Mobile-Friendly Test API with Python. The Mobile-Friendly Test indicates whether a page is mobile-friendly, reports resources blocked to Google, and returns a screenshot of the page.
Because the Mobile-Friendly Test uses the Googlebot Smartphone user agent, it not only checks mobile-friendliness but also shows how Google views a page and whether any resources are blocked.
Using the Mobile-Friendly Test API with Python makes it practical to analyze many pages in bulk and verify how Googlebot sees them. If you change your site’s layout, theme, or how pages are served to Googlebot, the API lets you quickly check many pages to ensure Googlebot renders them as expected by examining blocked resources and screenshots.

Table of Contents
Requirements and Assumptions
- Python 3 is installed and basic Python syntax is understood.
- A Google API key has been set up.
- Google Search Console service is enabled.
Import Modules
This tutorial uses three Python modules to interact with the Mobile-Friendly Test API:
- requests – to make POST requests to the Mobile-Friendly Test API endpoint.
- json – to parse and navigate the JSON response.
- base64 – to decode screenshots returned encoded in base64.
Install the requests module if needed:
pip install requests
With that installed, let’s see how the API works.
Making the request to the API endpoint
The endpoint is https://searchconsole.googleapis.com/v1/urlTestingTools/mobileFriendlyTest:run. It accepts the following parameters:
- URL: the URL to test.
- requestScreenshot: whether to return a screenshot. Default is false; set to “true” to request a screenshot.
- key: the Google API key set up previously.
url = 'https://searchconsole.googleapis.com/v1/urlTestingTools/mobileFriendlyTest:run'
params = {
'url': 'https://www.danielherediamejias.com',
'requestScreenshot': 'true',
'key': "yourkey"
}
x = requests.post(url, data = params)
data = json.loads(x.text)
The code above sends a request to the Mobile-Friendly Test API for https://www.danielherediamejias.com. The response is parsed using json.loads().
Which information can we get now?
- Response Code: verify the response code is 200; other codes may indicate Googlebot Smartphone had trouble crawling the page.
- Status: whether the audit completed.
- Mobile friendliness verdict: if the page is mobile friendly the API returns “MOBILE_FRIENDLY”. If there are mobile usability issues, the API lists them.
- Blocked resources: if any resources are blocked, confirm these do not prevent Googlebot Smartphone from rendering the page correctly.
- Screenshot: you can obtain a screenshot showing how Googlebot Smartphone sees the page.
The keys or expressions used to access these fields are:
- Response code: str(x)[len(str(x))-5:len(str(x))-2]
- Status: data[“testStatus”][“status”]
- Mobile Friendliness: data[“mobileFriendliness”]. If there are mobile-friendliness issues, get the list of issues with:
listmobileissues = []
for iteration in range (len(data["mobileFriendlyIssues"])):
listmobileissues.append(data["mobileFriendlyIssues"][iteration]["rule"])
- Blocked Resources List:
listblockedresources = [] for iteration in range (len(data["resourceIssues"])): listblockedresources.append(data["resourceIssues"][iteration]["blockedResource"]["url"])
- Screenshot: data[“screenshot”][“data”]
Putting everything together
Combining the pieces, the script looks like this:
import requests
import json
import base64
try:
url = 'https://searchconsole.googleapis.com/v1/urlTestingTools/mobileFriendlyTest:run'
params = {
'url': 'yoururl',
'requestScreenshot': 'true',
'key': "yourkey"
}
x = requests.post(url, data = params)
data = json.loads(x.text)
with open("Finalscreenshot.png", "wb") as fh:
fh.write(base64.b64decode(data["screenshot"]["data"]))
print("Response code for Google Smartphone is " + str(x)[len(str(x))-5:len(str(x))-2])
print("Page is " + data["mobileFriendliness"])
if data["mobileFriendliness"] == "NOT_MOBILE_FRIENDLY":
for iteration in range (len(data["mobileFriendlyIssues"])):
print("The page has problems with " + str(data["mobileFriendlyIssues"][iteration]["rule"]))
issues = data.get("resourceIssues", 0)
if issues != 0:
for iteration in range (len(data["resourceIssues"])):
print("Problems with the blocked resources " + str(data["resourceIssues"][iteration]["blockedResource"]["url"]))
print("Screenshot from " + str(params["url"]) + " is taken and downloaded.")
except:
print("Problem with " + str(params["url"]) + ". " + str(x)[len(str(x))-5:len(str(x))-2] + " Response Code.")
The output from this script looks like the example below:

The script also downloads the screenshot as a PNG. For this post, the screenshot returned was:

If the page has issues, the script prints the response code. Running the script against a non-mobile-friendly page might produce output like this:

That’s all — I hope you found this post useful!
Google Mobile Friendly Test API FAQ
- Google Mobile-Friendly Test API Tutorial for Python - October 5, 2020














