C#

Fetch secrets for your C# ASP.NET Core application using the Doppler API.

We don't have a C# SDK available yet, but if you'd like to be notified when it's available, please reach out to us at [email protected].

In the meantime, you can use the below code sample as a starting point which uses our download secrets API for fetching secrets at runtime.

Service Token

The first step is to create a Service Token to provide read-only access to the project and config you wish to supply secrets for:

1226

Then expose the Service Token value to your environment using the DOPPLER_TOKEN environment variable

export DOPPLER_TOKEN='dp.st.dev.xxxx'
$env:DOPPLER_TOKEN = 'dp.st.dev.xxxx'
SET VAR_NAME=dp.st.dev.xxxx

Secrets API

Here is a simple secrets fetching example.

📘

Let us know if you think this sample code can be improved by emailing us at [email protected].

using System;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace DopplerExamples
{
    public class Doppler
    {
        [JsonPropertyName("DOPPLER_PROJECT")]
        public string DopplerProject { get; set; }

        [JsonPropertyName("DOPPLER_ENVIRONMENT")]
        public string DopplerEnvironment { get; set; }

        [JsonPropertyName("DOPPLER_CONFIG")]
        public string DopplerConfig { get; set; }

        private static HttpClient client = new HttpClient();

        public static async Task<Doppler> FetchSecretsAsync()
        {   
            var dopplerToken = Environment.GetEnvironmentVariable("DOPPLER_TOKEN");

            client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", dopplerToken);
            var streamTask = client.GetStreamAsync("https://api.doppler.com/v3/configs/config/secrets/download?format=json");
            var secrets = await JsonSerializer.DeserializeAsync<Doppler>(await streamTask);

            return secrets;
        }
    }

    class Program
    {
        static async Task Main()
        {
            var secrets = await Doppler.FetchSecretsAsync();

            Console.WriteLine($"Project: {secrets.DopplerProject}");
            Console.WriteLine($"Environment: {secrets.DopplerEnvironment}");
            Console.WriteLine($"Config: {secrets.DopplerConfig}");
        }
    }
}

👍

Awesome Work!

Now you know how to fetch secrets for your ASP.NET Core C# application using the secrets download API.