Skip to main content

Go SDK

Segment membership​

Create the list in the dashboard and attach it to a Targeting filter. Sync members with a Backend application key. Evaluation uses the published list after the next definitions refresh. Flow · HTTP API

Server only

Requires a Backend application key. Do not call this from a browser, mobile app, or other public client.

client := toggly.SegmentMembershipClient{AppKey: os.Getenv("TOGGLY_APP_KEY")}
_, err := client.AddSegmentMembers("Beta Testers", []string{"user-123"})

Use Toggly’s Go SDK for server-side, local evaluation of feature flags.

Quick reference

Grab the printable Go cheat sheet (download PDF) — net/http middleware, signed defs, snapshots, gRPC metrics.

  • Module: github.com/ops-ai/Toggly.FeatureManagement/toggly-go
  • Package: github.com/ops-ai/Toggly.FeatureManagement/toggly-go/toggly
  • Go version: 1.24+

Installation​

go get github.com/ops-ai/Toggly.FeatureManagement/toggly-go@latest

Quick start​

package main

import (
"context"
"log"
"os"
"time"

"github.com/ops-ai/Toggly.FeatureManagement/toggly-go/toggly"
"github.com/ops-ai/Toggly.FeatureManagement/toggly-go/toggly/session"
)

func main() {
client, err := toggly.NewClient(toggly.Config{
AppKey: os.Getenv("TOGGLY_APP_KEY"),
Environment: "Production",
BaseURL: "https://app.toggly.io/",

// Optional cache for stable decisions from non-deterministic filters.
SessionStore: session.NewMemoryStore(),
SessionTTL: 30 * time.Minute,

// Optional features
EnableLiveUpdates: true,
})
if err != nil {
log.Fatal(err)
}
defer func() { _ = client.Close() }()

// Known user context is supplied before the first local evaluation.
// In an HTTP app, derive these values from the authenticated request.
evalCtx := toggly.Context{
Identity: "user-123",
Groups: []string{"beta"},
Traits: map[string]any{"country": "US"},
}

enabled, err := client.IsEnabled(context.Background(), "new-dashboard", evalCtx)
if err != nil {
log.Fatal(err)
}

_ = enabled
}

NewClient validates configuration and starts background loading; it does not wait for definitions to arrive. An early or unknown flag returns false by default. Keep a normal fallback experience available while the SDK loads, and handle evaluation errors before using the result. An empty app key returns an error: validate configuration or show setup instructions before constructing a client.

Try your first flag​

  1. Create an app in Toggly and set TOGGLY_APP_KEY locally. Create new-dashboard in its Production environment, initially off.
  2. Start the application with an existing page or handler as the off-state fallback.
  3. Enable the flag with an Always On rule. After the SDK receives the update, the next evaluation changes without an application deployment.
  4. Add user targeting and evaluate two different authenticated request contexts. Normal Boolean evaluation downloads shared definitions and evaluates users locally; changing the context does not cause a second API call.

Catalog-local variants assign from definitions. After middleware or SetIdentity, call GetVariant(ctx, "checkout-flow", Context{}) — do not lead with a hand-built {Identity: …} on every call, and do not create a separate variants client per user.

Next steps​

Sample catalog​

Explore Toggly.Samples for runnable feature-flag workshops and their setup recipes. Use each sample's README for the exact packages, supported SDK surfaces, and manual walkthrough.