Creating and managing API keys

This page explains how to create and manage API keys using the API Keys API.

For information on how to use an API key with your calls to Google Cloud APIs, see Using API keys.

Creating an API key

You can create an API key by using the CreateKey method. The method requires a Key parameter. You can only specify displayName and restrictions fields of the Key object. The CreateKey isn't a synchronous method. Instead, when you issue a call to CreateKey, you initiate a long-running operation. The following example issues a CreateKey call to create an API key with no restrictions:

curl -X POST \
     -H "Authorization: Bearer $(gcloud auth print-access-token)" \
     -H "Content-Type: application/json; charset=utf-8" \
     -d '{
          "displayName" : "Example API key"
        }' \
     'https://apikeys.googleapis.com/v2/projects/PROJECT_NUMBER/locations/global/keys'

On success, the method returns a long-running operation in the response. As described in Polling long running operations, you repeatedly make operations.get calls with the value from the name field. When the response from operations.get contains "done": true, the response object contains a Key, similar to the following:

{
  "name": "operations/akmf.p7-103621867718-06f94db2-7e91-4c58-b826-e6b80e4dc3eb",
  "done": true,
  "response": {
    "@type": "type.googleapis.com/google.api.apikeys.v2.Key",
    "name": "projects/PROJECT_NUMBER/locations/global/keys/aecd7943-98ff-4ce2-a876-ec1b37c671ca",
    "displayName": "Example API key",
    "keyString": "----REDACTED----",
    "createTime":