Getting started with API Keys API

This page describes how to set up your local environment to experiment with API Keys using the curl command.

Before you begin

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. Install the Google Cloud CLI.

  3. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity .

  4. To initialize the gcloud CLI, run the following command:

    gcloud  
    init
  5. Create or select a Google Cloud project .

    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID 
      

      Replace PROJECT_ID with a name for the Google Cloud project you are creating.

    • Select the Google Cloud project that you created:

      gcloud config set project PROJECT_ID 
      

      Replace PROJECT_ID with your Google Cloud project name.

  6. Verify that billing is enabled for your Google Cloud project .

  7. Enable the API Keys API:

    gcloud  
    services  
     enable 
      
    apikeys.googleapis.com
  8. Grant roles to your user account. Run the following command once for each of the following IAM roles: roles/serviceusage.apiKeysAdmin

    gcloud  
    projects  
    add-iam-policy-binding  
     PROJECT_ID 
      
    --member = 
     "user: USER_IDENTIFIER 
    " 
      
    --role = 
     ROLE 
    

    Replace the following:

    • PROJECT_ID : your project ID.
    • USER_IDENTIFIER : the identifier for your user account—for example, myemail@example.com .
    • ROLE : the IAM role that you grant to your user account.
  9. Install the Google Cloud CLI.

  10. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity .

  11. To initialize the gcloud CLI, run the following command:

    gcloud  
    init
  12. Create or select a Google Cloud project .

    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID 
      

      Replace PROJECT_ID with a name for the Google Cloud project you are creating.

    • Select the Google Cloud project that you created:

      gcloud config set project PROJECT_ID 
      

      Replace PROJECT_ID with your Google Cloud project name.

  13. Verify that billing is enabled for your Google Cloud project .

  14. Enable the API Keys API:

    gcloud  
    services  
     enable 
      
    apikeys.googleapis.com
  15. Grant roles to your user account. Run the following command once for each of the following IAM roles: roles/serviceusage.apiKeysAdmin

    gcloud  
    projects  
    add-iam-policy-binding  
     PROJECT_ID 
      
    --member = 
     "user: USER_IDENTIFIER 
    " 
      
    --role = 
     ROLE 
    

    Replace the following:

    • PROJECT_ID : your project ID.
    • USER_IDENTIFIER : the identifier for your user account—for example, myemail@example.com .
    • ROLE : the IAM role that you grant to your user account.

Testing the API Keys API

  1. Get the project number for the project in which you enabled API Keys.

  2. Define a convenient shell alias for calling the API:

     alias gcurl='curl -H "Authorization: Bearer $(gcloud auth print-access-token)" -H "Content-Type: application/json"' 
    
  3. Make a request to get a list of API keys in your project:

    gcurl https://apikeys.googleapis.com/v2/projects/ YOUR_PROJECT_NUMBER 
    /locations/global/keys

    On success, you get a response similar to the following:

     { 
      
     "keys" 
     : 
      
     [ 
      
     { 
      
     "name" 
     : 
      
     "projects/12345678/locations/global/keys/2885bf87-5b84-47fa-92af-08c3e9337349" 
     , 
      
     "displayName" 
     : 
      
     "API key 2" 
     , 
      
     "createTime" 
     : 
      
     "2019-05-29T22:07:22.036Z" 
     , 
      
     "uid" 
     : 
      
     "2885bf87-5b84-47fa-92af-08c3e9337349" 
     , 
      
     "updateTime" 
     : 
      
     "2019-05-29T22:07:22.058623Z" 
     , 
      
     "restrictions" 
     : 
      
     { 
      
     "androidKeyRestrictions" 
     : 
      
     {} 
      
     }, 
      
     "etag" 
     : 
      
     "zHib8eXEMCxe4ayQEbIaZg==" 
      
     }, 
      
     { 
      
     "name" 
     : 
      
     "projects/12345678/locations/global/keys/a4db08b7-5729-4ba9-8c08-f2df493465a1" 
     , 
      
     "displayName" 
     : 
      
     "API key 1" 
     , 
      
     "createTime" 
     : 
      
     "2019-05-29T22:06:58.844Z" 
     , 
      
     "uid" 
     : 
      
     "a4db08b7-5729-4ba9-8c08-f2df493465a1" 
     , 
      
     "updateTime" 
     : 
      
     "2019-05-29T22:06:58.855103Z" 
     , 
      
     "restrictions" 
     : 
      
     { 
      
     "androidKeyRestrictions" 
     : 
      
     {} 
      
     }, 
      
     "etag" 
     : 
      
     "0L5KcPMGoNi53K5+FqPxiw==" 
      
     } 
      
     ] 
     } 
    

What's next

Create a Mobile Website
View Site in Mobile | Classic
Share by: