Instant Index URLs With Google Indexing API | Python Code

You already know one way to index your URLs in Google i.e; Manually submit indexing requests to Google through the search console.

But there are two problems with that kind of indexing.

First, URLs take a long time to get indexed.

Second, it is difficult to index URLs in bulk.

Google’s indexing API helps us solve these problems. Let’s get started with it and see how you can automate your indexing requests to Google for faster and efficient indexing.

Visit https://console.cloud.google.com/ for your API dashboard. You will have a dashboard which will look something like this.

Click on create project.

Click enable APIS and services.

Search indexing API

Click enable.

Click create credentials

For which API are you using? Set it to indexing API

Next check the Application data radio button.

And for Are you planning to use this API…. set it to No!

Click next

Set any name for the service account name and skip the description. Click create and continue.

Set role to owner

And click done!

Copy the email under service accounts. We need to add this email as an owner in the particular search console property you are trying to index URL for.

For that, head over to your search console dashboard and go to settings tab

Click on Users and Permissions.

Now click on Add User

And add that email as the owner to your property.

Now we need to take a few more steps.

Click on the edit pencil on the right side of the email.

Go to the keys tab.

Click add key and create new key.

Select JSON and click create. A file will be downloaded into your system. 

Head over to https://colab.research.google.com/ 

Click the new notebook and paste the following code.

from oauth2client.service_account import ServiceAccountCredentials
import httplib2

SCOPES = [ "https://www.googleapis.com/auth/indexing" ]
ENDPOINT = "https://indexing.googleapis.com/v3/ur..."

# service_account_file.json is the private key that you created for your service account.
JSON_KEY_FILE = "your-file.json"

credentials = ServiceAccountCredentials.from_json_keyfile_name(JSON_KEY_FILE, scopes=SCOPES)

http = credentials.authorize(httplib2.Http())


content = """{
  "url": "your-url",
  "type": "URL_UPDATED"
}"""

response, content = http.request(ENDPOINT, method="POST", body=content)
print(response)
print(content)


Click the little upload icon on the left side and select the JSON file you downloaded.

Replace your-url with the actual URL that you are trying to index. Also, replace “your-file.json” with the path of the JSON file you downloaded.

If you receive response status code 200 then your URL has successfully been requested to be indexed and it will usually take less than an hour to do. 

That was it!

Leave a Reply

Your email address will not be published.

one + 11 =