Using the API

Introduction

This document is intended for developers who want to write client libraries, IDE plugins, and other tools for interacting with Google APIs. The Google APIs Discovery Service allows you to do all of the above by exposing machine readable metadata about other Google APIs through a simple API. This guide provides an overview of each section of the Discovery document, as well as helpful tips on how to use the document.

All calls to the API are unauthenticated, JSON-based, REST requests that use SSL, i.e. URLs begin with https.

If you're unfamiliar with Google APIs Discovery Service concepts, you should read Getting Started before starting to code.

Discovery document format

This section gives an overview of the Discovery document.

All the examples below use the Discovery document from the Google Cloud Service Management API. You can load the Discovery document for the Google Cloud Service Management API by executing this GET request:

GET https://servicemanagement.googleapis.com/$discovery/rest?version=v1

The format of a Discovery document includes information that falls into six main categories:

Each of these Discovery document sections is described below. Consult the Reference documentation for more details about each property.

Basic API Description

The Discovery document contains a set of API-specific properties:

"kind": "discovery#restDescription",
"name": "servicemanagement",
"version": "v1",
"title": "Service Management API",
"description": "Google Service Management allows service producers to publish their services on Google Cloud Platform so that they can be discovered and used by service consumers.",
"protocol": "rest",
"rootUrl": "https://servicemanagement.googleapis.com/. Root URL under which all API services live",
"servicePath": "",

These API-level properties include details about a particular version of an API, including the name, version, title and description. The protocol always has a fixed value of rest, as the APIs Discovery Service only supports RESTful methods of accessing the APIs.

The servicePath field indicates the path prefix for this particular API version.

Authentication

The auth section contains details about the OAuth 2.0 auth scopes for the API. To learn more about how to use scopes with OAuth 2.0, visit Using OAuth 2.0 to Access Google APIs.

The auth section contains a nested oauth2 and scopes section. The scopes section is a key/value mapping from the scope value to more details about the scope:

"auth": {
  "oauth2": {
    "scopes": {
      "https://www.googleapis.com/auth/cloud-platform.read-only": {
        "description": "View your data across Google Cloud Platform services"
      },
      "https://www.googleapis.com/auth/service.management.readonly": {
        "description": "View your Google API service configuration"
      },
      "https://www.googleapis.com/auth/cloud-platform": {
        "description": "View and manage your data across Google Cloud Platform services"
      },
      "https://www.googleapis.com/auth/service.management": {
        "description": "Manage your Google API service configuration"
      }
    }
  }
}

The auth section only defines the scopes for a particular API. To learn how these scopes are associated with an API method, consult the Methods section below.

Resources and schemas

An API's operations act on data objects called resources. The Discovery document is built around the concept of resources. Each Discovery document has a top-level resources section that groups all the resources associated with the API. For example, the Google Cloud Service Management API has a services resource and an operations resource under top-level resources, services resource has three sub resources, configs, rollouts and consumers:

"resources": {
  "services": {
    // Methods and sub-resources associated with the services resource
    "configs": {
      // Methods and sub-resources associated with the services.configs resource
    },
    "rollouts": {
      // Methods and sub-resources associated with the services.rollouts resource
    },
    "consumers": {
      // Methods and sub-resources associated with the services.consumers resource
    }
  },
  "operations": {
    // Methods and sub-resources associated with the operations resource
  }
}

Inside each resource section are the methods associated with that resource. For example, the Google Cloud Service Management API has three methods associated with the services.rollouts resource: get, list and create.

Schemas tell you what the resources in an API look like. Each Discovery document has a top-level schemas section, which contains a name/value pair of schema ID to object. Schema IDs are unique per API, and are used to uniquely identify the schema in the methods section of the Discovery document:

"schemas": {
  "Rollout": {
    // JSON Schema of the Rollout resource.
  }
}

APIs Discovery Service uses JSON Schema draft-03 for its schema representations. Here is a snippet of the JSON Schema for the Url resource, along with an actual response resource:

Rollout Resource JSON Schema: Actual Rollout Resource Response:
{
  "Rollout": {
    "id": "Rollout",
    "type": "object",
    "description": "...",
    "properties": {
      "serviceName": {
        "type": "string",
        "description": "..."
      },
      "rolloutId": {
        "type": "string",
        "description": "..."
      },
      "status": {
        "type": "string",
        "enum": [
          "ROLLOUT_STATUS_UNSPECIFIED",
          "IN_PROGRESS",
          "SUCCESS",
          "CANCELLED",
          "FAILED",
          "PENDING",
          "FAILED_ROLLED_BACK"
        ],
        "enumDescriptions": [
          ...
        ],
        "description": "..."
      },
      "trafficPercentStrategy": {
        "$ref": "TrafficPercentStrategy",
        "description": "..."
      },
      "deleteServiceStrategy": { ... },
      "createTime": { ... },
      "createdBy": { ... }
    }
  }
}

{
  "serviceName": "discovery.googleapis.com",
  "rolloutId": "2020-01-01R0",
  "status": "SUCCESS",
  "trafficPercentStrategy":{
    "precentages":{
      "2019-12-01R0": 70.00,
      "2019-11-01R0": 30.00
    }
  }
}

The fields in bold show the mapping between the JSON Schema and the actual response.

Schemas may also contain references to other schemas. If you are building a client library, this can help you effectively model the objects of an API in your data model classes. In the Rollout example above, the trafficPercentStrategy property is actually a reference to a schema with ID TrafficPercentStrategy. If you look at the schemas section, you will find the TrafficPercentStrategy schema. The value of this schema can be substituted for the trafficPercentStrategy property in the Rollout resource (note that the $ref syntax comes from the JSON Schema spec).

Methods may also reference schemas when indicating their request and response bodies. Refer to the Methods section for more details.

Methods

The core of the Discovery document is built around methods. Methods are the operations that can be performed on an API. You will find the methods section in various areas of the Discovery document, including at the top level (which we call API-level methods) or at the resources level.

"methods": {
  // API-level methods
}
"resources": {
  "resource1": {
    "methods": {
      // resource-level methods
    }
    "resources": {
      "nestedResource": {
        "methods": {
          // methods can even be found in nested-resources
        }
      }
    }
  }
}

While an API may have API-level methods, a resource must have a methods section.

Each methods section is a key/value map from method name to other details about that method. The example below documents three methods, get, list and create:

