Quickstart by stack
Ruby Quickstart
Install the zenmanage gem, evaluate flags in Rails controllers, views, and jobs or in plain Ruby, target users with context, and refresh rules with a signed webhook.
Prerequisites
- Ruby 3.2 or newer. The gem has no runtime dependencies, and Rails is optional.
- A server key (prefixed srv_) stored in environment configuration or Rails credentials and not committed to source. Client and mobile keys are rejected.
- A flag key you can test in a non-production environment.
bundle add zenmanage
# Without Bundler
gem install zenmanage
export ZENMANAGE_ENVIRONMENT_TOKEN=srv_your_server_key_here
# Optional in Rails: generate an initializer to keep your settings in the app
bin/rails generate zenmanage:install
class CheckoutController < ApplicationController
def show
@new_checkout = feature_enabled?("new-checkout")
@layout = zen_flags.single("checkout-layout", "classic").as_string
@page_size = zen_flags.single("page-size", 20).as_number
end
end
Rails, configured at boot
With ZENMANAGE_ENVIRONMENT_TOKEN set, there is nothing else to wire up. The gem configures itself when your app boots, logs to Rails.logger, and adds feature_enabled? and zen_flags to your controllers and views. Without a token your app still boots — it logs a warning and feature_enabled? returns the default.
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. feature_enabled? defaults to false, and takes default: true when a flag should fail open.
<% if feature_enabled?("promo-banner") %>
<%= render "shared/promo_banner" %>
<% end %>
# config/initializers/zenmanage.rb
Zenmanage.configure do |config|
# Leave this line out to keep using ZENMANAGE_ENVIRONMENT_TOKEN
config.environment_token = Rails.application.credentials.zenmanage_environment_token
config.cache_ttl = 300
end
Who flags are evaluated for
If a controller has a current_user, flags are evaluated for a user context with its id and, if it has one, its name. Both are sent to Zenmanage with usage reports. Override zenmanage_context to send something else, such as an attribute your targeting rules need, or return nil to evaluate without a context.
class ApplicationController < ActionController::Base
private
def zenmanage_context
return unless current_user
Zenmanage::Context.new(
"user",
identifier: current_user.id.to_s, # no name sent
attributes: [Zenmanage::Attribute.new("plan", [current_user.plan])],
)
end
end
class ExportJob < ApplicationJob
def perform(user)
context = Zenmanage::Context.single("user", user.id.to_s)
if Zenmanage.flags.with_context(context).single("new-export", false).enabled?
NewExport.call(user)
else
LegacyExport.call(user)
end
end
end
require "zenmanage"
config = Zenmanage::ConfigBuilder.create
.with_environment_token(ENV.fetch("ZENMANAGE_ENVIRONMENT_TOKEN"))
.build
# Create one client and reuse it. It holds the loaded rules.
client = Zenmanage::Client.new(config)
context = Zenmanage::Context.new("user", identifier: "user-123", name: "Jane Doe")
context.add_attribute(Zenmanage::Attribute.new("plan", ["pro"]))
if client.flags.with_context(context).single("beta-feature", false).enabled?
puts "Show the beta feature"
end
# single reports usage in the background, and a script that exits
# right away can lose that report. Send one more and wait for it.
# If both arrive, the flag counts as used twice.
client.flags.report_usage("beta-feature", context)&.join
require "sinatra"
require "zenmanage"
client = Zenmanage::Client.new(Zenmanage::ConfigBuilder.from_environment.build)
# The middleware is named under Rails, but works in any Rack app
use Zenmanage::Rails::Middleware, client
get "/" do
env["zenmanage.flags"].single("new-home", false).enabled? ? "new" : "old"
end
defaults = Zenmanage::DefaultsCollection.from_hash(
"new-search" => false,
"page-size" => 20,
)
flags = client.flags.with_defaults(defaults)
flags.single("page-size").as_number # the default applies if the flag can't be found
flags.all.each { |flag| puts "#{flag.key}: #{flag.value}" } # includes your defaults
Local evaluation
Rules are fetched once, cached, and evaluated inside your process, so a flag check does not cost a network round trip. Pass a stable identifier in the context: percentage rollouts use it, so the same user gets the same answer every time. single reports usage in the background; all does not.
Caching and refresh
Rules are used for cache_ttl seconds (3600 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 shared by processes on one machine such as Puma workers, no cache, or your own store with get and set.
Instant cache refresh via webhook
To apply changes as soon as they are published, mount the built-in endpoint and point an environment webhook at it, using the POST, PUT, or PATCH method. Set ZENMANAGE_WEBHOOK_SECRET to the signing secret (it starts with whsec_). The endpoint rejects a request whose X-Zenmanage-Signature header does not match that secret. A webhook reaches one process — with several Puma workers or hosts, the others pick the change up when their cache_ttl passes.
Rails.application.routes.draw do
mount Zenmanage::Webhook::Endpoint.new => "/zenmanage/webhook"
end
See the webhooks reference for the event types and payloads a Zenmanage environment webhook can send.
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.