The documentation you are viewing is for Dapr v0.11 which is an older version of Dapr. For up-to-date documentation, see the latest version.

How To: Retrieve a secret

Use the secret store building block to securely retrieve a secret

This article provides guidance on using Dapr’s secrets API in your code to leverage the secrets store building block. The secrets API allows you to easily retrieve secrets in your application code from a configured secret store.

Set up a secret store

Before retrieving secrets in your application’s code, you must have a secret store component configured. For the purposes of this guide, as an example you will configure a local secret store which uses a local JSON file to store secrets.

Note: The component used in this example is not secured and is not recommended for production deployments. You can find other alternatives here.

Create a file named secrets.json with the following contents:

   "my-secret" : "I'm Batman"

Create a directory for your components file named components and inside it create a file named localSecretStore.yaml with the following contents:

kind: Component
  name: my-secrets-store
  namespace: default
  type: secretstores.local.file
  version: v1
  - name: secretsFile
    value: <PATH TO SECRETS FILE>/mysecrets.json
  - name: nestedSeparator
    value: ":"

Make sure to replace <PATH TO SECRETS FILE> with the path to the JSON file you just created.

To configure a different kind of secret store see the guidance on how to configure a secret store and review supported secret stores to see specific details required for different secret store solutions.

Get a secret

Now run the Dapr sidecar (with no application)

dapr run --app-id my-app --dapr-http-port 3500 --components-path ./components

And now you can get the secret by calling the Dapr sidecar using the secrets API:

curl http://localhost:3500/v1.0/secrets/my-secrets-store/my-secret

For a full API reference, go here.

Calling the secrets API from your code

Once you have a secret store set up, you can call Dapr to get the secrets from your application code. Here are a few examples in different programming languages:

import (

func main() {
  url := "http://localhost:3500/v1.0/secrets/my-secrets-store/my-secret"

  res, err := http.Get(url)
  if err != nil {
  defer res.Body.Close()

  body, _ := ioutil.ReadAll(res.Body)

const secretsUrl = `http://localhost:3500/v1.0/secrets`;

        .then((response) => {
            if (!response.ok) {
                throw "Could not get secret";
            return response.text();
        }).then((secret) => {

import requests as req

resp = req.get("http://localhost:3500/v1.0/secrets/my-secrets-store/my-secret")

use std::{thread};

async fn main() -> Result<(), reqwest::Error> {
    let res = reqwest::get("http://localhost:3500/v1.0/secrets/my-secrets-store/my-secret").await?;
    let body = res.text().await?;
    println!("Secret:{}", body);



var client = new HttpClient();
var response = await client.GetAsync("http://localhost:3500/v1.0/secrets/my-secrets-store/my-secret");

string secret = await response.Content.ReadAsStringAsync();
Last modified July 7, 2022: update nav bar v0.11 (#2633) (b309d3d)