Quickstart: Get started with Swift

This quickstart shows you how to set up your development environment and create a Swift executable application using Swift Package Manager (SPM). You add the Google Cloud Secret Manager client library and make an API call to list secrets in your Google Cloud project.

Before you begin

  1. In the Google Cloud console, on the project selector page, select or create a Google Cloud project:

    Go to project selector

  2. Confirm that billing is enabled for your Google Cloud project.

  3. Install and initialize the Google Cloud CLI. For instructions, see Install the gcloud CLI.

  4. Install the Swift toolchain:

    • macOS: Install Xcode 16 or later from the Mac App Store, or install the Command Line Tools by running xcode-select --install.
    • Linux: Install Swift using swiftly or by downloading a toolchain for your distribution from Swift.org.

    Verify that Swift 6.2 or later is installed by running:

    swift --version
    
  5. Enable the Secret Manager API:

    gcloud services enable secretmanager.googleapis.com
    
  6. Verify that you have permissions to create and list secrets, such as the Secret Manager Admin Identity and Access Management (IAM) role (roles/secretmanager.admin).

  7. Set up local authentication using Application Default Credentials (ADC):

    gcloud auth application-default login
    

    The Google Cloud client libraries for Swift automatically discover and use these credentials when running in your local development environment.

  8. Optional: Create a secret to list during this quickstart:

    printf "SECRET_DATA" | \
        gcloud secrets create my-quickstart-secret \
        --data-file=- \
        --replication-policy="automatic"
    

    Replace SECRET_DATA with your secret payload.

Create a Swift package

Create a new directory for your application and initialize a Swift executable package:

mkdir Quickstart
cd Quickstart
swift package init --name Quickstart --type executable

This creates a standard Swift package structure:

  • Package.swift: Manifest defining your package, dependencies, and targets.
  • Sources/Quickstart/Quickstart.swift: The entry point for your app.

If you're developing on macOS, the client libraries require macOS 15 or later. Because swift package init defaults to older macOS deployment targets, edit Package.swift to add platforms: [ .macOS(.v15) ]:

let package = Package(
    name: "Quickstart",
    platforms: [
        .macOS(.v15)
    ],
    // ...
)

Add the Secret Manager client library

Add the swift-google-cloud-secretmanager-v1 package dependency and link GoogleCloudSecretManagerV1 to your executable target:

swift package add-dependency \
    https://github.com/googleapis/swift-google-cloud-secretmanager-v1.git \
    --from 0.4.0
swift package add-target-dependency \
    GoogleCloudSecretManagerV1 Quickstart \
    --package swift-google-cloud-secretmanager-v1

Write the application code

Open Sources/Quickstart/Quickstart.swift and replace its contents with the following code:

import Foundation
import GoogleCloudSecretManagerV1

@main
struct Quickstart {
  static func main() async throws {
    let args = CommandLine.arguments.dropFirst()
    guard let projectId = args.first else {
      print("Usage: GoogleCloudCLI <projectId>")
      exit(1)
    }
    let client = try SecretManagerServiceClient()
    let secrets = client.listSecretsByItems(
      request: ListSecretsRequest().with { $0.parent = "projects/\(projectId)" })
    for try await item in secrets {
      print("  \(item)")
    }
  }
}

Key concepts

  • Asynchronous main entry point (@main): Swift's built-in structured concurrency lets you declare static func main() as async throws, eliminating the need for custom run loops or blocking threads.
  • Automatic credential resolution: SecretManagerServiceClient() detects Application Default Credentials configured with gcloud auth application-default login without manual credential passing.
  • Fluent request builder: The .with { ... } pattern provides type-safe, closure-based chaining to configure request parameters.
  • Streaming pagination (AsyncSequence): The listSecretsByItems(request:) method returns an AsyncSequence. Iterating with for try await automatically fetches subsequent pages so you can stream large datasets without managing page tokens manually.

Run the application

Run the application using swift run, passing your Google Cloud project ID as an argument:

swift run Quickstart PROJECT_ID

Replace PROJECT_ID with your Google Cloud project ID.

If you created the my-quickstart-secret secret earlier, the output lists the secret resource in your project (where PROJECT_NUMBER is your project number):

  GoogleCloudSecretManagerV1.Secret:
  name: "projects/PROJECT_NUMBER/secrets/my-quickstart-secret"

Clean up

To avoid incurring charges on your Google Cloud account for the resources used in this quickstart, see Secret Manager pricing and delete the resources you created:

  1. Delete the test secret:

    gcloud secrets delete my-quickstart-secret
    
  2. Optional: If you created a project specifically for this quickstart, delete the project in the Google Cloud console:

    Go to Manage resources

What's next