Skip to content

DocsGet started

Node.js quickstart

View as Markdown

From an API key to a first decision and outcome, in a free test environment. You need Node.js 22.12 or later and server-side code to call from.

1. Create a project and copy its test key

  1. Sign in at /signin/. If you don't have an account yet, the e-mailed sign-in link creates one.
  2. Name your organization (usually your company) and accept the Terms of Service.
  3. Create a project, such as "Checkout". It comes with two environments: live, for real customers, and test, for development and staging.
  4. You land on the project's API keys page. Under Test environment, give the key a label if you like (such as "laptop") and select Create key.
  5. Copy the key now: it's shown only once. It starts with zb_.

The test environment is free, and it learns separately from live, so test traffic never sways live decisions. Keep the key on your server, for example in an environment variable. Never ship it in a browser or app.

2. Install the SDK

The SDK is published to Zigbongo's npm registry on GitLab. Point the @zigbongo scope at it in your project's .npmrc, then install:

echo "@zigbongo:registry=https://gitlab.com/api/v4/projects/87446139/packages/npm/" >> .npmrc
npm install @zigbongo/sdk

No token is needed. Commit the .npmrc line with your project, so npm ci finds the SDK in CI and image builds too. To upgrade, run npm install @zigbongo/sdk@latest.

The SDK needs Node.js 22.12 or later and has no runtime dependencies. It's an ES module, and require("@zigbongo/sdk") works from CommonJS too. The client refuses to start where window exists, since it carries your key.

3. Configure it from the environment

Set ZIGBONGO_KEY where your server runs:

export ZIGBONGO_KEY="zb_..."

Create one client when your server starts, and reuse it for every request:

import { Zigbongo } from "@zigbongo/sdk";

const zb = new Zigbongo({
  apiKey: process.env.ZIGBONGO_KEY,
  baseUrl: "https://zigbongo.com",
});
await zb.warm();

warm() opens the client's connection to Zigbongo before your first visitor's request, so their decision doesn't wait for it, and checks the key. It never throws: an error goes to onError instead (see Fallbacks and timeouts).

4. Make your first decision

Pick a choice your product already makes. In this example, the checkout can offer an annual upgrade, and today it doesn't:

const s = zb.session("checkout", user.id);
const upsell = await s.decide("upsell", ["none", "annual"], "none");

if (upsell.option === "annual") {
  // show the annual upgrade
}
  • "checkout" is the flow. It's created the first time you ask about it, so there's nothing to register.
  • user.id is the subject: your own id for this customer, a string or an integer. Send the same id every time. An integer is sent as its text, so 42 and "42" are the same customer.
  • "upsell" is the decision point, ["none", "annual"] are its options, and "none" is the default, the option you show today.

decide() resolves to a decision:

Field What it holds
option The option to show.
arm optimised when Zigbongo chose, holdout when this customer keeps your defaults, paused when the flow is paused, fallback when the SDK fell back.
fallback true when the SDK returned your default because Zigbongo didn't answer in time or failed.
sessionId The session. s keeps it for its next calls.
decisionId This decision.

Zigbongo starts from your default: at first your customers mostly see what they see today, and traffic moves as the evidence builds.

decide() never throws. If Zigbongo doesn't answer within 150 ms, can't be reached or answers with an error, you get your default back with fallback: true, and the SDK reports the fallback in the background so that decision is never learned from. In a serverless function, call await zb.flush() before it returns, so the report goes out. To log what the SDK absorbed, pass an onError function to the client. The details are in Fallbacks and timeouts.

More decision points in one session

If the flow has more choices, call s.decide() at each one. s keeps the session id, so they all count as one session, however many there are. In its last argument, s.decide() also takes a result describing what happened at the previous decision point, such as the answer given: see Sessions and outcomes.

Context, if you have it

Send what you know about the customer as inputs, so each group of customers can get its own best option:

const upsell = await s.decide("upsell", ["none", "annual"], "none", {
  inputs: { plan: "starter", country: "LT", source: "newsletter" },
});

Inputs are flat: strings, numbers, booleans or null, raw or bucketed. They add up over the session. See Inputs.

5. Report the outcome

When the customer pays, report what it's worth. You can do it per session or per customer.

Per session, when the code that sees the payment still has the session:

await s.outcome(invoice.total, invoice.id);

Per customer, when it doesn't, such as in your payment provider's webhook or for a renewal a month later:

await zb.outcome({ outcomeId: invoice.id, flowKey: "checkout", subjectId: user.id, value: invoice.total });
  • value is a number: revenue, margin, or 1 for a renewal. Higher is better unless you change that in the flow's settings.
  • outcomeId is your own id for the outcome, a string or an integer, such as the invoice id. Reporting it again is harmless: nothing counts twice, and the call usually resolves to duplicate (it may read accepted when the repeat reaches another of Zigbongo's servers). It's unique across the environment, not per flow, so if you report one payment to two flows, give each report its own id.
  • An outcome counts when it happens within the flow's attribution window: one day from the session's start, by default. For renewals or churn, make the window longer in the flow's settings (up to 90 days).
  • outcome() resolves to accepted or duplicate. It retries timeouts, rate limits (429) and server errors, waiting as long as Zigbongo asks, up to 10 seconds in all, and throws a ZigbongoError otherwise. For example, its code is invalid_input for a flow key that isn't valid, such as one with a space in it. An outcome for a flow that has had no decision yet is accepted, and changes nothing.

Which one to use

Report per session when the outcome belongs to one run of the flow and you can carry that session to where the outcome happens, such as the purchase at the end of a checkout. It counts for that session only. In a later request, use the sessionId you kept from the decision (on the order, say):

await zb.outcome({ outcomeId: invoice.id, flowKey: "checkout", sessionId: order.zigbongoSessionId, value: invoice.total });

A decision that fell back before any session started has a sessionId of null: report that customer's outcome per customer instead. s.outcome() does this for you.

Report per customer when you only know who it was: a webhook, a renewal, a cancellation. The outcome counts towards each of that customer's sessions in the flow whose attribution window it falls in, so send the same subjectId you sent with the decision.

6. See it in the dashboard

Open the project's Flows page and switch it to Test. checkout is listed after its first decision, marked "optimizing". Open it to see:

  • how many sessions it has had, and when the report was last updated;
  • the value per session of the customers Zigbongo chooses for against the holdout's, with a 95% interval, once each side has enough sessions with an outcome (the page says how many it's waiting for);
  • each decision point's options, their share of traffic, and how often the SDK fell back.

Sessions whose attribution window is still open show as pending. On the API keys page, each key shows when it was last used, and by which SDK.

7. Switch to the live key

When it works in test:

  1. On the project's API keys page, under Live environment, create a key and copy it.
  2. Set ZIGBONGO_KEY to it on your production servers. The code doesn't change: the key decides the environment.

The live environment learns on its own, starting from your defaults, and its flows appear on their first live decision. Live sessions count towards your usage: 10,000 free every month, then €4 per 1,000, excluding VAT (see Pricing). Keep the test key for development and staging.

Next

For AI assistants and agents: every docs page is plain Markdown at its address with .md (this one is /docs/quickstart/node.md), and llms.txt lists them all.