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
In the Google Cloud console, on the project selector page, select or create a Google Cloud project:
Confirm that billing is enabled for your Google Cloud project.
Install and initialize the Google Cloud CLI. For instructions, see Install the gcloud CLI.
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- macOS: Install Xcode 16 or later from the Mac App Store, or install
the Command Line Tools by running
Enable the Secret Manager API:
gcloud services enable secretmanager.googleapis.comVerify that you have permissions to create and list secrets, such as the Secret Manager Admin Identity and Access Management (IAM) role (
roles/secretmanager.admin).Set up local authentication using Application Default Credentials (ADC):
gcloud auth application-default loginThe Google Cloud client libraries for Swift automatically discover and use these credentials when running in your local development environment.
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_DATAwith 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:
Key concepts
- Asynchronous main entry point (
@main): Swift's built-in structured concurrency lets you declarestatic func main()asasync throws, eliminating the need for custom run loops or blocking threads. - Automatic credential resolution:
SecretManagerServiceClient()detects Application Default Credentials configured withgcloud auth application-default loginwithout manual credential passing. - Fluent request builder: The
.with { ... }pattern provides type-safe, closure-based chaining to configure request parameters. - Streaming pagination (
AsyncSequence): ThelistSecretsByItems(request:)method returns anAsyncSequence. Iterating withfor try awaitautomatically 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:
Delete the test secret:
gcloud secrets delete my-quickstart-secretOptional: If you created a project specifically for this quickstart, delete the project in the Google Cloud console:
What's next
- Read the Swift on Google Cloud overview for details on supported environments, Swift concurrency, and memory safety.
- Browse the Google Cloud client libraries for Swift reference documentation.
- Learn how to enable debug logging to troubleshoot requests and responses.
- Explore the Google Cloud client libraries on GitHub.