The Google Cloud client libraries for Swift automatically
configure the endpoint for each service. Some applications might need to
override the default endpoint, either because their network has specific
requirements or because they need to use regional versions of a service. This
document shows you how to override the default endpoint.

## Before you begin

This guide uses the Secret Manager API. To enable this API, follow the
[Secret Manager quickstart](https://docs.cloud.google.com/secret-manager/docs/quickstart).

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 endpoint

In this example, you configure the client library to use Secret Manager
[regional endpoints](https://docs.cloud.google.com/sovereign-controls-by-partners/docs/regional-endpoints.md.txt).
You can use the same override to configure the endpoint with one of the
[private access options](https://docs.cloud.google.com/vpc/docs/private-access-options.md.txt), or for
[locational endpoints](https://docs.cloud.google.com/storage/docs/locational-endpoints.md.txt) in the services that
support them.

1. Add the imports required to use the client library:


   ```swift
   import Foundation
   import GoogleGax
   import GoogleCloudSecretManagerV1
   ```

   <br />

2. Write a function that receives the project ID and region as parameters:


   ```swift
   func sample(projectId: String, region: String) async throws {
   ```

   <br />

3. Initialize a client and override the endpoint to use a regional endpoint:


   ```swift
   let client = try SecretManagerServiceClient(
     ClientOptions().with {
       $0.endpoint = "https://secretmanager.\(region).rep.googleapis.com"
     })
   ```

   <br />

4. Use the client to retrieve the list of secrets in a region and iterate over
   the results:


   ```swift
   let secrets = client.listSecretsByItems(
     request: ListSecretsRequest().with { $0.parent = "projects/\(projectId)/locations/\(region)" })
   for try await item in secrets {
     print("  \(item)")
   }
   ```

   <br />

## Complete code

The following example shows the complete code for this guide:

```swift
import Foundation
import GoogleGax
import GoogleCloudSecretManagerV1

func sample(projectId: String, region: String) async throws {
  let client = try SecretManagerServiceClient(
    ClientOptions().with {
      $0.endpoint = "https://secretmanager.\(region).rep.googleapis.com"
    })
  let secrets = client.listSecretsByItems(
    request: ListSecretsRequest().with { $0.parent = "projects/\(projectId)/locations/\(region)" })
  for try await item in secrets {
    print("  \(item)")
  }
}
```

## What's next

- [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.