Bridge protocol
Documents @snap4id-sdk/embed-protocol 1.0.0 · protocol 1.0
The Snap4ID page and your app talk over the WebView in JSON envelopes. @snap4id-sdk/embed-protocol declares every
message once (TypeScript types, Zod validators, JSON Schemas); the React Native component implements the host side,
and a native host implements it from the same contract. Every field:
bridge messages. Every code: error codes.
Transport
Section titled “Transport”| Direction | How |
|---|---|
| page → host | the page calls window.Snap4IdHost.postMessage(json), defined by the shim your host injects at document start; the shim forwards the string to your native code |
| host → page | your host evaluates window.__snap4idHostDeliver(json); the shim queues messages until the page calls Snap4IdHost.ready() |
hostShimSource(postExpression) builds the shim; only the post expression differs per stack (React Native
window.ReactNativeWebView.postMessage(json), iOS webkit.messageHandlers.<name>.postMessage(json), Android
<object>.postMessage(json)). deliverScript(json) builds the deliver call. Ready-made shims:
iOS, Android. A shim that arrives a
moment late is tolerated: the page waits up to 1.5 s before showing “no bridge”.
Envelope
Section titled “Envelope”{ "v": 1, "id": "<uuid>", "type": "<message type>", "payload": {}, "replyTo": "<id of the request, optional>" }| Field | Type | Required | Description |
|---|---|---|---|
v |
1 |
yes | Envelope version; anything else is refused (unsupported_protocol) |
id |
string | yes | Unique per message (a UUID) |
type |
string | yes | The message type, below |
payload |
object | yes | Validated per type |
replyTo |
string | The id being answered: on the host hello and price.result (both answer the page hello) and on purchase.result to a request |
Who checks the purchase (trust mode)
Section titled “Who checks the purchase (trust mode)”The protocol calls who checks purchases the trust mode; the page reports it in its hello (trustMode):
| Your setup | Trust mode |
|---|---|
| Snap4ID checks purchases and You check: our verifier server | verifier: the page sends the receipt to a verifier URL (Snap4ID’s, or your server’s); the verifier validates it and calls the Partner API unlock, signed with the key it holds |
| You check: your own backend | backend: the host hands the receipt to your backend, which validates it and calls the Partner API unlock, signed with your key |
Versioning
Section titled “Versioning”The protocol version is "1.0", exchanged in hello. Same major = compatible; another major ends the session with
a fatal unsupported_protocol. Minor versions are additive: unknown payload keys are dropped, never rejected, and new
error codes may appear (treat an unknown non-fatal code as a log line).
Messages
Section titled “Messages”| Page → host | Host → page | ||
|---|---|---|---|
hello |
always first; names your product | hello |
reply to the page hello |
job.created |
a new job | price.result |
your store price, after your hello |
purchase.request |
sell the photo | purchase.result |
reply to a request, or unsolicited |
job.completed |
paid, files ready | back |
system back gesture |
artifact.save, artifact.share |
Save / Share tapped | consent.changed |
analytics choice changed |
stepChanged, canGoBack, close |
navigation | error |
invalid_message, unsupported_protocol |
error |
a page error |
Shared types
Section titled “Shared types”Money={ amount_minor, currency }: integer minor units and an ISO 4217 code, for example{ "amount_minor": 1999, "currency": "PLN" }.Receipt:app-store(StoreKit 2 JWS),play(purchase data + signature) orfake(tests only), see receipts.consent={ statistics?, marketing? }: analytics only whenstatisticsistrue; marketing is never used.
Package entry points
Section titled “Package entry points”| Entry | Runs in | Contents |
|---|---|---|
@snap4id-sdk/embed-protocol |
anywhere | versions, schemas, parseWebToHost / parseHostToWeb (never throw), createWebToHost / createHostToWeb, error codes, receipts, storeBindingFor, consent rules, host overrides, hostShimSource, deliverScript |
@snap4id-sdk/embed-protocol/page |
browser | createPageBridge, the page side of the transport |
@snap4id-sdk/embed-protocol/node |
Node | Partner API request signing (signRequest, verifyRequest), webhook signing, test keys |
