Skip to content

API reference

Pulling a full catalog

Loop until next_cursor is null. Never loop on total — it is an approximate planner estimate on most endpoints, a display hint rather than a count. Where has_more is present it is exactly next_cursor !== null, so it is a convenience, not a second signal — and it is the one condition below that also holds on the two endpoints in the amber note further down that do not return has_more at all.

bash + jq — every brand, no gaps, no duplicates
#!/usr/bin/env bash
set -euo pipefail

CURSOR=""
PAGE=0
TOTAL=0

while :; do
  RESP=$(curl -sS -G "https://app.affilitera.com/api/v1/subnetwork/brands" \
    -H "Authorization: Bearer $AFFILITERA_API_KEY" \
    --data-urlencode "limit=200" \
    ${CURSOR:+--data-urlencode "cursor=$CURSOR"})

  COUNT=$(echo "$RESP" | jq '.data | length')
  PAGE=$((PAGE + 1)); TOTAL=$((TOTAL + COUNT))
  echo "page $PAGE: $COUNT brands (running total $TOTAL)" >&2

  echo "$RESP" | jq -c '.data[]'

  CURSOR=$(echo "$RESP" | jq -r '.next_cursor // empty')
  [ -z "$CURSOR" ] && break        # <-- the ONLY correct termination condition
done

echo "done: $TOTAL brands over $PAGE pages" >&2
node — the same loop, with 429 and 503 handled
const BASE = "https://app.affilitera.com/api/v1";
const KEY = process.env.AFFILITERA_API_KEY;

async function* brands(params = {}) {
  let cursor = null;
  do {
    const qs = new URLSearchParams({ limit: "200", ...params });
    if (cursor) qs.set("cursor", cursor);

    const res = await fetch(`${BASE}/subnetwork/brands?${qs}`, {
      headers: { Authorization: `Bearer ${KEY}` },
    });

    // 429 (throttled) and 503 (transient read timeout) both carry Retry-After
    // and both mean "retry this same page". A 500 does not — that one is a real fault.
    if (res.status === 429 || res.status === 503) {
      const wait = Number(res.headers.get("Retry-After") ?? 2);
      await new Promise((r) => setTimeout(r, wait * 1000));
      continue;
    }
    if (!res.ok) {
      const body = await res.json();
      throw new Error(
        `${body.error.code}: ${body.error.message} (request_id ${body.error.request_id})`,
      );
    }

    const page = await res.json();
    if (page.limit_clamped) {
      console.warn(`limit clamped to ${page.limit} (max ${page.limit_max})`);
    }
    yield* page.data;
    cursor = page.next_cursor;
  } while (cursor !== null);
}

let n = 0;
for await (const brand of brands({ has_products: "true" })) n++;
console.log(`pulled ${n} brands`);