Stay organized with collections
Save and categorize content based on your preferences.
This page describes how to set up your local environment to experiment
with API Keys using the curl command.
Before you begin
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.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-09-04 UTC."],[[["\u003cp\u003eThis page guides you through setting up a local environment for experimenting with API Keys using the \u003ccode\u003ecurl\u003c/code\u003e command.\u003c/p\u003e\n"],["\u003cp\u003eYou'll need to obtain your project number where API Keys are enabled to proceed.\u003c/p\u003e\n"],["\u003cp\u003eA shell alias named \u003ccode\u003egcurl\u003c/code\u003e is defined to simplify API calls for ease of use.\u003c/p\u003e\n"],["\u003cp\u003eYou will learn how to make a request to get a list of all API keys in your project, followed by a successful response example.\u003c/p\u003e\n"],["\u003cp\u003eThe page provides links to further actions, such as creating, managing, restricting, or troubleshooting API Keys.\u003c/p\u003e\n"]]],[],null,["# Getting started with API Keys API\n\nThis page describes how to set up your local environment to experiment\nwith API Keys using the `curl` command.\n\nBefore you begin\n----------------\n\n- Sign in to your Google Cloud account. If you're new to Google Cloud, [create an account](https://console.cloud.google.com/freetrial) to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.\n-\n [Install](/sdk/docs/install) the Google Cloud CLI.\n\n- If you're using an external identity provider (IdP), you must first\n [sign in to the gcloud CLI with your federated identity](/iam/docs/workforce-log-in-gcloud).\n\n-\n To [initialize](/sdk/docs/initializing) the gcloud CLI, run the following command:\n\n ```bash\n gcloud init\n ```\n- [Create or select a Google Cloud project](https://cloud.google.com/resource-manager/docs/creating-managing-projects).\n\n | **Note**: If you don't plan to keep the resources that you create in this procedure, create a project instead of selecting an existing project. After you finish these steps, you can delete the project, removing all resources associated with the project.\n - Create a Google Cloud project:\n\n ```\n gcloud projects create PROJECT_ID\n ```\n\n Replace \u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e with a name for the Google Cloud project you are creating.\n - Select the Google Cloud project that you created:\n\n ```\n gcloud config set project PROJECT_ID\n ```\n\n Replace \u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e with your Google Cloud project name.\n-\n [Verify that billing is enabled for your Google Cloud project](/billing/docs/how-to/verify-billing-enabled#confirm_billing_is_enabled_on_a_project).\n\n-\n\n\n Enable the API Keys API:\n\n\n ```bash\n gcloud services enable apikeys.googleapis.com\n ```\n-\n Grant roles to your user account. Run the following command once for each of the following\n IAM roles:\n `roles/serviceusage.apiKeysAdmin`\n\n ```bash\n gcloud projects add-iam-policy-binding PROJECT_ID --member=\"user:\u003cvar translate=\"no\"\u003eUSER_IDENTIFIER\u003c/var\u003e\" --role=ROLE\n ```\n\n Replace the following:\n - \u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e: your project ID.\n - \u003cvar translate=\"no\"\u003eUSER_IDENTIFIER\u003c/var\u003e: the identifier for your user account---for example, `myemail@example.com`.\n - \u003cvar translate=\"no\"\u003eROLE\u003c/var\u003e: the IAM role that you grant to your user account.\n\n-\n [Install](/sdk/docs/install) the Google Cloud CLI.\n\n- If you're using an external identity provider (IdP), you must first\n [sign in to the gcloud CLI with your federated identity](/iam/docs/workforce-log-in-gcloud).\n\n-\n To [initialize](/sdk/docs/initializing) the gcloud CLI, run the following command:\n\n ```bash\n gcloud init\n ```\n- [Create or select a Google Cloud project](https://cloud.google.com/resource-manager/docs/creating-managing-projects).\n\n | **Note**: If you don't plan to keep the resources that you create in this procedure, create a project instead of selecting an existing project. After you finish these steps, you can delete the project, removing all resources associated with the project.\n - Create a Google Cloud project:\n\n ```\n gcloud projects create PROJECT_ID\n ```\n\n Replace \u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e with a name for the Google Cloud project you are creating.\n - Select the Google Cloud project that you created:\n\n ```\n gcloud config set project PROJECT_ID\n ```\n\n Replace \u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e with your Google Cloud project name.\n-\n [Verify that billing is enabled for your Google Cloud project](/billing/docs/how-to/verify-billing-enabled#confirm_billing_is_enabled_on_a_project).\n\n-\n\n\n Enable the API Keys API:\n\n\n ```bash\n gcloud services enable apikeys.googleapis.com\n ```\n-\n Grant roles to your user account. Run the following command once for each of the following\n IAM roles:\n `roles/serviceusage.apiKeysAdmin`\n\n ```bash\n gcloud projects add-iam-policy-binding PROJECT_ID --member=\"user:\u003cvar translate=\"no\"\u003eUSER_IDENTIFIER\u003c/var\u003e\" --role=ROLE\n ```\n\n Replace the following:\n - \u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e: your project ID.\n - \u003cvar translate=\"no\"\u003eUSER_IDENTIFIER\u003c/var\u003e: the identifier for your user account---for example, `myemail@example.com`.\n - \u003cvar translate=\"no\"\u003eROLE\u003c/var\u003e: the IAM role that you grant to your user account.\n\n\u003cbr /\u003e\n\nTesting the API Keys API\n------------------------\n\n1. Get the [project number](/resource-manager/docs/creating-managing-projects#identifying_projects)\n for the project in which you enabled API Keys.\n\n2. Define a convenient shell alias for calling the API:\n\n alias gcurl='curl -H \"Authorization: Bearer $(gcloud auth print-access-token)\" -H \"Content-Type: application/json\"'\n\n3. Make a request to get a list of API keys in your project:\n\n ```\n gcurl https://apikeys.googleapis.com/v2/projects/YOUR_PROJECT_NUMBER/locations/global/keys\n ```\n\n On success, you get a response similar to the following: \n\n ```json\n {\n \"keys\": [\n {\n \"name\": \"projects/12345678/locations/global/keys/2885bf87-5b84-47fa-92af-08c3e9337349\",\n \"displayName\": \"API key 2\",\n \"createTime\": \"2019-05-29T22:07:22.036Z\",\n \"uid\": \"2885bf87-5b84-47fa-92af-08c3e9337349\",\n \"updateTime\": \"2019-05-29T22:07:22.058623Z\",\n \"restrictions\": {\n \"androidKeyRestrictions\": {}\n },\n \"etag\": \"zHib8eXEMCxe4ayQEbIaZg==\"\n },\n {\n \"name\": \"projects/12345678/locations/global/keys/a4db08b7-5729-4ba9-8c08-f2df493465a1\",\n \"displayName\": \"API key 1\",\n \"createTime\": \"2019-05-29T22:06:58.844Z\",\n \"uid\": \"a4db08b7-5729-4ba9-8c08-f2df493465a1\",\n \"updateTime\": \"2019-05-29T22:06:58.855103Z\",\n \"restrictions\": {\n \"androidKeyRestrictions\": {}\n },\n \"etag\": \"0L5KcPMGoNi53K5+FqPxiw==\"\n }\n ]\n }\n ```\n\nWhat's next\n-----------\n\n- [Creating and managing API keys](/api-keys/docs/create-manage-api-keys)\n- [Getting information about API keys](/api-keys/docs/get-info-api-keys)\n- [Adding restrictions to API keys](/api-keys/docs/add-restrictions-api-keys)\n- [Troubleshooting](/api-keys/docs/troubleshooting)"]]