The Google Cloud client libraries for Swift automatically
configure the authentication credentials used to authenticate requests to
Google Cloud. Some applications might need to override the default
credentials. This document shows you how to override the default authentication
credentials.

## Before you begin

This guide uses the Cloud Natural Language API. To enable this API, follow the
[Natural Language API setup guide](https://docs.cloud.google.com/natural-language/docs/setup.md.txt).

For complete setup instructions for the Swift client libraries, see
[Quickstart: Get started with Swift](https://docs.cloud.google.com/swift/quickstart.md.txt).

## Override the default credentials with an API key

[API keys](https://docs.cloud.google.com/docs/authentication/api-keys.md.txt) are text strings that grant access to
some Google Cloud services. Using API keys can simplify development
because they require less configuration than other
[authentication methods](https://docs.cloud.google.com/docs/authentication.md.txt). Before you use API keys, read
[Best practices for managing API
keys](https://docs.cloud.google.com/docs/authentication/api-keys-best-practices) to understand the associated
security risks.

In this example, you configure the Natural Language API client to authenticate
using an API key:

1. Add the imports required to use the client library and authentication types:


   ```swift
   import Foundation
   import GoogleAuth
   import GoogleGax
   import GoogleCloudLanguageV2
   ```

   <br />

2. Write a function that receives the API key as a parameter:


   ```swift
   func sample(apiKey: String) async throws {
   ```

   <br />

3. Create a `Credentials` instance with the API key, and pass it to
   `ClientOptions` when initializing the client:


   ```swift
   let credentials = try Credentials(configuration: .apiKey(apiKey))
   let client = try LanguageServiceClient(
     ClientOptions().with { $0.credentials = credentials }
   )
   ```

   <br />

4. Use the client to analyze the sentiment of a text document:


   ```swift
   let document = Document().with {
     $0.type = .plainText
     $0.source = .content("Hello World!")
   }
   let response = try await client.analyzeSentiment(
     request: .init().with { $0.document = document })
   print("response=\(response)")
   ```

   <br />

## Complete code

The following example shows the complete code for this guide:

```swift
import Foundation
import GoogleAuth
import GoogleGax
import GoogleCloudLanguageV2

func sample(apiKey: String) async throws {
  let credentials = try Credentials(configuration: .apiKey(apiKey))
  let client = try LanguageServiceClient(
    ClientOptions().with { $0.credentials = credentials }
  )
  let document = Document().with {
    $0.type = .plainText
    $0.source = .content("Hello World!")
  }
  let response = try await client.analyzeSentiment(
    request: .init().with { $0.document = document })
  print("response=\(response)")
}
```

## What's next

- [Override the default endpoint](https://docs.cloud.google.com/swift/override-endpoint.md.txt) describes how to change the default endpoint used by the Swift client libraries.
- [Enable logging](https://docs.cloud.google.com/swift/enable-logging.md.txt) describes how to enable debug logging to troubleshoot requests and responses.
- Learn how to configure [Package traits](https://docs.cloud.google.com/swift/package-traits.md.txt) for large client libraries.
- Browse the [Swift Cloud Client Libraries](https://docs.cloud.google.com/swift/reference.md.txt) reference documentation.