@@ -355,6 +355,44 @@ Credentials in the following locations (in order):
3553554. Google Cloud Shell built-in credentials
3563565. Google Compute Engine built-in credentials
357357
358+ # ## Authenticating with an API Key
359+
360+ [Authenticating with API Keys](https://cloud.google.com/docs/authentication/api-keys) is supported by a handful of Google Cloud APIs.
361+
362+ We are actively exploring ways to improve the API Key experience.
363+ Currently, to use an API Key with a Java client library, you need to set the header for the relevant service Client manually.
364+
365+ For example, to set the API Key with the [Language service](https://cloud.google.com/java/docs/reference/google-cloud-language/latest/overview):
366+
367+ ` ` ` java
368+ public LanguageServiceClient createGrpcClientWithApiKey(String apiKey) throws Exception {
369+ // Manually set the api key via the header
370+ Map< String, String> header = new HashMap< String, String> () { {put(" x-goog-api-key" , apiKey); }};
371+ FixedHeaderProvider headerProvider = FixedHeaderProvider.create(header);
372+
373+ // Create the client
374+ TransportChannelProvider transportChannelProvider = InstantiatingGrpcChannelProvider.newBuilder().setHeaderProvider(headerProvider).build ();
375+ LanguageServiceSettings settings = LanguageServiceSettings.newBuilder().setTransportChannelProvider(transportChannelProvider).build ();
376+ LanguageServiceClient client = LanguageServiceClient.create(settings);
377+ return client;
378+ }
379+ ```
380+
381+ An example instantiation with the Language Client using rest:
382+ ``` java
383+ public LanguageServiceClient createRestClientWithApiKey(String apiKey) throws Exception {
384+ // Manually set the api key header
385+ Map<String , String > header = new HashMap<String , String > () { {put(" x-goog-api-key" , apiKey);}};
386+ FixedHeaderProvider headerProvider = FixedHeaderProvider . create(header);
387+
388+ // Create the client
389+ TransportChannelProvider transportChannelProvider = InstantiatingHttpJsonChannelProvider . newBuilder(). setHeaderProvider(headerProvider). build();
390+ LanguageServiceSettings settings = LanguageServiceSettings . newBuilder(). setTransportChannelProvider(transportChannelProvider). build();
391+ LanguageServiceClient client = LanguageServiceClient . create(settings);
392+ return client;
393+ }
394+ ```
395+
358396## Troubleshooting
359397
360398To get help, follow the instructions in the [ Troubleshooting document] ( https://github.com/googleapis/google-cloud-java/blob/main/TROUBLESHOOTING.md ) .
0 commit comments