"methods": {
  "get": { //details about the "get" method },
  "list": { //details about the "list" method },
  "create": { //details about the "create" method }
}

Finally, each method's section details various properties about that method. Here is an example of the get method:

"get": {
  "id": "servicemanagement.services.rollouts.get",
  "path": "v1/services/{serviceName}/rollouts/{rolloutId}",
  "flatPath": "v1/services/{serviceName}/rollouts/{rolloutId}",
  "httpMethod": "GET",
  "description": "Gets a service configuration rollout.",
  "response": { "$ref": "Rollout" },
  "parameters": { // parameters related to the method },
  "parameterOrder": [ // parameter order related to the method ],
  "scopes": [ // OAuth 2.0 scopes applicable to this method ],
  "mediaUpload": { // optional media upload information },
},

This section contains general method details such as a unique ID to identify the method, the httpMethod to use, and the path of the method (for details on how to use the path property to calculate the full method url, see the Compose a request section). In addition to these general method properties, there are a few properties that connect the method with other sections in the Discovery document:

Scopes

The auth section defined earlier in this documentation contains information about all the scopes supported by a particular API. If a method supports one of these scopes, it will have a scopes array. There is one entry in this array for each scope supported by the method. For example, the scopes section of the Google Cloud Service Management API get method looks like this:

"scopes": [
  "https://www.googleapis.com/auth/cloud-platform",
  "https://www.googleapis.com/auth/cloud-platform.read-only",
  "https://www.googleapis.com/auth/service.management",
  "https://www.googleapis.com/auth/service.management.readonly"
]

Note that choosing an auth scope to use in your application depends on various factors such as which methods are being called and what parameters are sent along with the method. Therefore, the decision of which scope to use is left to the developer. Discovery only documents which scopes are valid for a method.

Request and response

If the method has a request or response body, these are documented in the request or response sections, respectively. In the get example above, the method has a response body:

"response": {
  "$ref": "Rollout"
}

The syntax above indicates that the response body is defined by a JSON Schema with an ID of Rollout. This schema can be found in the top-level schemas section. Both request and response bodies use the $ref syntax for referring to schemas.

Parameters

If a method has parameters that should be specified by the user, these parameters are documented in the method-level parameters section. This section contains a key/value mapping of the parameter name to more details about that parameter:

"parameters": {
  "serviceName": {
    "type": "string",
    "description": "Required. The name of the service.",
    "required": true,
    "location": "path"
  },
  "rolloutId": { ... }
},
"parameterOrder": [
  "serviceName",
  "rolloutId"
]

In the example above, there are two parameters for the get method:serviceName and rolloutId. Parameters can either go in the path or the URL query; the location property indicates where the client library should put the parameter.

There are many other properties describing the parameter, including the parameter data type (useful for strongly-typed languages), whether the parameter is required, and whether the parameter is an enum. Consult the Reference Guide for more details on the properties.

Parameter order

There are many ways for client libraries to structure their interfaces. One way is to have a method with each API parameter in the method signature. However, since JSON is an unordered format, it's difficult to know programmatically how to order the parameters in the method signature.  The parameterOrder array provides a fixed parameter ordering for making requests. The array lists the name of each parameter in order of significance; it can contain either path or query parameters, but every parameter in the array is required. In the example above, a Java method signature might look something like this:

public Rollout get(String serviceName, String rolloutId, Map<String, Object> optionalParameters);

The first value in the parameterOrder array, serviceName, is also the first element in the method signature. If there were other parameters in the parameterOrder array, they would go after the serviceName parameter.  Since the parameterOrder array only contains the required parameters, it is a good practice to include a way for users to specify optional parameters as well.  The example above does this with the optionalParameters map.

Media upload

If a method supports uploading media, such as images, audio, or video, then the location and protocols supported for uploading that media are documented in the mediaUpload section. This section contains details about which upload protocols are supported, and information about what kinds of media can be uploaded:

"supportsMediaUpload": true,
"mediaUpload": {
  "accept": [
    "image/*"
  ],
  "maxSize": "10MB",
  "protocols": {
    "simple": {
      "multipart": true,
      "path": "/upload/storage/v1beta1/b/{bucket}/o"
    },
    "resumable": {
     "multipart": true,
     "path": "/resumable/upload/storage/v1beta1/b/{bucket}/o"
    }
  }
}

In the example above, the supportsMediaUpload property is a boolean value that determines if the method supports uploading media. If the value is true then the mediaUpload section documents the kinds of media that can be uploaded.

The accept property is a list of media-ranges that determine which mime-types are acceptable to upload. The endpoint shown in the example above will accept any image format.

The maxSize property has the maximum size of an upload. The value is a string in units of MB, GB or TB. In the example above, uploads are limited to a maximum size of 10 MB. Note that this value doesn't reflect an individual user's remaining storage quota for that API, so even if the upload is less than maxSize the client library should still be prepared to handle an upload that fails because of insufficient space.

The protocols section lists the upload protocols that a method supports. The simple protocol is simply POSTing the media to the given endpoint in a single HTTP request. The resumable protocol implies that the endpoint given in the path URI supports the resumable upload protocol. If the multipart property is true then the endpoint accepts multipart uploads, which means both the JSON request and the media can be wrapped together in a mutlipart/related body and sent together. Note that both simple and resumable protocols may support multipart uploads.

The path property is a URI Template and should be expanded just like the path property for the method, as outlined in the Compose a request section.

Media download

If a method supports downloading media, such as images, audio, or video, then that is indicated by the supportsMediaDownload parameter:

"supportsMediaDownload": true,

When downloading media you must set the alt query parameter to media in the request URL.

If the useMediaDownloadService property of the API method is true, insert /download before servicePath to avoid a redirect. For example, the download path is /download/youtube/v3/captions/{id} if the concatenation of servicePath and path is /youtube/v3/captions/{id}. It is recommended to construct media download URL with /download even when useMediaDownloadService is false.

Common parameters

The top-level Discovery document contains a parameters property. This section is similar to the parameters section for each method, however these parameters can be applied to any method in the API.

For example, the get, insert or list methods of the Google Cloud Service Management API can have a prettyPrint parameter in the request parameters, which will format the response for all those methods in a human-readable format. Here's a list of common parameters:

Parameter Meaning Notes Applicability
access_token OAuth 2.0 token for the current user.
alt

Data format for the response.

  • Valid values: json, atom, csv.
  • Default value: varies per API.
  • Not all values are available for every API.
  • Applies to all operations for all resources.
callback Callback function.
  • Name of the JavaScript callback function that handles the response.
  • Used in JavaScript JSON-P requests.
fields Selector specifying a subset of fields to include in the response.
  • For more information, see the partial response documentation.
  • Use for better performance.
key API key. (REQUIRED*)
  • *Required unless you provide an OAuth 2.0 token.
  • Your API key identifies your project and provides you with API access, quota, and reports.
  • Obtain your project's API key from the APIs console.
prettyPrint

Returns response with identations and line breaks.

  • Returns the response in a human-readable format if true.
  • Default value: varies per API.
  • When this is false, it can reduce the response payload size, which might lead to better performance in some environments.
quotaUser Alternative to userIp.
  • Lets you enforce per-user quotas from a server-side application even in cases when the user's IP address is unknown. This can occur, for example, with applications that run cron jobs on App Engine on a user's behalf.
  • You can choose any arbitrary string that uniquely identifies a user, but it is limited to 40 characters.
  • Overrides userIp if both are provided.
  • Learn more about capping usage.
userIp IP address of the end user for whom the API call is being made.
  • Lets you enforce per-user quotas when calling the API from a server-side application.
  • Learn more about capping usage.

Features

There are some cases where APIs support custom features outside the scope of the rest of the Discovery document. These are collected in the features array. The features array contains a string label for each special feature supported by the API. Currently, dataWrapper is the only supported feature, but other features might be supported in the future.

"features": dataWrapper

The dataWrapper feature indicates that all requests to and responses from the API are wrapped in a data JSON object. This is a feature of older Google APIs, but it is being deprecated moving forward. The following APIs support the dataWrapper feature: Moderator v1, and Translate v2.

As a client library developer, if an API supports the "dataWrapper" feature, you should do the following:

  1. On requests: Wrap the request resource in a data JSON object.
  2. On responses: Find the resource inside a data JSON object.

The schemas in the Discovery document do not contain the data wrapper, so you must explicitly add and remove them. For example, suppose there is an API with a resource named "Foo" that looks like this:

{
  "id": 1,
  "foo": "bar"
}

Before inserting this resource using the API, you need to wrap it in a data element:

{
  "data": {
    "id": 1,
    "foo": "bar"
  }
}

On the flip side, when you get a response from the API, it also contains the data wrapper:

{
  "data": {
    "id": 1,
    "foo": "bar"
  }
}

In order to get the resource defined by the schema, you need to remove the data wrapper:

{
  "id": 1,
  "foo": "bar"
}

Inline documentation

Each Discovery document is annotated with a number of description fields that provide inline documentation for the API. The description fields can be found for the following API elements:

  • API itself
  • OAuth scopes
  • Resource schemas
  • API Methods
  • Method parameters
  • Acceptable values for certain parameters

These fields are especially useful if you wish to use the Google APIs Discovery Service to generate human readable documentation for a client library, e.g. JavaDoc.

Next steps