DEV Community

Suman Chatterjee
Suman Chatterjee

Posted on

Accessing the Google Discovery API and its Associated Discovery Documents

The Google API Discovery Service is a crucial component for developers integrating with Google's API ecosystem. It serves as a dynamic catalog, providing:

  • Machine-readable specifications (Discovery Documents) for API integration.
  • Automation for generating client libraries and tools.
  • Detailed API specifications for scalable integration.

Discovery Documents, formatted in JSON, describe how to access APIs via RESTful HTTP calls, including:

  • Data structures (schemas) and operations (methods).
  • Authentication scopes.
  • Inline documentation.

Google uses these documents for their client libraries, ensuring reliability, while adhering to the JSON Schema standard.


Accessing the Directory of Google APIs

To discover supported APIs, developers can retrieve a directory via the following endpoint:

HTTP GET Request:

https://discovery.googleapis.com/discovery/v1/apis

The response includes metadata about each API, such as:

  • ID (name:version)
  • Title, description, and discoveryRestUrl (discoveryLink)
  • API icons and documentation links.

Example JSON Structure:

{
  "kind": "discovery#directoryList",
  "discoveryVersion": "v1",
  "items": [
    {
      "id": "urlshortener:v1",
      "name": "urlshortener",
      "version": "v1",
      "title": "Google URL Shortener API",
      "description": "The Google URL Shortener API lets you create...",
      "discoveryRestUrl": "https://urlshortener.googleapis.com/$discovery/rest?version=v1",
      "icons": {
        "x16": "icon_url_16",
        "x32": "icon_url_32"
      },
      "preferred": true
    }
  ]
}
Enter fullscreen mode Exit fullscreen mode

Optional Parameters:

  • name: Filters results by API name.
  • preferred: Returns only the preferred API version if set to true.

Table of Key Fields in the Response:

Field Name Description Example Value
kind Response type. discovery#directoryList
discoveryVersion Version of the Discovery API used for the response. v1
items API directory entries array. [...]
items.id API ID (name:version). urlshortener:v1
items.name API name. urlshortener
items.version API version. v1
items.title API title. "Google URL Shortener API"
items.description API description. "The Google URL Shortener API lets you create..."
items.discoveryRestUrl RESTful Discovery Document's URL. https://urlshortener.googleapis.com/...
items.icons Links to API icons. { "x16": "icon_url_16", "x32": "icon_url_32"}
items.documentationLink Human-readable API documentation. http://code.google.com/...
items.labels API status labels (e.g., limited availability). []
items.preferred Indicates if it's the preferred API version. true or false

Retrieving Discovery Documents for Specific APIs

After identifying a desired API and version, Discovery Documents can be accessed as follows:

1. Directly Documented URLs

Certain APIs provide Discovery URLs in their documentation. For example:

  • Google People API: https://people.googleapis.com/$discovery/rest?version=v1

2. Constructing Default URLs

Default URLs follow this format:

https://www.googleapis.com/discovery/v1/apis/API_NAME/API_VERSION/rest

Example:

  • Google Translate API v2: https://www.googleapis.com/discovery/v1/apis/translate/v2/rest

3. API-Specific Documentation

Official documentation can reliably provide Discovery URLs, ensuring up-to-date and accurate APIs as they evolve.


Programmatic Access Using Python

Developers can use the googleapiclient or requests library to programmatically fetch Discovery Documents.

Using googleapiclient

Install the library:

pip install google-api-python-client

Fetch the API service object:

from googleapiclient.discovery import build

service = build('translate', 'v2')
# API methods can now be accessed via 'service'
Enter fullscreen mode Exit fullscreen mode

To fetch a non-default URL, use the discoveryServiceUrl parameter. Alternatively, use build_from_document with the document content (JSON).

Using requests

Manually fetch and parse a Discovery Document:

import requests

discovery_url = 'https://www.googleapis.com/discovery/v1/apis/translate/v2/rest'
response = requests.get(discovery_url)

discovery_document = response.json()
# 'discovery_document' holds the parsed JSON content
Enter fullscreen mode Exit fullscreen mode

Alternative Methods and Tools

  • Google APIs Explorer: A web-based interface for exploring and testing APIs interactively.

Best Practices and Considerations

Caching

Cache Discovery Documents locally to improve performance, respecting the Discovery Service's HTTP caching headers.

Security

Handle API keys and OAuth 2.0 credentials securely to prevent unauthorized access.

Using Client Libraries

Libraries like google-api-python-client simplify authentication and API interaction while reducing boilerplate code.


Accessing the Google API Discovery API facilitates efficient integration with Google's services. By leveraging Discovery Documents and available tools, developers can construct robust, scalable, and integrated applications.

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay