Introduction to Google Search Console API with C#

Written by Nick Swan. Published on 28, October 2021

This is the first article in a whole series that will show you how to write code and use the Google Search Console API to help you with your day to day SEO tasks. We’ll be replicating some of the reports in SEOTesting.com so you can run them yourself, and improve or add to them as you wish.

This introduction article will take you through how to set up access to the Google Search Console API via the Google Cloud Platform, and build your first request to get some query data from Google Search Console.

Introduction to using C# and the Google Search Console API

Python is currently the language of choice for lots of SEOs looking to get into programming and data science, but it’s not the only choice.

I’ve been programming with C# since 2004 and so it’s my go to choice when needing to code something. All of SEOTesting.com is developed using C#, ASP.NET MVC as the web app, and Microsoft SQL Server for data storage.

While Visual Studio and the tools needed to write code in C# used to be expensive and only usable on Windows, Microsoft has released VSCode for free which enables cross platform development for Windows, Mac and Linux.

There are three main benefits of using C# that keep me coming back to it:

  1. Statically typed - the code editor will show you errors as you write it, rather than having to wait until you run it.
  2. Intellisense - it’s like auto complete. The code editor will help you write the code rather than you having to memorise it.
  3. Debugging - walking through code to figure out that data is returned and how data structures hang together.

These three things help me be more productive, and learn new libraries and APIs more quickly.

Set up Google Cloud Platform and GSC API access

Register (or log in) to Google Cloud Platform.

Screenshot of Google Cloud Platform homepage

GCP is the mechanism for managing authentication and authorization to the various APIs that Google supports.

Once you are in GCP we are going to want to create a new Project.

How to create a new project in Google Cloud Platform

Give it a sensible name such as GSCAccess.

Once the project is created, click into it, and from the left hand menu click on ‘IAM & Admin’, and then 'Service Accounts'.

Click on ‘+ Create Service Account’

Give the service account a meaningful name. You’ll notice GCP adds a generated section so the service account has an email address. This is the ‘email address’ of the service account that you’ll add to GSC to get access. Copy the email address for use later

Ignore the two ‘Optional’ steps (2 and 3) they’re not needed, click ‘Done’.

It’s really important to be careful who you share the name of this account with, or the JSON credentials file. Whoever has access to these two things will potentially be able to access the same GSC data that you will be accessing.

Once you create the service account, and you are back on the service account screen, click on the service account itself and you’ll see a menu item along the top for ‘Keys’.

Click ‘Add Key’ and then ‘Create new key’.

Leave the type selected as JSON and click CREATE.

Upon the creation of the Key file, it’ll automatically be downloaded to your computer (to the Downloads folder if you are on Windows). We’ll need this file shortly so keep it safe!

Enable the Google Search Console API

From the side menu, click into APIs & Services -> Library.

Search for 'search console', click the result

Click 'Enable' button

Give Search Console access to the GCP Service Account

Go into Google Search Console and select the GSC property you wish to work with.

Go Settings -> Users

Give the service account created in GCP Restricted Permission to the GSC property you want to get data from.

C# code to access Google Search Console API

Download VSCode - it's free! Runs on Mac or Windows.

Open VSCode

Terminal window in Visual Studio Code

Using the terminal, make a directory to work from within by typing the following code and pressing enter

mkdir gsc

Then move into the directory with the following command:

cd mkdir

Create a new dot net core console app:

dotnet new console

Add the Google Search Console library to your project:

dotnet add package google.apis.webmasters.v3

open the gsc folder in the Explorer by typing:

code . -r

into the terminal window and pressing enter.

You’ll get a pop up in the bottom right corner asking you to add assets required to build a C# project, click Yes for this:

Visual Studio Code pop up

Drag the service account key json file from Windows Explorer or Finder into the VS code Explorer to add it to the project.

In the Explorer, open Program.cs - this is the main file where you will do your coding today.

At the top of the file, below “using System;”, paste the following:

using System.Collections.Generic;
using System.IO;
using System.Text;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
using Google.Apis.Webmasters.v3;
using Google.Apis.Webmasters.v3.Data;

Copy the following code below, and paste it in to replace the line Console.WriteLine(“Hello World!”);

using System.Collections.Generic;
            var credentialsPath = "./service-key-filename.json";
            var searchConsoleUrl = "sc-domain:yourdomain.com";

            var stream = new FileStream(credentialsPath, FileMode.Open);

            var credentials = GoogleCredential.FromStream(stream);

            if (credentials.IsCreateScopedRequired)
            {
                credentials = credentials.CreateScoped(new string[] { WebmastersService.Scope.Webmasters });
            }

            var service = new WebmastersService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credentials,
                ApplicationName = "Console App"
            });

            DateTime startDate = DateTime.Now.AddDays(-501);
            DateTime endDate = DateTime.Now;

            List dimensionList = new List();
            dimensionList.Add("query");
                        
            var request = new SearchAnalyticsQueryRequest();

            request.StartDate = startDate.ToString("yyyy-MM-dd");
            request.EndDate = endDate.ToString("yyyy-MM-dd");
            request.Dimensions = dimensionList;

            var response = service.Searchanalytics.Query(request, searchConsoleUrl).Execute();

            StringBuilder sb = new StringBuilder();

            sb.AppendLine("Query,Clicks,Impressions,CTR,AvgPosition");

            if(response.Rows != null)
            {
                foreach(var row in response.Rows)
                {
                    sb.Append(row.Keys[0]);
                    sb.Append(",");
                    sb.Append(row.Clicks);
                    sb.Append(",");
                    sb.Append(row.Impressions);
                    sb.Append(",");
                    sb.Append(row.Ctr);
                    sb.Append(",");
                    sb.Append(row.Position);
                    sb.AppendLine();
                }
            }

            File.WriteAllText("queries.csv", sb.ToString());

You will need to replace the two variable at the top with your own values. Firstly the name of the JSON key file you downloaded from Google Cloud Platform, and secondly the Search Console property name you want to query.

When you are ready to run the code, press F5 and VS Code will compile and run it for you.

Once it has completed running you’ll see a new file called queries.csv, which will contain all the queries from the past (roughly) 16 months from Google Search Console.

csv queries file in Visual Studio Code

Pretty cool huh!

Getting access to Google Discover data for urls was just released this week, so if you want to be able to export that data:

1, Change dimensionList.Add(“query”) to dimensionList.Add(“page”).

2, Add in the following code once the request object has been created:


            request.AggregationType = "byPage";
            request.SearchType = "discover";

3, You may also want to change Query to Url in the line sb.AppendLine("Query,Clicks,Impressions,CTR,AvgPosition"); so your CSV file has the correct header.

4, Finally, change the name of the CSV file to DiscoverUrls.csv

Run the code again by pressing F5, and you'll now have an export of the discover urls and respective data.

Conclusion

Hopefully you have that running and a CSV file full of Google Search Console data! In the next blog post we'll look at doing something more interesting with it. If you have any issues getting the above steps to work, feel free to reach out on Twitter.