All docs

docs / get started

Self-hosting Flagsmith for experiments

Run Flagsmith with experimentation, store events, and point the SDK's events at your server.

On this page

Petri is built for self-hosted Flagsmith first. Experiments need three things a default self-hosted install doesn't give you out of the box, and Petri checks for each one.

Zero clicks

sh
docker compose -f selfhost/docker-compose.yml up -d
node selfhost/bootstrap.mjs

bootstrap.mjs waits for the API, then creates everything this page otherwise walks you through: the admin account (random password, saved to .petri/admin.json), an organisation, the Petri Coffee project with Development and Production, an organisation API key, and event storage for both environments (step 2). It writes the Flagsmith CLI's flagsmith.json and a gitignored flagsmith.env holding FLAGSMITH_API_KEY_localhost_8000, so Petri, petri-check and the flagsmith CLI all connect with no prompt. Re-running it is safe. GitHub Codespaces runs the same two commands for you (.devcontainer/).

1. Run Flagsmith with experimentation enabled

From VS Code: Petri: Start Self-Hosted Flagsmith (Docker). It writes selfhost/docker-compose.yml to the extension's storage, runs docker compose up -d in a terminal, waits for /health/liveness/, and points Petri at http://localhost:8000/api/v1.

Or run it yourself:

sh
docker compose -f selfhost/docker-compose.yml up -d
open http://localhost:8000   # create the first admin user

The file is Flagsmith's own Compose file trimmed to what experiments need:

Service Why
postgres Flags, and with USE_POSTGRES_FOR_ANALYTICS the SDK events too
migrate-db Runs migrations once before the others start
flagsmith API + dashboard on :8000, with EXPERIMENTATION_ENABLED: 'true'
task-processor Computes exposures and results in the background (TASK_RUN_METHOD: TASK_PROCESSOR)

The Compose file binds Flagsmith to 127.0.0.1 only. It reads DJANGO_SECRET_KEY and WAREHOUSE_CREDENTIALS_SECRET from the environment, falling back to a placeholder for local use; set real values (openssl rand -hex 32) before exposing it anywhere, behind a TLS proxy. The Codespaces setup generates random ones.

2. Store events for the environment

An environment needs a warehouse connection, or the events endpoint rejects exposures and trackEvent with a 404, and experiments never get results. Petri flags this as PETRI011. Its quick fix (or Petri: Store Experiment Events in Flagsmith) creates the built-in Postgres connection after you approve the request.

3. Point the SDK's events at your server

Setting apiUrl isn't enough. Server SDKs send events through a separate event processor whose URL defaults to Flagsmith Cloud. Petri flags this as PETRI012.

js
const flagsmith = new Flagsmith({
  environmentKey: process.env.FLAGSMITH_ENVIRONMENT_KEY,
  apiUrl: "http://localhost:8000/api/v1/",
  enableEvents: true,
  eventProcessorConfig: { eventsApiUrl: "http://localhost:8000/api/" },
});

Connecting Petri

  • Petri: Connect to Flagsmith: the API URL (http://localhost:8000) and an organisation API key (Organisation Settings → API Keys) or a user token. The credential goes into VS Code's SecretStorage.
  • Flagsmith CLI users: a repo bound with flagsmith init has a flagsmith.json with apiUrl, project and environment. Petri reads it and picks that environment automatically. If the CLI's credential variable is set (FLAGSMITH_API_KEY_localhost_8000 for self-hosted, FLAGSMITH_API_KEY for Cloud), Petri connects with no prompts.

Flagsmith Cloud works the same way with https://api.flagsmith.com. Petri's self-hosted checks (PETRI012) switch off automatically.

docs/self-hosting.mdView or edit on GitHub →