Skip to content

Host the page in any native app

Documents @snap4id-sdk/embed-protocol 1.0.0 · protocol 1.0

Without React Native, your app implements the host side of the bridge itself. It is a small, well-defined job: a WebView, a script injected at document start, a message handler, and your store code. The contract is the bridge protocol; field-level details are in bridge messages. Platform walk-throughs: iOS and Android.

TypeScript hosts can use @snap4id-sdk/embed-protocol directly (parseWebToHost, createHostToWeb, hostShimSource, deliverScript). Other languages use the JSON Schemas it ships under schema/ and the shim text on the platform pages.

  1. Load only Snap4ID. Open https://snap4.id/embed/{partnerId} (or /embed/{partnerId}/job/{jobId} to reopen a job) full screen. Allow main-frame navigation and accept messages from https://snap4.id only; open other links outside the WebView or not at all.
  2. Inject the shim at document start. A short script, before any page script, defines window.Snap4IdHost. Its only stack-specific part is the line that posts to your native code (webkit.messageHandlers… on iOS, your JavaScript object on Android). If your WebView injects it a moment late, the page waits up to 1.5 s.
  3. Receive messages. Each page message arrives as one JSON string: { v: 1, id, type, payload, replyTo? }. Validate it; answer an invalid one that expected a reply with error { code: 'invalid_message' }.
  4. Send messages by running the shim’s deliver function with the JSON string: window.__snap4idHostDeliver(json). The shim queues messages until the page is ready.
  5. Answer hello at once with your hello (replyTo = the page hello’s id): protocolVersion: '1.0', platform, appVersion, the user’s consent, capabilities. Check the page’s trustMode matches your setup. Then send the price: the page’s hello names your product per store (productIds, the ids you gave Snap4ID at onboarding; your app configures none). Look up the id for your store and answer price.result { products } (replyTo = the page hello’s id; products: {} when your store has no price), which the page shows on its order button. Never hold your hello for the store.
  6. Sell on purchase.request. Buy the consumable productId bound to the paymentRef (StoreKit appAccountToken, Play obfuscatedProfileId) and keep the purchase unfinished. Then, by who checks purchases:
    • Snap4ID or our verifier server: answer purchase.result { status: 'succeeded', receipt }; the page has the receipt checked;
    • your own backend: send the receipt to your backend; after it unlocked the job, answer purchase.result { status: 'succeeded' } without a receipt (on failure: failed with a reason). Cancelled, pending and failed purchases answer cancelled, pending, failed.
  7. Finish after the unlock. The page had the receipt checked: when job.completed arrives for the job. Your own backend: when it answered success. A duplicate_purchase error names a purchase to finish and refund.
  8. Deliver. Store job.completed idempotently by jobId and copy the files (links expire after 30 days): receiving the files. Handle artifact.save / artifact.share if you granted them.
  9. Navigate. Show stepChanged.title in your header; while the page reported canGoBack: true, the system back gesture sends back; close on close and on a fatal error.
  10. Grant camera and files. Camera permission for https://snap4.id and a file chooser for the photo library.
  11. Recover at every start. Unlock purchases the store still holds unfinished (resend an unsolicited purchase.result when a pending one resolves while the page is open), and reopen paid jobs you have not delivered: the page sends job.completed again.

Rules 6–11 are the ones that protect a paid photo; see payments and trust and delivery and recovery.

Use a WebView that can inject a user script at document start and expose a JavaScript channel to native code (for Flutter, for example, flutter_inappwebview). Build the shim with that channel’s post call as the stack-specific line, and deliver messages by evaluating window.__snap4idHostDeliver(json). Everything else in the list above is the same.