KeyEnv 1.2.2

dotnet add package KeyEnv --version 1.2.2
                    
NuGet\Install-Package KeyEnv -Version 1.2.2
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="KeyEnv" Version="1.2.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="KeyEnv" Version="1.2.2" />
                    
Directory.Packages.props
<PackageReference Include="KeyEnv" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add KeyEnv --version 1.2.2
                    
#r "nuget: KeyEnv, 1.2.2"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package KeyEnv@1.2.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=KeyEnv&version=1.2.2
                    
Install as a Cake Addin
#tool nuget:?package=KeyEnv&version=1.2.2
                    
Install as a Cake Tool

KeyEnv .NET SDK

Official .NET SDK for KeyEnv - Secrets management made simple.

Installation

dotnet add package KeyEnv

Or via the NuGet Package Manager:

Install-Package KeyEnv

Quick Start

using KeyEnv;

// Create a client with your service token
var client = KeyEnvClient.Create("your-service-token");

// Fetch all secrets for an environment
var secrets = await client.ExportSecretsAsync("your-project-id", "production");

foreach (var secret in secrets)
{
    Console.WriteLine($"{secret.Key}={secret.Value}");
}

Loading Secrets into Environment Variables

// Load all secrets into environment variables
int count = await client.LoadEnvAsync("your-project-id", "production");
Console.WriteLine($"Loaded {count} secrets");

// Now use them
var dbUrl = Environment.GetEnvironmentVariable("DATABASE_URL");

Getting a Single Secret

var secret = await client.GetSecretAsync("your-project-id", "production", "DATABASE_URL");
Console.WriteLine(secret.Value);

Setting Secrets

// Set a secret (creates or updates)
await client.SetSecretAsync(
    "your-project-id",
    "production",
    "API_KEY",
    "sk_live_...",
    description: "My API key"
);

Bulk Import

var secrets = new[]
{
    SecretInput.Create("DATABASE_URL", "postgres://..."),
    SecretInput.Create("API_KEY", "sk_...", "My API key"),
};

var result = await client.BulkImportAsync(
    "your-project-id",
    "development",
    secrets,
    new BulkImportOptions { Overwrite = true }
);

Console.WriteLine($"Created: {result.Created}, Updated: {result.Updated}");

Error Handling

try
{
    var secret = await client.GetSecretAsync("project-id", "production", "MISSING_KEY");
}
catch (KeyEnvException ex) when (ex.IsNotFound)
{
    Console.WriteLine("Secret not found");
}
catch (KeyEnvException ex) when (ex.IsUnauthorized)
{
    Console.WriteLine("Invalid or expired token");
}
catch (KeyEnvException ex) when (ex.IsForbidden)
{
    Console.WriteLine("Access denied");
}
catch (KeyEnvException ex)
{
    Console.WriteLine($"Error {ex.StatusCode}: {ex.Message}");
}

Configuration Options

var client = KeyEnvClient.Create(new KeyEnvOptions
{
    Token = "your-service-token",
    BaseUrl = "https://api.keyenv.dev",      // Optional: custom API URL
    Timeout = TimeSpan.FromSeconds(60),       // Optional: request timeout
    CacheTtl = TimeSpan.FromMinutes(5)        // Optional: cache secrets for 5 min
});

Caching

Enable caching for better performance in serverless environments:

var client = KeyEnvClient.Create(new KeyEnvOptions
{
    Token = "your-token",
    CacheTtl = TimeSpan.FromMinutes(5)  // Cache secrets for 5 minutes
});

// First call fetches from API
var secrets1 = await client.ExportSecretsAsync("project-id", "production");

// Second call returns cached result
var secrets2 = await client.ExportSecretsAsync("project-id", "production");

// Clear cache when needed
client.ClearCache("project-id", "production");
// Or clear all cache
client.ClearAllCache();

Generating .env Files

string envContent = await client.GenerateEnvFileAsync("project-id", "production");
await File.WriteAllTextAsync(".env", envContent);

ASP.NET Core Integration

// Program.cs
var builder = WebApplication.CreateBuilder(args);

// Load KeyEnv secrets before building
using var keyenv = KeyEnvClient.Create(builder.Configuration["KeyEnv:Token"]!);
await keyenv.LoadEnvAsync(
    builder.Configuration["KeyEnv:ProjectId"]!,
    builder.Environment.EnvironmentName.ToLowerInvariant()
);

// Now environment variables are available
var app = builder.Build();

API Reference

KeyEnvClient

Method Description
Create(token) Create a client with a token
Create(options) Create a client with options
ExportSecretsAsync(projectId, environment) Export all secrets for an environment
ExportSecretsAsDictionaryAsync(projectId, environment) Export secrets as a key-value dictionary
GetSecretAsync(projectId, environment, key) Get a single secret
SetSecretAsync(projectId, environment, key, value, description?) Create or update a secret
DeleteSecretAsync(projectId, environment, key) Delete a secret
LoadEnvAsync(projectId, environment) Load secrets into environment variables
GenerateEnvFileAsync(projectId, environment) Generate .env file content
BulkImportAsync(projectId, environment, secrets, options?) Import multiple secrets
GetSecretHistoryAsync(projectId, environment, key) Get secret version history
ValidateTokenAsync() Validate the token and get user info
ClearCache(projectId, environment) Clear cache for an environment
ClearAllCache() Clear all cached data

KeyEnvException

Property Description
StatusCode HTTP status code (0 for non-HTTP errors)
ErrorCode Optional error code for programmatic handling
IsNotFound True if 404 error
IsUnauthorized True if 401 error
IsForbidden True if 403 error
IsConflict True if 409 error
IsRateLimited True if 429 error
IsServerError True if 5xx error

Requirements

  • .NET 6.0 or later
  • Nullable reference types enabled

License

MIT License - see LICENSE for details.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.2.2 123 2/24/2026
1.2.1 118 2/16/2026