This quickstart shows you how to create and access secrets using Secret Manager on Google Cloud.
Before you begin
Configure Secret Manager and your local environment, once per project.
Create and access a secret version
The following examples demonstrate creating and accessing a secret version's contents. For more information, refer to Managing secrets.
Web UI
To create the Secret and Secret Version:
-
Go to the Secret Manager page in the Cloud Console.
-
On the Secret Manager page, click Create Secret.
-
On the Create secret page, under Name, enter
my-secret
. -
In the Secret value field, enter
my super secret data
. -
Leave the Regions section unchanged.
-
Click the Create secret button.
To access the contents of the secret version:
-
Go to the Secret Manager page in the Cloud Console.
-
On the Secret Manager page, click on my-secret.
-
On the Secret details page, in the Versions table, locate version 1.
-
In the Actions column, click View more
. -
Click View secret value from the menu.
-
A dialog appears that shows the Secret value. Click Done to close the dialog.
Command-line
To use Secret Manager on the command line, first Install or upgrade to version 306.0.0 or higher of the Cloud SDK.
To create a secret and store a string as the contents of the first secret version:
$ echo -n "my super secret data" | gcloud secrets create my-secret \
--replication-policy="replication-policy" \
--data-file=-
Where replication-policy is one of automatic
or user-managed
.
To access the contents of a specific secret version:
$ gcloud secrets versions access 1 --secret="my-secret"
To access the contents of the latest secret version:
$ gcloud secrets versions access latest --secret="my-secret"
C#
To run this code, first set up a C# development environment and install the Secret Manager C# SDK.
Go
To run this code, first set up a Go development environment and install the Secret Manager Go SDK.
Java
To run this code, first set up a Java development environment and install the Secret Manager Java SDK.
Node.js
To run this code, first set up a Node.js development environment and install the Secret Manager Node.js SDK.
PHP
To run this code, first learn about using PHP on Google Cloud and install the Secret Manager PHP SDK.
Python
To run this code, first set up a Python development environment and install the Secret Manager Python SDK.
Ruby
To run this code, first set up a Ruby development environment and install the Secret Manager Ruby SDK.
API
These examples use curl to demonstrate using the API.
To create the secret and store a string as the contents of a secret version:
$ curl "https://secretmanager.googleapis.com/v1/projects/project-id/secrets?secretId=my-secret" \
--request "POST" \
--header "authorization: Bearer $(gcloud auth print-access-token)" \
--header "content-type: application/json" \
--header "x-goog-user-project: project-id" \
--data "{\"replication\": {\"automatic\": {}}}"
To access the secret version's contents:
$ curl "https://secretmanager.googleapis.com/v1/projects/project-id/secrets/my-secret/versions/1:access" \
--request "GET" \
--header "authorization: Bearer $(gcloud auth print-access-token)" \
--header "content-type: application/json" \
--header "x-goog-user-project: project-id"
What's next
- Learn about Secret Manager.
- Learn about managing secrets.