# Sellalog API — guide for agents

You are an agent automating a **Sellalog** store (wholesale apparel catalogs) through its REST API.
This guide is enough to work without reading anything else; every endpoint's details are in the OpenAPI spec.

- **Base URL:** `https://api.sellalog.com/api`
- **OpenAPI (JSON):** `https://api.sellalog.com/api/openapi.json` · docs: `https://api.sellalog.com/api/public-docs/`
- **This document:** `https://sellalog.com/api-agents.md` · LLM index: `https://sellalog.com/llms.txt`

## 1. Authentication

Every request carries `Authorization: Bearer sk_live_…`.

The store owner creates the key in **Sellalog → Settings → Integrations → API keys → Create key** (**Pro** plan).
It is shown once: ask the user for it and keep it as a secret, never in code or in a message. A key acts as an ADMIN
of **its** store and of no other.

```bash
curl https://api.sellalog.com/api/catalogs -H "Authorization: Bearer $SELLALOG_API_KEY"
```

## 2. Conventions

- JSON both ways (`Content-Type: application/json`), UTF-8. Dates are ISO 8601 (UTC).
- Money: a number in the store currency (`GET /tenant/me` → `currency`) unless a `currency` field says otherwise.
- **Your own id (`externalRef`)**: catalogs and products take an `externalRef` (1–120 of `A-Z a-z 0-9 . _ - : @ ~`,
  unique per store). `PUT /catalogs/by-ref/{ref}` and `PUT /products/by-ref/{ref}` create the first time (201,
  header `X-Upsert: created`) and update afterwards (200, `X-Upsert: updated`). **Always use it**: re-running a job
  never creates duplicates.
- **Idempotency-Key** (optional) on POST create endpoints: same key + same body within 24 h = the same answer
  (`Idempotent-Replayed: true`), nothing created twice. Same key with another body → 422.
- Lookups: `GET /catalogs/by-slug/{slug}`, `GET /catalogs/by-ref/{ref}`, `GET /products/by-ref/{ref}`,
  `GET /products?sku=`, `GET /catalogs?externalRef=`.
- Pages: `GET /orders` → `page` and `limit` (max 200) with `total` and `hasMore`. `GET /products` → `limit`
  (max 500) and `offset`; the total is in the `X-Total-Count` header (the body stays a list).

## 3. Errors

Always `{ "error": "English message", "code": "STABLE_CODE" }`. Decide on `code`, never on the message.

| HTTP | code | What to do |
|---|---|---|
| 400 | `VALIDATION` (+ `issues`), `EXTERNAL_REF_INVALID`, `CATALOG_INVALID` | Fix the body; don't retry as is |
| 401 | `AUTH_REQUIRED`, `API_KEY_INVALID` | Missing, mistyped or revoked key: tell the user |
| 403 | `PLAN_FEATURE_REQUIRED`, `PLAN_LIMIT_PRODUCTS`, `PLAN_LIMIT_CATALOGS`, `FORBIDDEN`, `API_KEY_NOT_ALLOWED` | Plan limit or permission: tell the user, don't retry |
| 404 | `*_NOT_FOUND`, `ROUTE_NOT_FOUND` | The id isn't this store's, or the route doesn't exist |
| 409 | `EXTERNAL_REF_TAKEN`, `CATALOG_SLUG_IN_TRASH`, `CATALOG_REF_IN_TRASH`, `DUPLICATE` | Conflict: find the existing one and update it |
| 422 | `IDEMPOTENCY_KEY_REUSED` | Use another Idempotency-Key for another body |
| 429 | `RATE_LIMITED` | Wait `Retry-After` seconds and retry |
| 5xx | `INTERNAL` | Retry with growing waits (1 s, 2 s, 4 s…) |

## 4. Limits

**1200 requests per minute per key.** Over it: 429 `RATE_LIMITED` with `Retry-After` (seconds) and the
`RateLimit` / `RateLimit-Policy` headers. Keep at most 3 requests in flight. The plan caps how many products and
catalogs the store may have (403 `PLAN_LIMIT_*` with `limit` and `current`).

