Once complete, your service account key is downloaded to your browser's
default location.
Next, decide whether you'll provide your service account authentication as a
bearer token or using application default credentials.
Bearer tokens
If you're calling the Vision API directly, such as by making an HTTP request
with cURL, you'll pass your authentication as a bearer token in an
Authorization header. To obtain a bearer token using your service account:
If you're using a client library to call the Vision API, use
Application Default Credentials (ADC).
Services using ADC look for credentials within a GOOGLE_APPLICATION_CREDENTIALS
environment variable. Unless you specifically wish to have ADC use other
credentials (for example, user credentials), we recommend you set this
environment variable to point to your service account key file.
Replace PATH_TO_KEY_FILE with the path to your JSON service account file.
GOOGLE_APPLICATION_CREDENTIALS should be written out as-is (it's not a
placeholder in the example above).
Using an API key
You can use a Google Cloud console API key to authenticate to the Vision API.
[[["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-08-28 UTC."],[],[],null,["# Authenticating to the Cloud Vision API\n\nUsing a service account\n-----------------------\n\nUsing a service account to authenticate is the preferred method. To use a\nservice account to authenticate to the Vision API:\n\n1. Follow the instructions to [create a service account](/iam/docs/creating-managing-service-accounts#creating_a_service_account). Select **JSON** as your key type.\n\nOnce complete, your service account key is downloaded to your browser's\ndefault location.\n\nNext, decide whether you'll provide your service account authentication as a\n**bearer token** or using **application default credentials**.\n\n### Bearer tokens\n\nIf you're calling the Vision API directly, such as by making an HTTP request\nwith cURL, you'll pass your authentication as a bearer token in an\n`Authorization` header. To obtain a bearer token using your service account:\n\n1. [Install the gcloud CLI](/sdk/downloads#interactive).\n2. Authenticate to your service account, replacing `KEY_FILE` below with the\n path to your service account key file:\n\n gcloud auth activate-service-account --key-file KEY_FILE\n\n3. Obtain an authorization token using your service account:\n\n gcloud auth print-access-token\n\n The command returns an access token value.\n4. When calling the API, obtain an authorization token using your service\n account in an `Authorization` header:\n\n curl -s -H \"Authorization: Bearer $(gcloud auth application-default print-access-token)\" \\\n -H \"Content-Type: application/json\" \\\n \"https://vision.googleapis.com/v1/images:annotate\" -d @request.json\n\n### Application default credentials\n\nIf you're using a client library to call the Vision API, use\n[Application Default Credentials](https://developers.google.com/identity/protocols/application-default-credentials) (ADC).\nServices using ADC look for credentials within a `GOOGLE_APPLICATION_CREDENTIALS`\nenvironment variable. Unless you specifically wish to have ADC use other\ncredentials (for example, user credentials), we recommend you set this\nenvironment variable to point to your service account key file. \n\n```\nexport GOOGLE_APPLICATION_CREDENTIALS=PATH_TO_KEY_FILE\n```\n\nReplace `PATH_TO_KEY_FILE` with the path to your JSON service account file.\n`GOOGLE_APPLICATION_CREDENTIALS` should be written out as-is (it's not a\nplaceholder in the example above).\n\nUsing an API key\n----------------\n\nYou can use a Google Cloud console API key to authenticate to the Vision API.\n\nTo do so:\n\n1. Follow the instructions to\n [create an API key for your Google Cloud console project](https://support.google.com/cloud/answer/6158862?hl=en).\n\n2. When making any Vision API request, pass your key as the value of a `key`\n parameter. For example:\n\n POST https://vision.googleapis.com/v1/images:annotate?key=YOUR_API_KEY\n\nMake sure to read\n[Best practices for securely using API keys](https://support.google.com/cloud/answer/6310037?hl=en&ref_topic=6262490)\nto prevent unauthorized use of your API key."]]