Crate keyenv

Crate keyenv 

Source
Expand description

§KeyEnv Rust SDK

Official Rust SDK for KeyEnv - Secrets management made simple.

§Quick Start

use keyenv::KeyEnv;
use std::env;

#[tokio::main]
async fn main() -> Result<(), keyenv::Error> {
    let client = KeyEnv::builder()
        .token(env::var("KEYENV_TOKEN").expect("KEYENV_TOKEN not set"))
        .build()?;

    // Load secrets into environment
    client.load_env("your-project-id", "production").await?;

    println!("{}", env::var("DATABASE_URL").unwrap());
    Ok(())
}

§Loading Secrets

§Load into Environment

let count = client.load_env("project-id", "production").await?;
println!("Loaded {} secrets", count);

§Export as HashMap

let secrets = client.export_secrets_as_map("project-id", "production").await?;
println!("{}", secrets.get("DATABASE_URL").unwrap());

§Managing Secrets

// Get a secret
let secret = client.get_secret("project-id", "production", "DATABASE_URL").await?;
println!("{}", secret.value);

// Set a secret
client.set_secret("project-id", "production", "API_KEY", "sk_live_...").await?;

// Delete a secret
client.delete_secret("project-id", "production", "OLD_KEY").await?;

§Error Handling

use keyenv::{KeyEnv, Error};

match client.get_secret("project-id", "production", "MISSING_KEY").await {
    Ok(secret) => println!("{}", secret.value),
    Err(Error::Api { status, message, .. }) => {
        match status {
            401 => eprintln!("Invalid or expired token"),
            403 => eprintln!("Access denied"),
            404 => eprintln!("Secret not found"),
            _ => eprintln!("Error {}: {}", status, message),
        }
    }
    Err(e) => eprintln!("Error: {}", e),
}

§Caching

Enable caching for better performance in serverless environments:

use keyenv::KeyEnv;
use std::time::Duration;

let client = KeyEnv::builder()
    .token("your-token")
    .cache_ttl(Duration::from_secs(300))  // 5 minutes
    .build()?;

Structs§

BulkImportOptions
Options for bulk import operations.
BulkImportResult
Result of a bulk import operation.
CurrentUserResponse
Response containing current user or service token information.
DefaultPermission
Default permission settings for an environment.
Environment
An environment within a project.
KeyEnv
KeyEnv API client.
KeyEnvBuilder
Builder for creating a KeyEnv client.
MyPermissionsResponse
Response containing the current user’s permissions.
Permission
A permission for an environment.
PermissionInput
Input for setting a permission.
Project
A KeyEnv project.
Secret
A secret’s metadata without the value.
SecretHistory
Historical version of a secret.
SecretInput
Input for creating or importing a secret.
SecretWithInheritance
A secret with inheritance information.
SecretWithValue
A secret including its decrypted value.
SecretWithValueAndInheritance
A secret with value and inheritance information.
ServiceToken
Information about a service token.
Team
A team.
User
A KeyEnv user.

Enums§

Error
Error type for KeyEnv SDK operations.

Constants§

DEFAULT_BASE_URL
Default API base URL.
DEFAULT_TIMEOUT
Default request timeout.
VERSION
SDK version.

Type Aliases§

Result
Result type alias for KeyEnv operations.