## 5. Recipes

### a) One catalog per customer from a spreadsheet (CSV or Excel)

The sheet has a `name` column (and, if available, the customer's own id). Each customer gets a catalog, out of the
store menu, with the products of a base catalog at their own %.

1. Find the base catalog: `GET /catalogs/by-slug/{slug}` (or `GET /catalogs`) → keep its `id`.
2. For each row build a stable `ref`: the customer id, or the name lowercased with dashes
   (`"North Store"` → `customer-north-store`).
3. Create or update their catalog:

```http
PUT /catalogs/by-ref/customer-north-store
{ "name": "North Store", "showInMenu": false }
```

   201 (new) or 200 (existed) with the catalog: keep `id` and `url` (its public address). Without `slug` one is
   made from the name. To let in approved buyers only: `"visibility": "private"`.
4. Link the base catalog's products at the customer's % (repeating it only updates the rule):

```http
POST /catalogs/{id}/links
{ "sourceCatalogId": "<base id>", "all": true, "linkAll": true, "percent": 10, "rounding": "step", "roundStep": 100 }
```

   `percent` may be negative (a discount). `linkAll: true` = new products of the base show up by themselves.
   `rounding`: `none` | `step` (round up to `roundStep`) | `99`.
5. At the end report: customers created, updated and failed (row + `code`).

### b) Create or update products

```http
PUT /products/by-ref/art-1001
{ "name": "Basic tee", "price": 12.5, "sku": "BT-1001", "catalogRef": "base", "stock": 40,
  "images": ["https://…/tee.jpg"], "description": "Combed cotton" }
```

- Creating needs `name` and `price`; updating is partial (only what you send changes).
- The catalog goes as `catalogId` or `catalogRef` (a catalog's `externalRef`). With neither: the main catalog.
- `stock`: a number; `-1` = stock not tracked (still orderable). `active: false` hides it.
- Photos: public URLs, or upload first with `POST /upload` (multipart, field `images`) and use the URLs it returns.
- Sizes and tags use ids: `GET /sizes`, `GET /tags` (create missing ones with `POST /sizes` / `POST /tags`).
- Many products at once: `PATCH /products/bulk` (`ids` + `updates`, or `pricePercent`).

### c) Price links per customer

A link opens a catalog and shows its prices to whoever gets it (useful when the catalog hides prices).

1. Before creating, check it isn't there yet: `GET /price-links?externalRef=link-north-store`.
2. If it isn't:

```http
POST /price-links
Idempotency-Key: link-north-store-2026-12
{ "catalogId": "<the customer's catalog id>", "clientName": "North Store", "clientPhone": "+15550000000",
  "externalRef": "link-north-store", "minutes": 43200 }
```

   201 with `url`, `expiresAt`, `shareText` and `whatsappUrl` ready to send. `minutes` max 259200 (180 days).
   To extend it: `PATCH /price-links/{id}` with `minutes` or `expiresAt`. Don't send messages yourself: hand the
   link to the user.

### d) Read new orders

Poll every few minutes, oldest first, from the date of the last order you processed:

```http
GET /orders?since=2026-12-01T00:00:00Z&sort=asc&limit=200&page=1
```

- Go on with `page=2, 3…` while `hasMore` is `true`. Keep the last `createdAt` as your next `since`; skip the
  `id`s you already processed (`since` includes that instant).
- Each order has `number`, `status`, `customerName`, `customerPhone`, `items` (`sku`, `name`, `qty`, `price`),
  `total` and `currency`. Details: `GET /orders/{id}`.
- Change the status: `PUT /orders/{id}/status` with `{ "status": "CONFIRMED" }`
  (RECEIVED, PENDING, CONFIRMED, PREPARING, READY, SHIPPED, DELIVERED, CANCELLED).
- Instead of polling you can register a webhook: `PUT /webhooks` (events signed with `X-Sellalog-Signature`).

## 6. Rules

- Work only on the key's store. Never delete catalogs or products unless the user asks.
- Try 1 or 2 rows first and show the result before processing the whole sheet.
- Never put the key in URLs, logs or answers.
