# Read results back

```bash
npx @sparkletree/cli results 9b8e7d6c-5a4f-4e3d-8c2b-1a0f9e8d7c6b --days 30 --json
```

```ts
import { getResults } from "@sparkletree/core/server";

const { ok, results, reason } = await getResults({
  apiKey: process.env.SPARKLETREE_API_KEY!, // a secret workspace API key, st_…
  campaignId: "9b8e7d6c-5a4f-4e3d-8c2b-1a0f9e8d7c6b",
  days: 30,
});
```

[Impressions and clicks](https://docs.sparkletree.io/developers/sdk/analytics.md) covers what the SDK
sends. This page covers reading the outcome back: how many impressions,
clicks and conversions each variant got, which one leads, and which one
leads in each context.

## Server only

Results need a secret credential: a workspace API key (`st_…`, created in
Settings) or a CLI sign-in. Never send either to a browser. A publishable key
(`st_pk_…`) cannot read results.

- `getResults` is in `@sparkletree/core/server`. Called in a browser (a page,
  a Web Worker or a service worker) it makes no request and resolves to
  `{ ok: false, reason: "browser" }`, with a warning in development.
- The route sends no CORS headers, so a page could not read the answer
  anyway.
- `sparkletree results` uses the sign-in from `sparkletree login` (or
  `SPARKLETREE_TOKEN`). See [the CLI](https://docs.sparkletree.io/developers/sdk/cli.md#results).

Both read from the app, `https://app.sparkletree.io`, not from the API base
your pages use: `GET /api/embed/v1/results?campaign=<id>&days=<1-90>`. The
workspace always comes from the credential; a campaign of another workspace
answers 404.

The limit is 60 requests a minute per credential. Beyond it the answer is
429 (`reason: "rate_limited"`); wait a minute.

## What comes back

```json
{
  "results": 1,
  "campaign": { "id": "9b8e7d6c-5a4f-4e3d-8c2b-1a0f9e8d7c6b", "name": "Autumn firing", "status": "active" },
  "window": { "start": "2026-10-20", "end": "2026-11-19", "days": 30 },
  "totals": { "impressions": 4120, "clicks": 310, "conversions": 22, "ctr": 0.0752 },
  "leader": { "variantId": "2e3d4c5b-6f7a-4b8c-9d0e-1f2a3b4c5d6e" },
  "variants": [
    {
      "variantId": "2e3d4c5b-6f7a-4b8c-9d0e-1f2a3b4c5d6e",
      "name": "Warm kiln",
      "copy": { "headline": "Fresh from a warm kiln", "cta": "Shop mugs" },
      "impressions": 2100, "clicks": 190, "conversions": 15, "ctr": 0.0905,
      "score": 0.71, "share": 0.5097, "isLeader": true
    },
    {
      "variantId": "1d2c3b4a-5e6f-4a7b-8c9d-0e1f2a3b4c5d",
      "name": "Plain",
      "copy": { "headline": "Stoneware, fired slow", "cta": "Shop mugs" },
      "impressions": 2020, "clicks": 120, "conversions": 7, "ctr": 0.0594,
      "score": 0.29, "share": 0.4903, "isLeader": false
    }
  ],
  "contexts": [
    {
      "dimension": "timeOfDay", "value": "evening", "impressions": 900,
      "leaderVariantId": "1d2c3b4a-5e6f-4a7b-8c9d-0e1f2a3b4c5d",
      "variants": [
        { "variantId": "1d2c3b4a-5e6f-4a7b-8c9d-0e1f2a3b4c5d", "impressions": 460, "clicks": 52, "ctr": 0.113 },
        { "variantId": "2e3d4c5b-6f7a-4b8c-9d0e-1f2a3b4c5d6e", "impressions": 440, "clicks": 31, "ctr": 0.0705 }
      ]
    }
  ],
  "site": { "pageViews": 800, "clicks": 40, "conversions": 3 },
  "minSample": 30,
  "generatedAt": "2026-11-19T10:00:00.000Z"
}
```

Rates are fractions: `0.0752` is 7.52 %. The figures are the ones the
dashboard's Insights shows for the same window.

| Member | |
|---|---|
| `campaign` | `id`, `name` and `status` (the campaign's status in the dashboard, such as `active`). |
| `window` | The dates covered, `start` to `end` (UTC), and `days`. |
| `totals` | `impressions`, `clicks`, `conversions` and `ctr` (clicks over impressions) across all variants. |
| `leader` | `{ variantId }`, or null when no variant has enough data. |
| `variants` | One entry per variant, in the dashboard's ranking: by score, then impressions, then CTR. `sparkletree results` lists the leader first. |
| `variants[].copy` | The variant's `headline`, `body` and `cta`, each present only when the variant has it. |
| `variants[].score` | The variant's performance score, 0 to 1, updated hourly. 0 when it has not been scored yet. |
| `variants[].share` | The variant's share of the campaign's impressions, 0 to 1. |
| `variants[].isLeader` | True for the leader. |
| `variants[].archived` | Present, and true, only on a variant that was archived. Its figures stay. |
| `contexts` | Which variant leads in each context; see below. |
| `site` | The activity your pages sent with `track` for this campaign: `pageViews`, `clicks`, `conversions`. Null when there is none. |
| `minSample` | The impressions a variant needs before it can lead, overall or in a context: 30. |
| `generatedAt` | When the answer was made. |

### What "leader" means

The leader is the first variant in the ranking (score, then impressions, then
CTR) that has at least `minSample` impressions in the window. With fewer, no
variant leads and `leader` is null; a variant with 20 impressions and a high
CTR has not shown anything yet.

### Contexts

Each entry is one value of one dimension the analytics record with an
impression, with each variant's `impressions`, `clicks` and `ctr` there:

| `dimension` | |
|---|---|
| `timeOfDay` | Such as `morning` or `evening`. |
| `device` | The visitor's device type. |
| `weather` | The weather at the visitor's location. |
| `locationType` | The kind of place. |
| `locale` | The visitor's language. |
| `placement` | Where on the page or screen the copy ran. |
| `audienceSegment` | The audience segment. |
| `vibe_mood`, `vibe_energy`, `vibe_density`, `vibe_motion` | The mood signals; energy, density and motion are bucketed as `high`, `medium` or `low`. |
| `surfaceId`, `zoneId` | The surface or zone that served it. |

`leaderVariantId` is the variant with the best CTR among those with at least
`minSample` impressions in that context, or null when none has that many. A
value with fewer than `minSample` impressions in total is left out. At most
500 contexts come back, the ones with the most impressions.

## `getResults`

| Option | |
|---|---|
| `apiKey` | Required. A secret `st_…` workspace API key, or an `stcli_…` CLI token. Sent as `x-api-key`. |
| `campaignId` | Required. |
| `days` | 1 to 90; a value outside is clamped. Defaults to 30 on the server. |
| `appBase` | Default `https://app.sparkletree.io`. Must be `https` (`http` for `localhost` only). |
| `signal` | An `AbortSignal`. |
| `fetchImpl` | A `fetch` to use instead of the platform's, for tests. |

It never rejects. It resolves to `{ ok, results, reason?, status? }`:
`results` is the document above when `ok` is true and null otherwise. No
request follows a redirect.

| `reason` | |
|---|---|
| `browser` | Called in a browser. No request was made. |
| `config` | No `apiKey` or no `campaignId`. No request was made. |
| `insecure_app_base` | `appBase` is not `https`. No request was made. |
| `unauthorized` | 401: the key is missing, unknown, disabled, expired or revoked. |
| `forbidden` | 403. |
| `not_found` | 404: the campaign is not in the key's workspace. |
| `rate_limited` | 429: more than 60 requests a minute. |
| `network`, `aborted`, `http_error`, `malformed` | No answer, cancelled, another error status, or an answer that is not a results document. |

## Worked example: an agent improves the evening copy

An agent with a terminal and the SparkleTree MCP tools can close the loop:
read the results, find where a variant loses, and change its copy.

1. **Read the results.**

   ```bash
   npx @sparkletree/cli results 9b8e7d6c-5a4f-4e3d-8c2b-1a0f9e8d7c6b --json
   ```

2. **Find the context where the overall leader does not lead.** In the
   document above, "Warm kiln" leads overall (`leader`), but in
   `timeOfDay: evening` the `leaderVariantId` is "Plain", with a CTR of 0.113
   against 0.0705. Both have more than `minSample` impressions there, so the
   difference is worth acting on.

3. **Read what each variant says.** `variants[].copy` has the headline and
   CTA of both, so the agent can see what "Plain" says that works in the
   evening.

4. **Change the copy through the MCP tools.** Read the variant first with
   `query_campaigns` (`action: "variants"`), then write the new headline with
   `manage_campaigns`:

   ```json
   {
     "action": "update_variant",
     "campaignId": "9b8e7d6c-5a4f-4e3d-8c2b-1a0f9e8d7c6b",
     "variantId": "2e3d4c5b-6f7a-4b8c-9d0e-1f2a3b4c5d6e",
     "contentStructure": { "headline": "Stoneware for slow evenings" }
   }
   ```

   The fields in `contentStructure` are merged into the variant's content.
   This changes what visitors read, so show the change to a person and get
   their approval before you send it.

5. **Read the results again later** with the same command and compare the
   `evening` entry over the new window.

To connect an agent to the MCP tools, see
[Install and connect](https://docs.sparkletree.io/developers/agents/install.md).
