# Creatives sync

> **TL;DR** — Pull approved ad creatives incrementally: pass your last checkpoint as `updated_since`, follow `next_cursor` to the end, overwrite records by `creative_id`, and move the checkpoint only after the whole run succeeded. Re-check a stored creative with `GET /creatives/{id}`; stop using it after the UTC day of `rights.ends_at`.

## Before you start
- A key with `creatives:export` for the job, in `WANDURA_API_KEY`. The marketing job never needs `playback`.
- The record and the envelope: [Creatives](https://docs.wandura.ai/developers/concepts/creatives).

## The procedure
1. **First run:** `GET /creatives?limit=100` without `updated_since`; while `data.next_cursor` is not `null`, call again with `cursor=<next_cursor>`.
2. **Upsert** every record by `creative_id`. A record can come twice (`updated_at` is rounded down to the second, and `updated_since` is strict): overwrite it.
3. **Checkpoint:** after the run **completed**, save the largest `updated_at` you received.
4. **Later runs:** the same, with `updated_since=<checkpoint>` on every page of the run.
5. **On failure** (including `422` `4222`, a cursor no longer valid): keep the old checkpoint; the next run starts again from it. Nothing is skipped.
6. **Withdrawn creatives** are not announced. Before you use a stored creative, or once a day, call `GET /creatives/{id}`: `404` `4040` means stop using it.
7. **Rights:** usable until the end of the UTC day `rights.ends_at` points at (`ends_at + 86400`).

A few runs an hour is well within the creatives limit.

## How the creative was approved
`status: approved` alone does not mean an admin reviewed the creative. The `approval` field says which way it got there:

| `approval` | Meaning |
|---|---|
| `self_published` | Its maker published it directly with Submit & publish, without review. |
| `self_approved` | Its maker approved it with their own admin rights; no second person is on record as having reviewed it. |
| `reviewed` | Someone other than its maker approved it. |

It never names who approved.

## Policy scan status
> **It is a real scan result only when `rubric` is not `null`, does NOT start with `derive-`, and `stale` is `false`. A `rubric` starting with `derive-` = a preliminary result, `level` 2–4: scan for real before launching a campaign. `rubric` = `null` = no result yet, `level` 3 is an assigned default.**

Every record carries a `policy` block with exactly three fields: a summary of the latest Meta ad-policy scan of the creative on Wandura (a scan of its draft preview, started by a member of the owning organisation on the partner portal or over the API). It never contains the individual findings; a member of the owning organisation sees those on the creative's page (`links.view`).

| Field | Value |
|---|---|
| `level` | Always a number, 0 to 4. A real scan: its level, or `2` when the scan finished but could not check part of the creative. A preliminary result: the derived level, never below `2` (never below `3` when `rubric` ends in `-src`). No result: the assigned default `3`. |
| `rubric` | The rule set the result was scored with. A real scan: e.g. `meta70-2026-10-28+axb-2026-10-29`. A preliminary result: the derivation version, always starting with `derive-` (e.g. `derive-2026-11-17`); the `derive-` prefix is part of the contract, the rest is an opaque label, except a trailing `-src` (e.g. `derive-2026-11-17-src`): derived from the source footage only. `null` when there is no result: never scanned, scanning now, the scan failed or its result cannot be read, and no preliminary result. |
| `stale` | `true` when Wandura's real scan of the creative no longer holds: the creative was edited after the scan, or the scan was scored on a different scale from today's L0–L4 (an older scan engine). `level` and `rubric` are then the preliminary result of the current edit if there is one, otherwise `3` with the rule set of the older scan. A later adjustment of the rules on the same scale does not make a result stale. Always `false` when `rubric` is `null`. |

**Preliminary results** are derived from the scans of the episodes and clips the creative uses, without scanning the creative itself: free, computed in the background for approved creatives without a usable real scan (and again when the creative changes), less exact than a real scan, so they never show below level 2. When the creative has added text or images, those are not checked: the result comes from the episode and clip footage only, `rubric` ends in `-src`, and it never shows below level 3.

| `level` | Meaning | Recommendation |
|---|---|---|
| 0–1 | Likely to pass | Usable. |
| 2 | Needs a review | Review the flagged moments. |
| 3 | Likely to be rejected | Think carefully, or scan it yourself again, before launching a campaign. |
| 4 | Likely to be rejected, serious issue | Better not used. |

These are **recommendations only: Wandura does not block anything**, and the scan itself is advisory.

A scan does not change `updated_at`, so an incremental run with `updated_since` does not bring a new scan result. Before you rely on `policy` (for example right before launching a campaign), read the creative with `GET /creatives/{id}`, the same call as in step 6. Reading `policy` is free and never starts a scan.

## Code
```ts
export async function syncCreatives(store: CreativeStore, o: CallOptions = {}): Promise<boolean> {
  const since = await store.checkpoint();
  let cursor: string | null = null;
  let max = since ?? 0;
  try {
    do {
      const qs = new URLSearchParams({ limit: '100' });
      if (since !== null) qs.set('updated_since', String(since)); // the same value on every page of a run
      if (cursor) qs.set('cursor', cursor);
      const { data }: { data: Envelope } = await call<Envelope>('GET', `/api/delivery/v1/creatives?${qs}`, { policy: BACKGROUND, ...o });
      for (const c of data.data.items) { await store.upsert(c); max = Math.max(max, c.updated_at); }
      cursor = data.data.next_cursor;
    } while (cursor);
  } catch (e) {
    // 422 / code 4222 (cursor no longer valid) or anything else: the next run restarts from the old checkpoint.
    log('creatives_sync_failed', { status: (e as { status?: number }).status, code: (e as { errorCode?: string }).errorCode });
    return false;
  }
  await store.saveCheckpoint(max); // only after the whole run: nothing is ever skipped
  return true;
}
```

## Done when
- [ ] A second run with no changes on Wandura's side returns nothing new.
- [ ] A run interrupted half-way does not move the checkpoint.
- [ ] A creative past its rights day is no longer used.

## Related
- [List creatives](https://docs.wandura.ai/developers/reference/creatives-list), [Get a creative](https://docs.wandura.ai/developers/reference/creatives-get)
