Skip to main contentSkip to footer

Google Search Console API: 25 Python Scripts to Automate SEO Analysis

Google Search Console API with Python, ready to run

25 copy-ready Python scripts that pull data from the Search Console API and turn it into SEO answers: striking-distance keywords, cannibalization, content decay, indexing checks and automated alerts. Set up once, then paste any script below.

Set up the API in 5 steps

  1. Create a Google Cloud project. Open console.cloud.google.com and create a project, or pick an existing one.
  2. Enable the Search Console API. Go to APIs & Services, then Library, search for “Google Search Console API” and click Enable.
  3. Create a service account and a JSON key. Under IAM & Admin, then Service Accounts, create one, open it, add a key of type JSON and save it as key.json next to your scripts.
  4. Add the service account to your property. In Search Console open Settings, then Users and permissions, and add the service account email with Full or Restricted access.
  5. Install the libraries. Run the command below, then paste the shared setup block into every script file.

Install command

pip install google-api-python-client google-auth pandas matplotlib requests

Shared setup block (paste at the top of every script)

Authenticates, defines the date helper d(), the paginated query helper q() and the period comparison helper compare(). Change SITE to your property: sc-domain:example.com for a domain property or https://example.com/ for a URL-prefix property.

Good to know: Search Console data arrives with a delay of about two days, so every script ends at d(3). Each API request returns at most 25,000 rows, and the q() helper pages through results for you.

The 25 scripts

Rules that save you time

  • Start with the shared setup block and add one script at a time. Each script prints a table or saves a CSV, so you can inspect results before automating.
  • The API has no row limit of 1,000 like the web interface. Use q() with a high limit to export everything.
  • Percentages are returned as fractions, so a CTR of 0.035 means 3.5%.
  • The URL Inspection API allows about 2,000 requests per day per property, so batch your URLs.
  • Never commit key.json to a public repository. Keep it outside your project folder or load it from an environment variable.
  • Schedule any script with cron, GitHub Actions or Cloud Scheduler to turn a one-off check into a weekly report.

Frequently asked questions

Is the Google Search Console API free?

Yes. The API is free to use within Google’s default quotas, which are enough for most sites running daily or weekly scripts.

How far back does the API return data?

Up to 16 months, the same window as the Performance report in Search Console.

Why does my script return a 403 error?

The service account email has not been added as a user on the property, or SITE does not match the property type exactly. Domain properties need the sc-domain: prefix.

Why do totals differ from the Search Console interface?

Google anonymizes rare queries. Totals grouped by query can be lower than totals grouped by date or page, in both the interface and the API.

Can I use these scripts with Google Colab?

Yes. Upload key.json to the Colab session, run pip install in a cell, then paste the setup block and any script.

You might also like

More Similar Posts