# The SDK

> The four parts of @sypnia/cases, and how to parse, validate and change maps from code.

Applies to: the Sypnia engine (open source, self-hosted) and Sypnia Cloud (paid service).  
Source: https://cases.docs.sypnia.com/tools/sdk/

For agents: import { parseMap, validateMap, diffMaps, checkMap, mapSchema, navigationSchema, caseSchema, eventSchema } from '@sypnia/cases/spec' (works offline on files; validateMap → { ok, errors: [{ file, path, line, message, kind }], findings }; map.navigation.pathOf(place), map.events() → nodes, edges, cycles); import { createClient, CasesApiError } from '@sypnia/cases/client' with createClient({ token (Sypnia token or Cases project token syp_cpt_), baseUrl?, map?, headers?(method, url) for DPoP }): me, workspaces, maps (get, create, update, diff, navigation, place, find, events, linkData, linkTelemetry, upload, audit), versions.list(map), proposals (list, get, create(map, {title, reason, changes, reviewers}), requestReview, approve, close, comments, comment), checks (run, last), cases (get, search, links, comments, comment), deliveries.list, deadLetters (list, replay, drop), tokens (list, create, revoke). Errors: CasesApiError { status, code, errors[], retryAfter }.

```bash
npm install @sypnia/cases
```

| Import                  | For                                                                                                                                                                              |
| ----------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `@sypnia/cases/sypnorm` | Sypnorm, the use-case ORM, and the runtime inside each area's service ([Sypnorm](https://cases.docs.sypnia.com/sypnorm), [Running cases](https://cases.docs.sypnia.com/runtime)) |
| `@sypnia/cases/map`     | derive and upload the map from code ([The map from your code](https://cases.docs.sypnia.com/sypnorm/the-map))                                                                    |
| `@sypnia/cases/spec`    | the format, offline (below)                                                                                                                                                      |
| `@sypnia/cases/client`  | the Cases API (below)                                                                                                                                                            |

## `@sypnia/cases/spec`: the format, offline

```ts
import { parseMap, validateMap, diffMaps } from '@sypnia/cases/spec';

const map = await parseMap('./cases');           // reads map.yaml, navigation.yaml and every case file
const { ok, errors } = validateMap(map);         // [{ file, path, line, message, kind }]
const changes = diffMaps(previous, map);         // added, removed and changed cases, field by field
const path = map.navigation.pathOf('shop.order-for-tomorrow');   // ['Shop', 'Order for tomorrow']
const graph = map.events();                      // { nodes, edges, cycles }, the event graph
```

`parsePlan(file, text)` reads `cases/plan.md` into its milestones (title, cases, delivered, line), and `checkMap(map, { code, plan, caseIds, production, contract })` runs the whole [check](https://cases.docs.sypnia.com/contract/the-check) on what you give it. `sameTable(a, b)` compares table names by singular and plural, as the check does, and `repairOneOf` joins a `one_of(a, b)` that a YAML flow mapping split at the comma.

The JSON Schemas of the format ship in the package too: `mapSchema`, `navigationSchema`, `caseSchema` and `eventSchema` from `@sypnia/cases/spec`, and the files in `@sypnia/cases/schema/` ([JSON Schema](https://cases.docs.sypnia.com/format/json-schema)).

## `@sypnia/cases/client`: the Cases API

```ts
import { createClient } from '@sypnia/cases/client';

const cases = createClient({ token: process.env.SYPNIA_TOKEN });   // a Sypnia token (session, PAT) or a Cases project token

const map = await cases.maps.get('la-espiga');
const proposal = await cases.proposals.create(map.id, {
  title: 'Gift cards',
  changes: [{ op: 'add', case: { id: 'redeem-gift-card', title: 'Pay with a gift card', actor: 'customer', goal: 'Use a gift card at checkout' } }],
});
await cases.proposals.requestReview(proposal.id, { reviewers: ['ana@laespiga.co'] });
```

Every method maps to one endpoint of the [API](https://cases.docs.sypnia.com/api): `me`, `workspaces`, `maps` (with navigation, places, `find`, events, links to Data and Telemetry, uploads and the audit), `versions`, `proposals`, `checks`, `cases`, `deliveries`, `deadLetters` and `tokens`.

- **One map.** `createClient({ token, map: 'la-espiga' })` lets the methods about one map leave it out: `cases.cases.links('place-order')`.
- **Another server.** `baseUrl` points the client at your own Cases server (by default `https://cases.api.sypnia.com/v1`, or `SYPNIA_CASES_API` plus `/v1`).
- **A session bound to a key.** Pass `headers: (method, url) => …` instead of `token`, to send `Authorization: DPoP …` and its `DPoP` proof on every request.
- **Errors** throw a `CasesApiError` with the `status`, the `code` of the [API errors](https://cases.docs.sypnia.com/api#errors), the `errors[]` of an `invalid_map`, and `retryAfter` for a `429`.
