Quickstart by stack

Rust Quickstart

Install the zenmanage crate, evaluate flags in async Rust, target users with context, and share one client across Axum, Actix Web, or Rocket handlers.

Prerequisites

  • Rust 1.88 or newer, with a tokio runtime.
  • A server key (prefixed srv_) stored in environment configuration and not committed to source. Client and mobile keys are rejected.
  • A flag key you can test in a non-production environment.
Install bash

cargo add zenmanage
cargo add tokio --features macros,rt-multi-thread
Set the server key bash

export ZENMANAGE_ENVIRONMENT_TOKEN=srv_your_server_key_here
Initialize and evaluate rust

use zenmanage::{ConfigBuilder, Zenmanage};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Reads ZENMANAGE_ENVIRONMENT_TOKEN and the other ZENMANAGE_* settings
    let config = ConfigBuilder::from_environment().build()?;

    // Create one client when your app starts and share it. It holds the rules.
    let zenmanage = Zenmanage::new(config)?;
    let flags = zenmanage.flags();

    // The second argument is your default
    let new_checkout = flags
        .single("new-checkout", Some(false))
        .await?
        .is_enabled();
    let layout = flags
        .single("checkout-layout", Some("classic"))
        .await?
        .as_string();
    let page_size = flags.single("page-size", Some(20)).await?.as_number();

    println!("{new_checkout} {layout} {page_size}");

    // A program that exits right away can lose reports that are still in flight
    flags.flush_usage().await;

    Ok(())
}

Safe defaults

Pass a default to single. You get it when the flag is not in your rules, including when the rules could not be loaded, so an outage does not turn into a failed request. With no default, a flag that can't be found is an error.

Reading values

Use is_enabled() for boolean flags, and as_string(), as_number(), or as_json() for the rest. The wrong accessor never converts a value. as_bool() returns true for every flag that is not boolean, so don't use it to ask whether a flag is on.

Target a user with context rust

use zenmanage::{Attribute, Context, Zenmanage};

async fn can_see_reports(
    zenmanage: &Zenmanage,
    user_id: &str,
    plan: &str,
) -> zenmanage::Result<bool> {
    let context = Context::single("user", user_id, None::<String>) // no name sent
        .add_attribute(Attribute::new("plan", vec![plan]));

    // with_context returns a new manager, so build one per request
    let flag = zenmanage
        .flags()
        .with_context(context)
        .single("advanced-reports", Some(false))
        .await?;

    Ok(flag.is_enabled())
}

Share the client with your handlers

Each framework integration is a Cargo feature with one helper that puts the shared client in your framework's state: axum for Axum 0.8, actix for Actix Web 4, and rocket for Rocket 0.5. These examples take the user from the path to stay short. In your app, take it from your authentication.

Install for Axum bash

cargo add zenmanage --features axum
cargo add axum
cargo add tokio --features macros,rt-multi-thread,net
Axum rust

use std::sync::Arc;

use axum::{Extension, Router, extract::Path, routing::get};
use zenmanage::integrations::axum::extension_layer;
use zenmanage::{ConfigBuilder, Context, Zenmanage};

async fn home(
    Extension(zenmanage): Extension<Arc<Zenmanage>>,
    Path(user_id): Path<String>,
) -> &'static str {
    let context = Context::single("user", user_id, None::<String>);

    let new_home = zenmanage
        .flags()
        .with_context(context)
        .single("new-home", Some(false))
        .await
        .is_ok_and(|flag| flag.is_enabled());

    if new_home { "new" } else { "old" }
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let config = ConfigBuilder::from_environment().build()?;
    let zenmanage = Arc::new(Zenmanage::new(config)?);

    let app = Router::new()
        .route("/users/{user_id}/home", get(home))
        .layer(extension_layer(zenmanage));

    let listener = tokio::net::TcpListener::bind("127.0.0.1:3000").await?;
    axum::serve(listener, app).await?;

    Ok(())
}
Install for Actix Web bash

cargo add zenmanage --features actix
cargo add actix-web
Actix Web rust

use std::sync::Arc;

use actix_web::{App, HttpServer, get, web};
use zenmanage::integrations::actix::app_data;
use zenmanage::{ConfigBuilder, Context, Zenmanage};

#[get("/users/{user_id}/home")]
async fn home(
    zenmanage: web::Data<Arc<Zenmanage>>,
    user_id: web::Path<String>,
) -> &'static str {
    let context = Context::single("user", user_id.into_inner(), None::<String>);

    let new_home = zenmanage
        .flags()
        .with_context(context)
        .single("new-home", Some(false))
        .await
        .is_ok_and(|flag| flag.is_enabled());

    if new_home { "new" } else { "old" }
}

#[actix_web::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let config = ConfigBuilder::from_environment().build()?;
    let zenmanage = Arc::new(Zenmanage::new(config)?);

    HttpServer::new(move || {
        App::new()
            .app_data(app_data(zenmanage.clone()))
            .service(home)
    })
    .bind(("127.0.0.1", 3000))?
    .run()
    .await?;

    Ok(())
}
Install for Rocket bash

cargo add zenmanage --features rocket
cargo add rocket
Rocket rust

use std::sync::Arc;

use rocket::{State, get, launch, routes};
use zenmanage::integrations::rocket::manage_sdk;
use zenmanage::{ConfigBuilder, Context, Zenmanage};

#[get("/users/<user_id>/home")]
async fn home(zenmanage: &State<Arc<Zenmanage>>, user_id: &str) -> &'static str {
    let context = Context::single("user", user_id, None::<String>);

    let new_home = zenmanage
        .flags()
        .with_context(context)
        .single("new-home", Some(false))
        .await
        .is_ok_and(|flag| flag.is_enabled());

    if new_home { "new" } else { "old" }
}

#[launch]
fn rocket() -> _ {
    let config = ConfigBuilder::from_environment()
        .build()
        .expect("Zenmanage configuration");
    let zenmanage = Arc::new(Zenmanage::new(config).expect("Zenmanage client"));

    manage_sdk(rocket::build().mount("/", routes![home]), zenmanage)
}
Shared defaults and every flag rust

use zenmanage::{DefaultsCollection, Zenmanage};

async fn print_flags(zenmanage: &Zenmanage) -> zenmanage::Result<()> {
    let defaults = DefaultsCollection::new()
        .set("new-search", false)
        .set("page-size", 20);
    let flags = zenmanage.flags().with_defaults(defaults);

    // The shared default applies if the flag can't be found
    let page_size = flags.single("page-size", None::<i32>).await?.as_number();
    println!("page size: {page_size}");

    // all() lists the flags in your rules and does not report usage
    for flag in flags.all().await? {
        println!("{}: {:?}", flag.key(), flag.value());
    }

    Ok(())
}
Cache rules on disk rust

use zenmanage::{CacheBackendType, ConfigBuilder, Zenmanage};

fn client_with_disk_cache() -> zenmanage::Result<Zenmanage> {
    let config = ConfigBuilder::from_environment()
        .with_cache_ttl(300)
        .with_cache_backend(CacheBackendType::FileSystem)
        // Use one directory per environment
        .with_cache_directory("/var/cache/zenmanage")
        .build()?;

    Zenmanage::new(config)
}

Local evaluation

Rules are cached and evaluated inside your process, so a check against loaded rules does not cost a network round trip. The first check, and the first one after the rules expire, loads them before it answers. Pass a stable identifier in the context: percentage rollouts use it, so the same user gets the same answer on every server. single reports usage in the background and never delays an evaluation. In a command-line tool, job, or serverless function, call flush_usage before you exit.

Caching and refresh

Rules are used for the cache TTL (3600 seconds by default), then reloaded by the next flag check. If a reload fails, the rules already loaded stay in use. Pick the memory cache (the default), a filesystem cache, no cache, or your own CacheBackend, such as one backed by Redis. To apply a change right away, call refresh_rules — for example from your webhook handler, after you verify the X-Zenmanage-Signature header.

Next step

Take the next integration step in your own stack.

Start with the quickstart that matches your runtime, then return to the reference pages when you need exact request and payload details.

Language and framework names and logos shown above are trademarks or registered trademarks of their respective owners. Their use here is for identification purposes only and does not imply endorsement of, or affiliation with, Zenmanage.