# HTTP quickstart

> From an API key to a first decision and outcome with plain HTTPS requests, for any language without an SDK. You also build the timeout and fallback that the SDKs give you.

## 1. Create a project and copy its test key

1. Sign in at [/signin/](https://zigbongo.com/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: every call to
Zigbongo comes from your server.

```sh
export ZIGBONGO_KEY="zb_..."
```

## 2. Know the conventions

Every call goes to `https://zigbongo.com/v1/...` with these headers:

| Header | |
|---|---|
| `Authorization` | `Bearer $ZIGBONGO_KEY`. Without a valid key, the answer is `401`. |
| `Content-Type` | `application/json`. Every body is JSON. |
| `Idempotency-Key` | On decide calls: a new unique value for each call, such as a UUID (1 to 200 visible ASCII characters). |

A field the service doesn't know is refused with `400 invalid_input`; only `result` takes fields of your own. Every
answer carries an `X-Request-Id` header, which identifies the request if you ever need to ask about it.

## 3. 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. Ask which option to show with `POST /v1/flows/{flowKey}/decide`:

```sh
curl https://zigbongo.com/v1/flows/checkout/decide \
  -H "Authorization: Bearer $ZIGBONGO_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: 5b0e7c2a-3f61-4d8e-9a47-1c2b3d4e5f60" \
  -d '{
    "subjectId": "user_1842",
    "node": "upsell",
    "options": [{ "key": "none" }, { "key": "annual" }],
    "default": "none"
  }'
```

`checkout` is the flow. It's created by this first call, so there's nothing to register. Its key is 1 to 128
letters, digits, `-`, `_` or `.`. The body:

| Field | |
|---|---|
| `subjectId` | Your own id for this customer, as a string. Required to start a session. On later calls of a session it's optional, and must match. |
| `sessionId` | Leave it out on a session's first call. Send the `sessionId` you got back on its later calls. |
| `node` | The decision point. |
| `options` | The options, as objects with a `key` each. Keys are unique and not empty. |
| `default` | The option you show today: one of the keys. |
| `inputs` | Optional. Context about the customer, as a flat object of strings, numbers, booleans or `null`, such as `{ "plan": "starter", "country": "LT" }`. Inputs add up over the session; see [Inputs](https://zigbongo.com/docs/inputs.md). |
| `holdoutPercent` | Optional. From 0 to 100: the share of customers who always get the default at this decision point. Zigbongo remembers it for the decision point until you send another. 5 if never sent. |
| `result` | Optional, on a session's later calls: what happened at its previous decision, as an object with that decision's `decisionId` and any fields of your own, such as `"answer": "yes"`. |

The answer is `200` with the option to show:

```json
{
  "sessionId": "0f8b6a52-9c1d-4e3a-8b7f-2a6c5d4e3f21",
  "decisionId": "7d2e9f41-6b3a-4c5d-9e8f-1a2b3c4d5e6f",
  "option": "none",
  "arm": "optimised"
}
```

| Field | |
|---|---|
| `sessionId` | The session. Send it with the session's later calls, and with outcomes per session. |
| `decisionId` | This decision. Send it in the next call's `result`. |
| `option` | The option to show. |
| `arm` | `optimised` when Zigbongo chose, `holdout` when this customer keeps your defaults, `paused` when the flow is paused. |

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

If Zigbongo receives the same `Idempotency-Key` again within 30 seconds, it answers with the decision it already made
and changes nothing. A repeat that reaches another of its servers is decided again, but Zigbongo then learns from
neither and counts the session once. The key is also how a fallback report finds its decision (see step 5).

### More decision points in one session

If the flow has more choices, call `decide` at each one with the session's `sessionId`. They all count as one
session, however many there are. If you like, `result` reports what happened at the previous decision:

```sh
curl https://zigbongo.com/v1/flows/checkout/decide \
  -H "Authorization: Bearer $ZIGBONGO_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: 9c4f1e2d-8a7b-4c6d-b5e4-3f2a1b0c9d8e" \
  -d '{
    "sessionId": "0f8b6a52-9c1d-4e3a-8b7f-2a6c5d4e3f21",
    "node": "addon",
    "options": [{ "key": "none" }, { "key": "priority_support" }],
    "default": "none",
    "result": { "decisionId": "7d2e9f41-6b3a-4c5d-9e8f-1a2b3c4d5e6f", "accepted": false }
  }'
```

See [Sessions and outcomes](https://zigbongo.com/docs/sessions-and-outcomes.md).

## 4. Report the outcome

When the customer pays, report what it's worth with `POST /v1/outcomes`. **Per session**:

```sh
curl https://zigbongo.com/v1/outcomes \
  -H "Authorization: Bearer $ZIGBONGO_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "outcomeId": "inv_10023",
    "flowKey": "checkout",
    "sessionId": "0f8b6a52-9c1d-4e3a-8b7f-2a6c5d4e3f21",
    "value": 49.0
  }'
```

Or **per customer**, with `subjectId` in place of `sessionId`:

```json
{
  "outcomeId": "inv_10023",
  "flowKey": "checkout",
  "subjectId": "user_1842",
  "value": 49.0
}
```

| Field | |
|---|---|
| `outcomeId` | Your own id for the outcome, as a string, such as the invoice id. It's unique across the environment, not per flow: if you report one payment to two flows, give each report its own id. |
| `flowKey` | The flow. It isn't checked against your flows: an outcome for a flow key Zigbongo doesn't know is accepted and changes nothing, so check the spelling. |
| `sessionId` or `subjectId` | Exactly one of them: the session, or your id for the customer. |
| `value` | A number: revenue, margin, or 1 for a renewal. Higher is better unless you change that in the flow's settings. |
| `at` | Optional. When it happened, as an RFC 3339 time such as `2026-10-11T09:30:00Z`, from 400 days ago to a day ahead. Now, if left out. |

The answer is `200`:

```json
{ "outcomeId": "inv_10023", "status": "accepted" }
```

Reporting the same `outcomeId` again is harmless: nothing counts twice, and the answer's `status` is usually
`duplicate` (a repeat that reaches another of Zigbongo's servers, or comes over 10 minutes later, may read
`accepted`). So
when an outcome call times out or gets a `429` or `5xx` answer, send it again, after the number of seconds in the
answer's `Retry-After` header if it has one. The SDKs try three times, waiting 200 ms and then 400 ms, or longer when
Zigbongo asks. See [Retries and rate limits](https://zigbongo.com/docs/retries-and-rate-limits.md). To send many outcomes at once, use
`POST /v1/outcomes:batch` ([API reference](https://zigbongo.com/docs/api.md)).

### Per session or per customer

Report **per session** when the outcome belongs to one run of the flow and you can carry its `sessionId` to where the
outcome happens, such as the purchase at the end of a checkout. It counts for that session only.

Report **per customer** when you only know who it was: a payment 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.

The attribution window is one day from the session's start, by default. For renewals or churn, make it longer in the
flow's settings (up to 90 days).

## 5. Fall back when Zigbongo is slow

Nobody should wait on Zigbongo for long. The SDKs give every decide call 150 ms; if there's no answer by then, or it
fails, they show your default and tell Zigbongo, so that decision is never learned from. With plain HTTP, you build
this yourself:

1. **Give each decide call a time limit.** The SDKs use 150 ms. Reuse connections where your HTTP client can, so a
   new connection doesn't take up the time.
2. **On a timeout, a network error or any answer but `200`, show your default.** That includes `429` and `5xx`.
   Don't retry the decide call after a timeout or an answer: the SDKs don't either. One failure is worth sending
   again, once, at once and with the same `Idempotency-Key`: a connection that closed before any answer, as a server
   or proxy retiring a reused connection does. The SDKs do that within the same 150 ms.
3. **Report the fallback, in the background,** with `POST /v1/fallbacks`. `requestId` is the `Idempotency-Key` of the
   decide call that fell back:

   ```sh
   curl https://zigbongo.com/v1/fallbacks \
     -H "Authorization: Bearer $ZIGBONGO_KEY" \
     -H "Content-Type: application/json" \
     -d '{
       "flowKey": "checkout",
       "requestId": "5b0e7c2a-3f61-4d8e-9a47-1c2b3d4e5f60",
       "node": "upsell",
       "reason": "timeout"
     }'
   ```

   `node` and `reason` are optional; the SDKs send reasons such as `timeout`, `network` and `http_503`. If the decision
   reached Zigbongo, now or after the report, it's never learned from. The answer is `{ "status": "recorded" }`, or
   may be `{ "status": "duplicate" }` for a report already made.

4. **Carry on without that decision.** Don't send a `result` for it. If it was the session's first call, you have no
   `sessionId`: the next call starts a new session with `subjectId`, and that customer's outcome goes per customer.

The flow's page in the dashboard shows how often each decision point fell back. More in
[Fallbacks and timeouts](https://zigbongo.com/docs/fallbacks-and-timeouts.md).

## 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 it 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.

## 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 requests don'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](https://zigbongo.com/pricing/)). Keep the test key for development and staging.

## Errors

An error answer has a status and a JSON body such as
`{ "error": { "code": "invalid_input", "message": "default upgrade is not one of the options" } }`. The ones you're
most likely to meet:

| Status | `code` | When |
|---|---|---|
| 400 | `invalid_input` | A field is missing, malformed or unknown, such as a `default` that isn't one of the options. |
| 401 | `unauthorized` | The key is missing, wrong or revoked. |
| 404 | `not_found` | An unknown session or decision. |
| 409 | `decision_already_reported` | A `result` that differs from the one already reported for that decision. |
| 413 | `body_too_large` | A body over 2 MiB. |
| 429 | `rate_limited` | Over your rate limit. `Retry-After` says how many seconds to wait. |
| 503 | `starting`, `timeout` or `unavailable` | Zigbongo is starting, busy or briefly unavailable. `Retry-After` says when to try again. |

On a decide call, every one of them means: show your default. The [HTTP API reference](https://zigbongo.com/docs/api.md) lists every call,
field and error.

---

The Markdown of https://zigbongo.com/docs/quickstart/http/. Every docs page: https://zigbongo.com/llms.txt
