Credentials class for calling Google APIs using an API key.
Uses an API key directly in the request metadata to provide authorization.
Note: ApiKeyCredentials extends from base Credentials class rather than
GoogleCredentials/OAuth2Credentials, as it does not provide an access token and is not considered
an OAuth2 credential.
Credentialscredentials=ApiKeyCredentials.create("your api key");
A constant string name describing the authentication technology.
E.g. “OAuth2”, “SSL”. For use by the transport layer to determine whether it supports the
type of authentication in the case where Credentials#hasRequestMetadataOnly is false.
Also serves as a debugging helper.
Get the current request metadata in a blocking manner, refreshing tokens if required.
This should be called by the transport layer on each request, and the data should be
populated in headers or other context. The operation can block and fail to complete and may do
things such as refreshing access tokens.
The convention for handling binary data is for the key in the returned map to end with
"-bin" and for the corresponding values to be base64 encoded.
Indicates whether or not the Auth mechanism works purely by including request metadata.
This is meant for the transport layer. If this is true a transport does not need to take
actions other than including the request metadata. If this is false, a transport must
specifically know about the authentication technology to support it, and should fail to accept
the credentials otherwise.
[[["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,["# Class ApiKeyCredentials (1.38.0)\n\nVersion latestkeyboard_arrow_down\n\n- [1.38.0 (latest)](/java/docs/reference/google-auth-library/latest/com.google.auth.ApiKeyCredentials)\n- [1.37.1](/java/docs/reference/google-auth-library/1.37.1/com.google.auth.ApiKeyCredentials)\n- [1.36.0](/java/docs/reference/google-auth-library/1.36.0/com.google.auth.ApiKeyCredentials)\n- [1.35.0](/java/docs/reference/google-auth-library/1.35.0/com.google.auth.ApiKeyCredentials)\n- [1.34.0](/java/docs/reference/google-auth-library/1.34.0/com.google.auth.ApiKeyCredentials)\n- [1.33.1](/java/docs/reference/google-auth-library/1.33.1/com.google.auth.ApiKeyCredentials)\n- [1.32.1](/java/docs/reference/google-auth-library/1.32.1/com.google.auth.ApiKeyCredentials)\n- [1.31.0](/java/docs/reference/google-auth-library/1.31.0/com.google.auth.ApiKeyCredentials)\n- [1.30.1](/java/docs/reference/google-auth-library/1.30.1/com.google.auth.ApiKeyCredentials)\n- [1.29.1-SNAPSHOT](/java/docs/reference/google-auth-library/1.29.1-SNAPSHOT/com.google.auth.ApiKeyCredentials)\n- [1.28.0](/java/docs/reference/google-auth-library/1.28.0/com.google.auth.ApiKeyCredentials)\n- [1.27.0](/java/docs/reference/google-auth-library/1.27.0/com.google.auth.ApiKeyCredentials)\n- [1.26.0](/java/docs/reference/google-auth-library/1.26.0/com.google.auth.ApiKeyCredentials)\n- [1.25.0](/java/docs/reference/google-auth-library/1.25.0/com.google.auth.ApiKeyCredentials)\n- [1.24.1](/java/docs/reference/google-auth-library/1.24.1/com.google.auth.ApiKeyCredentials)\n- [1.20.0](/java/docs/reference/google-auth-library/1.20.0/com.google.auth.ApiKeyCredentials)\n- [1.19.0](/java/docs/reference/google-auth-library/1.19.0/com.google.auth.ApiKeyCredentials)\n- [1.18.0](/java/docs/reference/google-auth-library/1.18.0/com.google.auth.ApiKeyCredentials)\n- [1.7.0](/java/docs/reference/google-auth-library/1.7.0/com.google.auth.ApiKeyCredentials)\n- [1.6.1](/java/docs/reference/google-auth-library/1.6.1/com.google.auth.ApiKeyCredentials)\n- [1.4.0](/java/docs/reference/google-auth-library/1.4.0/com.google.auth.ApiKeyCredentials)\n- [1.3.0](/java/docs/reference/google-auth-library/1.3.0/com.google.auth.ApiKeyCredentials) \n\n public class ApiKeyCredentials extends Credentials\n\nCredentials class for calling Google APIs using an API key.\n\nUses an API key directly in the request metadata to provide authorization.\n\nNote: ApiKeyCredentials extends from base [Credentials](/java/docs/reference/google-auth-library/latest/com.google.auth.Credentials) class rather than\nGoogleCredentials/OAuth2Credentials, as it does not provide an access token and is not considered\nan OAuth2 credential. \n\n\n Credentials credentials = ApiKeyCredentials.create(\"your api key\");\n \nInheritance\n-----------\n\n[java.lang.Object](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html) \\\u003e [Credentials](/java/docs/reference/google-auth-library/latest/com.google.auth.Credentials) \\\u003e ApiKeyCredentials \n\nInherited Members\n-----------------\n\n[Credentials.blockingGetToCallback(URI,RequestMetadataCallback)](/java/docs/reference/google-auth-library/latest/com.google.auth.Credentials#com_google_auth_Credentials_blockingGetToCallback_java_net_URI_com_google_auth_RequestMetadataCallback_) \n[Credentials.getAuthenticationType()](/java/docs/reference/google-auth-library/latest/com.google.auth.Credentials#com_google_auth_Credentials_getAuthenticationType__) \n[Credentials.getMetricsCredentialType()](/java/docs/reference/google-auth-library/latest/com.google.auth.Credentials#com_google_auth_Credentials_getMetricsCredentialType__) \n[Credentials.getRequestMetadata()](/java/docs/reference/google-auth-library/latest/com.google.auth.Credentials#com_google_auth_Credentials_getRequestMetadata__) \n[Credentials.getRequestMetadata(URI)](/java/docs/reference/google-auth-library/latest/com.google.auth.Credentials#com_google_auth_Credentials_getRequestMetadata_java_net_URI_) \n[Credentials.getRequestMetadata(URI,Executor,RequestMetadataCallback)](/java/docs/reference/google-auth-library/latest/com.google.auth.Credentials#com_google_auth_Credentials_getRequestMetadata_java_net_URI_java_util_concurrent_Executor_com_google_auth_RequestMetadataCallback_) \n[Credentials.getUniverseDomain()](/java/docs/reference/google-auth-library/latest/com.google.auth.Credentials#com_google_auth_Credentials_getUniverseDomain__) \n[Credentials.hasRequestMetadata()](/java/docs/reference/google-auth-library/latest/com.google.auth.Credentials#com_google_auth_Credentials_hasRequestMetadata__) \n[Credentials.hasRequestMetadataOnly()](/java/docs/reference/google-auth-library/latest/com.google.auth.Credentials#com_google_auth_Credentials_hasRequestMetadataOnly__) \n[Credentials.refresh()](/java/docs/reference/google-auth-library/latest/com.google.auth.Credentials#com_google_auth_Credentials_refresh__) \n[Object.clone()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#clone--) \n[Object.equals(Object)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#equals-java.lang.Object-) \n[Object.finalize()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#finalize--) \n[Object.getClass()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#getClass--) \n[Object.hashCode()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#hashCode--) \n[Object.notify()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#notify--) \n[Object.notifyAll()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#notifyAll--) \n[Object.toString()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#toString--) \n[Object.wait()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait--) \n[Object.wait(long)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait-long-) \n[Object.wait(long,int)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait-long-int-)\n\nStatic Methods\n--------------\n\n### create(String apiKey)\n\n public static ApiKeyCredentials create(String apiKey)\n\nMethods\n-------\n\n### getAuthenticationType()\n\n public String getAuthenticationType()\n\nA constant string name describing the authentication technology.\n\nE.g. \"OAuth2\", \"SSL\". For use by the transport layer to determine whether it supports the\ntype of authentication in the case where [Credentials#hasRequestMetadataOnly](/java/docs/reference/google-auth-library/latest/com.google.auth.Credentials#com_google_auth_Credentials_hasRequestMetadataOnly_) is false.\nAlso serves as a debugging helper.\n\n**Overrides** \n[Credentials.getAuthenticationType()](/java/docs/reference/google-auth-library/latest/com.google.auth.Credentials#com_google_auth_Credentials_getAuthenticationType__)\n\n### getRequestMetadata(URI uri)\n\n public Map\u003cString,List\u003cString\u003e\u003e getRequestMetadata(URI uri)\n\nGet the current request metadata in a blocking manner, refreshing tokens if required.\n\nThis should be called by the transport layer on each request, and the data should be\npopulated in headers or other context. The operation can block and fail to complete and may do\nthings such as refreshing access tokens.\n\nThe convention for handling binary data is for the key in the returned map to end with\n`\"-bin\"` and for the corresponding values to be base64 encoded.\n\n**Overrides** \n[Credentials.getRequestMetadata(URI uri)](/java/docs/reference/google-auth-library/latest/com.google.auth.Credentials#com_google_auth_Credentials_getRequestMetadata_java_net_URI_)\n\n### hasRequestMetadata()\n\n public boolean hasRequestMetadata()\n\nWhether the credentials have metadata entries that should be added to each request.\n\nThis should be called by the transport layer to see if [Credentials#getRequestMetadata](/java/docs/reference/google-auth-library/latest/com.google.auth.Credentials#com_google_auth_Credentials_getRequestMetadata_) should be used for each request.\n\n**Overrides** \n[Credentials.hasRequestMetadata()](/java/docs/reference/google-auth-library/latest/com.google.auth.Credentials#com_google_auth_Credentials_hasRequestMetadata__)\n\n### hasRequestMetadataOnly()\n\n public boolean hasRequestMetadataOnly()\n\nIndicates whether or not the Auth mechanism works purely by including request metadata.\n\nThis is meant for the transport layer. If this is true a transport does not need to take\nactions other than including the request metadata. If this is false, a transport must\nspecifically know about the authentication technology to support it, and should fail to accept\nthe credentials otherwise.\n\n**Overrides** \n[Credentials.hasRequestMetadataOnly()](/java/docs/reference/google-auth-library/latest/com.google.auth.Credentials#com_google_auth_Credentials_hasRequestMetadataOnly__)\n\n### refresh()\n\n public void refresh()\n\nThere is no concept of refreshing an API tokens, this method is a no-op.\n**Overrides** \n[Credentials.refresh()](/java/docs/reference/google-auth-library/latest/com.google.auth.Credentials#com_google_auth_Credentials_refresh__)"]]