Onjsdev

Share


Google Indexing API With Javascript


By onjsdev

Dec 31st, 2023

The Google Indexing API allows to notify Google when a new page or an updated page is added to a website. Instead of waiting for Google’s bots to crawl and discover new content or submit the content via Google Search Console, this API lets you notify Google about changes.

Let's show how to make HTTP requests to the Google Indexing API with JavaScript and Nodejs.

Install Packages

To make HTTP requests to the Google Indexing API with JavaScript and Nodejs, you will need to install the following packages:

  • googleapis – provides an interface to Google APIs.
  • axios – allow to make HTTP requests

You can install these packages using the following command:

npm i googleapis axios

Authenticate with the API

To authenticate with the Google Indexing API, you will need to create a JWT (JSON Web Token) token using your credentials. You can do this with the following code:

const axios = require("axios");
const { google } = require("googleapis");
const key = require("./path/to/your/credentials.json");

const client = new google.auth.JWT(
  key.client_email,
  null,
  key.private_key,
  ["https://www.googleapis.com/auth/indexing"],
  null
);

Send A Request For Indexing A Page

Once you have authenticated with the Google Indexing API, you can use the access token to submit a page for indexing or deleting existing page from search.

Google provides two types:

  • URL_UPDATED
  • URL_DELETED

You can notify google to update your URL this with the following code:

async function main() {
  try {
    const tokens = await client.authorize();
    const options = {
      url: "https://indexing.googleapis.com/v3/urlNotifications:publish",
      method: "POST",
      headers: {
        "Content-Type": "application/json",
        Authorization: `Bearer ${tokens.access_token}`,
      },
      data: {
        url: `path/to/url/updated`, // https://ontechmap.com/new-content
        type: "URL_UPDATED",
      },
    };

    const response = await axios(options);
    console.log(response.data);
  } catch (error) {
    console.error(error.message);
  }
}

main();

After sending a request here is a message returned with success code:

{
  urlNotificationMetadata: {
    url: 'path/to/url/updated', // https://ontechmap.com/new-content
    latestUpdate: {
      url: 'path/to/url/updated',
      type: 'URL_UPDATED',
      notifyTime: '2023-04-27T12:24:22.811755980Z'
    }
  }
}

Send A Request For Removing A Page

You can also notify google to remove your URLs from search page sending a HTTP request with the type URL_DELETED. While sending a request, make sure your URL return 404 or 410 status code for completing request without problem.

Here is how to send request to google index API for removing a specfic URL

async function main() {
  try {
    const tokens = await jwtClient.authorize();
    const options = {
      url: "https://indexing.googleapis.com/v3/urlNotifications:publish",
      method: "POST",
      headers: {
        "Content-Type": "application/json",
        Authorization: `Bearer ${tokens.access_token}`,
      },
      data: {
        url: `path/to/url/deleted`, // https://ontechmap.com/deleted-content
        type: "URL_DELETED",
      },
    };

    const response = await axios(options);
    console.log(response.data);
  } catch (error) {
    console.error(error.message);
  }
}

main();

Get Notification Status Of Indexing

With the Indexing API, you can verify the latest timestamp for each type of notification that Google has received for a specific URL. It’s important to note that the information retrieved through a GET request using this API won’t inform you about when Google indexes or removes a URL. Instead, it only confirms whether your request was successfully submitted.

For example,

async function main() {
  try {
    const tokens = await client.authorize();
    const options = {
      url: "https://indexing.googleapis.com/v3/urlNotifications/metadata?url=https%3A%2F%2Fexample.com%2Farticle%2Fhow-to-trigger-query-in-postgresql",
      method: "GET",
      headers: {
        Authorization: `Bearer ${tokens.access_token}`,
      },
    };

    const response = await axios(options);
    console.log(response.data);
  } catch (error) {
    console.error(error.message);
  }
}

main();

As you may notice here, Google accepts encoded URLs, which requires using %3F for the character : and %2F for the character /.

For example, if you want to learn the indexing status of the URL https://example.com/post , you should send the URL as encoded as shown below:

https://example.com/post -> https%3F%2F%2Fexample.com%2Fpost

And here is a response with success code 200:

{
  url: 'https://example.com/post',
  latestUpdate: {
    url: 'https://example.com/post',
    type: 'URL_UPDATED',
    notifyTime: '2023-04-27T12:24:22.811755980Z'
  }
}

Conclusion

Google Indexing API is a useful API that allows webmasters their URLS to add to index or remove the search page. If you have a large scale website and you want to send google your URL for indexing without using sitemap or index request from search console, you can easily use the google indexing API with javascript and nodejs.