Skip to main content

Configure Access Keys

The SDK needs to be configured with your access key which is available in your Locker Secrets Dashboard. Initialize the access_key_id and secret_access_key to its value. You also need to set api_base value (default is https://secrets-core.locker.io). If you need to set your custom headers, you also need to set headers value in the options param:
using Locker;

string accessKeyId = "YOUR_ACCESS_KEY_ID";
string secretAccessKey = "YOUR_SECRET_ACCESS_KEY";
string apiBase = "YOUR_API_BASE";
Dictionary<string, string> headers = new Dictionary<string, string>()
{
    { "CF-Access-Client-Id", "YOUR_CF_ACCESS_CLIENT_ID" },
    { "CF-Access-Client-Secret", "YOUR_CF_ACCESS_CLIENT_SECRET" }
};
LockerConfiguration.Instance.Init(
    accessKeyId: accessKeyId,
    secretAccessKey: secretAccessKey,
    apiBase: apiBase,
    headers: headers
);

// setting by .env file
LockerConfiguration.Instance.Init(
    envPath: "YOUR_ENV_FILE_PATH"
);

Per-request configuration

All of the service methods accept an optional RequestOptions object. This is used if you want to pass the access key, headers on each method, or you want set type of return value (default type is string, if you want type of object use IsJson=true)
var requestOptions = new RequestOptions();
requestOptions.AccessKeyId = "ACCESS KEY ID";
requestOptions.SecretAccessKey = "SECRET ACCESS KEY";
requestOptions.ApiBase = "API BASE";
requestOptions.IsJson = true;
Now, you can use SDK to get or set values.

List secrets

Use .list() to get all the secrets in your project.
var service = new SecretService();
var secrets = service.List();

Get secret values

This function will get the secret value by a key. If the key does not exist, SDK will return the default_value
// Get a secret value by secret key.
// If the Key does not exist, SDK will return the defaultValue
var secretValue = service.GetSecret(
    name: "REDIS_CONNECTION",
    defaultValue: "Default Value"
);
Console.WriteLine(secretValue);

// Get a secret value by secret key and specific environment name.
// If the Key does not exist, SDK will return the defaultValue
secretValue = service.GetSecret(
    name: "REDIS_CONNECTION",
    environmentName: "staging",
    defaultValue: "Default Value"
);
Console.WriteLine(secretValue);

// Get a secret value by secret key.
// If the Key does not exist, SDK will throw exception
var options = new SecretRetrieveOptions();
var requestOptions = new RequestOptions();
var secretValue = service.Get(
    name: "REDIS_CONNECTION",
    retrieveOptions: options,
    requestOptions: requestOptions
);
Console.WriteLine(secretValue);

// Get a secret value by secret key and specific environment name.
// If the Key does not exist, SDK will throw exception
var options = new SecretRetrieveOptions();
var requestOptions = new RequestOptions();
var secretValue = service.Get(
    name: "REDIS_CONNECTION",
    environmentName: "staging",
    retrieveOptions: options,
    requestOptions: requestOptions
);
Console.WriteLine(secretValue);

Create secrets

Use the .Create() function to create a new secret:
var service = new SecretService();
var option = new SecretCreateOptions
{
    Key = "YOUR_NEW_SECRET_KEY",
    Value = "YOUR_NEW_SECRET_VALUE",
};
var newSecret = service.Create(option);

Update secrets

Use the .Modify() function to update a secret value:
var service = new SecretService();
var option = new SecretUpdateOptions
{
    Key = "YOUR_UPDATE_SECRET_KEY",
    Value = "YOUR_UPDATED_SECRET_VALUE",
};

// Update a secret value by secret key
var updatedSecret = service.Modify(
    name: "YOUR_SECRET_KEY",
    updateOptions: option
);

// Update a secret value by secret key and a specific environment name
var updatedSecret = service.Modify(
    name: "YOUR_SECRET_KEY",
    environmentName: "YOUR_ENV_NAME",
    updateOptions: option
);

List environments

Use the .List() function to retrieve all environments in your project:
var service = new EnvironmentService();
var environments = service.List();

Get environment by name

To retrieve an environment by name, use .Get():
var service = new EnvironmentService();
var environment = service.Get(name: "YOUR_ENV_NAME");
Console.WriteLine(environment);

Create an environment

To create a new environment, use .Create():
var service = new EnvironmentService();
var option = new EnvironmentCreateOptions()
{
    Name = "YOUR_NEW_ENV_NAME",
    ExternalUrl = "YOUR_NEW_ENV_EXTERNAL_URL",
    Description = "YOUR_NEW_ENV_DESCRIPTION"
};
var newEnv = service.Create(option);

Update an environment

To update the external_url of an environment by name, use .Modify():
environment = locker.modify_environment(name="staging", external_url="new.staging.host")