# Snap4ID embed: instructions for coding agents

This project embeds Snap4ID (ID / passport photos) in a mobile app with the public Snap4ID SDK. Full docs in one
file: `llms-full.txt` at the root of the docs site (every link in it is an absolute URL you can fetch); in a checkout
of the SDK repository it is `docs/dist/llms-full.txt` after `npm run docs:build`, and the pages are the Markdown files
under `docs/src/content/docs/` (a site path `/test/fake-store/` is `docs/src/content/docs/test/fake-store.md`).
Production origin: `https://snap4.id` (the only one; never invent others).

The model: the app shows the Snap4ID page (`https://snap4.id/embed/{partnerId}`) in a WebView, answers its bridge
messages and sells the photo through the App Store or Google Play. React Native apps use `@snap4id-sdk/app-embed`,
which does all of it; other native apps (iOS, Android, Flutter) implement the bridge protocol themselves.

## Docs map (paths on the docs site)

Every page below also exists in Polish under `/pl/` (for example `/pl/how-it-works/flow/`), for Polish-speaking
partners; the Partner API reference is English only. Code, identifiers and message names are the same in both.

- Start: `/try-it/` (the example app on a simulator against the fake page, no account), `/go-live/onboarding/`
  (getting a partner id). Decide: `/choose-your-setup/` (embed path × who checks the purchase). Flow and rules: `/how-it-works/flow/`,
  `/how-it-works/payments-and-trust/`, `/how-it-works/receiving-files/` (what the app gets after payment and how to
  store, show, save and share it), `/how-it-works/delivery-and-recovery/`, `/how-it-works/privacy-and-consent/`.
- Integrate: `/integrate/react-native/` (quickstart), `/integrate/react-native/own-store/`,
  `/integrate/react-native/details/`; native hosts `/integrate/native-app/` (the host contract),
  `/integrate/native-app/ios/`, `/integrate/native-app/android/`.
- Payments (who checks the purchase): `/set-up-payments/snap4id-checks/` (Snap4ID checks; what the partner sends
  Snap4ID), `/set-up-payments/our-verifier-server/` (the partner runs our verifier server),
  `/set-up-payments/your-backend/` (the partner's own backend), `/set-up-payments/webhooks/`.
- Customize: `/customize/theme/`, `/customize/texts/`, `/customize/done-screen/`. Test: `/test/fake-store/`,
  `/test/app-store/`, `/test/google-play/`, `/test/your-backend/`. Launch: `/go-live/checklist/`.
- Reference: `/reference/component-api/`, `/reference/bridge-protocol/`, `/reference/bridge-protocol/messages/`,
  `/reference/partner-api/`, `/reference/purchase-verifier/`, `/reference/errors/`. Help: `/help/troubleshooting/`. Installing these instructions
  and skills in Claude Code, Codex, Antigravity or Cursor: `/reference/agent-context/`.

## Packages

- `@snap4id-sdk/app-embed`: React Native / Expo component. `createSnap4IdClient(config)` once per process,
  `client.start()` at every launch, `<Snap4IdResume client>` once near the root, `<Snap4IdFlow client … />` full screen.
  Expo plugins: `['@snap4id-sdk/app-embed', 'expo-iap']`. Needs a development build (`expo-dev-client`; not Expo Go).
  Install: `npx expo install expo-dev-client react-native-webview expo-iap expo-file-system expo-media-library
  expo-sharing expo-constants`, then `npm install @snap4id-sdk/app-embed`. The docs' snippets assume Expo Router (the
  default template); with another navigator, put the root-layout code in the root component.
- `@snap4id-sdk/app-embed/dev` (development builds only): `createDevFakeStore()`, a fake store with a "Test purchase"
  alert, passed as `createSnap4IdClient(config, { builtInStore: createDevFakeStore() })`.
- The SDK repository's fake Snap4ID page (`tools/fake-snap4id-page`, `npm run fake-page`, `http://127.0.0.1:8090`,
  partners `test-verifier` and `test-backend`, product `dev.snap4id.test.photo`): a local stand-in for simulator and
  backend tests (`/test/fake-store/`).
- `@snap4id-sdk/partner-api` (Node backend only): `createPartnerClient({ baseUrl: 'https://snap4.id', partnerId,
  keyId, privateKey })`, `unlock(jobId, …)`, `getJob(jobId)`, `verifyWebhook({ headers, rawBody })`.
- `@snap4id-sdk/purchase-verifier` (Node backend only): `verifyReceipt`, `choosePrice`, `verifyAndUnlock`.
- `@snap4id-sdk/embed-protocol`: the bridge contract (types, JSON Schemas, `hostShimSource`, `deliverScript`); used
  directly by non-React-Native hosts.

## Setup choices (ask the user if unknown; they must match the partner configuration Snap4ID holds)

- Who buys: `purchases: 'built-in'` (recommended) or `{ onPurchaseRequest, onConfirmPurchase, getUnfinishedPurchases,
  getPendingPurchases }`.
- Who checks the purchase (always a server, never the app):
  1. Snap4ID checks: no `onVerifyPurchase`; the partner sent Snap4ID its iOS bundle id, Android package, product ids,
     Play licensing public key and accepted environments, and Snap4ID holds the verifier signing key.
  2. The partner checks, either (a) on our ready-made verifier server (configuration only; app set up as in 1; the
     page sends receipts to the partner's verifier URL), or (b) in its own backend (`onVerifyPurchase`; the backend
     verifies, optionally with `@snap4id-sdk/purchase-verifier`, and calls the Partner API `unlock`).
  On the bridge, 1 and 2a are trust mode `verifier`, 2b is `backend`; a mismatch fails with `trust_mode_mismatch`.

## Rules (do not break)

1. Bind every store purchase to the page's `paymentRef` (`storeBinding.appAccountToken` on iOS,
   `storeBinding.obfuscatedProfileId` on Android).
2. Finish / consume a purchase only in `onConfirmPurchase`. Never auto-finish (RevenueCat: observer mode).
3. `onCompleted` and webhook handling are idempotent, keyed by `jobId` / `event_id`. Copy files: links expire with the
   job (30 days).
4. Webhooks: verify the raw body (no JSON body parser on that route); answer `401` on failure, `503` on
   `keys_unavailable`, never `410` unless deliberately stopping the event.
5. Refund every `duplicate_purchase` (`onError`) and every `already_paid` unlock answer.
6. Pass `consent={{ statistics }}` only from the user's real choice; omit it when unknown.
7. Partner private keys stay on the server, from the secret manager; never in the app bundle or the repository.
8. `@snap4id-sdk/app-embed/dev` and `/testing` only in tests and one deletable dev-only file, loaded under `__DEV__`.
   `EXPO_PUBLIC_*` variables are the app's own build settings: the component reads none.
9. Leave `origin` unset in release builds; `allowLocalOrigins` only in test builds that are never shipped.
10. Local App Store tests: a StoreKit configuration file applies only when Xcode launches the app (Product → Run, not
    `expo run:ios`); its `xcode` receipts are refused by production (fake page or test deployments only).

## Verify your work

- The app builds (`npx expo run:ios` / `run:android`) and a flow against a fake store reaches the done screen.
- Killing the app right after a test purchase still delivers the photo on the next start.
- Backend: unit tests for the webhook receiver with signed fixtures; an `already_paid` path that records a refund.
