1. Introduction
Sanal AI
  • Introduction
    • ๐Ÿ’ฌ Sanal AI Integration
    • ๐Ÿค Overview
    • ๐Ÿš€ Getting Started
    • ๐Ÿ› ๏ธ Integration Flow
    • โœ… Checklist
    • ๐Ÿ”Œ Bridge Protocol
  • Data Sync
    • ๐Ÿ› ๏ธ Data Sync
    • ๐Ÿ“ Data Layout
    • ๐Ÿช Merchants
    • ๐Ÿ“‚ Categories
    • ๐Ÿท๏ธ Items
    • ๐Ÿ›’ Products
    • ๐Ÿ“ฆ Inventory
    • ๐Ÿ‘ค Customers
    • ๐Ÿงพ Orders
    • ๐Ÿงพ Variants & Combinations
  • API Requirements
    • ๐Ÿ”Œ API Requirements
    • API: `syncMerchants`
    • API: `syncCategories`
    • API: `syncItems`
    • API: `syncProducts`
    • API: `syncInventory`
    • API: `syncCustomers`
    • API: `syncOrders`
  1. Introduction

๐Ÿ”Œ Bridge Protocol

๐Ÿ” Overview#

The host app and widget exchange JSON messages. Every message has a type field prefixed with SANAL_.
Host โ†’ Widget: window.postMessage(json, '*') inside the WebView, or iframe.contentWindow.postMessage(json, '*') on web.
Widget โ†’ Host: window.SanalBridge.postMessage(json) (Flutter), ReactNativeWebView.postMessage (React Native), or window.parent.postMessage (iframe).

๐Ÿ“ฑ Embed the widget#

Flutter#
Add webview_flutter to pubspec.yaml. Register the channel name SanalBridge exactly (case-sensitive).
Android: INTERNET permission in AndroidManifest.xml.

Web (iframe)#

React Native#
Use a WebView with JavaScript enabled. Listen with onMessage. Send to the widget with window.postMessage(json, '*') via injectJavaScript.

โžก๏ธ Host โ†’ Widget#

SANAL_INIT#
Send after SANAL_READY. Provides the user session. Processed once per widget session.
{
  "type": "SANAL_INIT",
  "payload": {
    "userId": "YOUR_USER_ID",
    "authToken": "YOUR_JWT_OR_SESSION_TOKEN",
    "language": "en"
  }
}
FieldRequiredDescription
userIdYesPlatform customer ID
authTokenYesToken for Sanal API calls
aiUserIdNoAI user ID; defaults to userId
languageNoen or ar
addressNoPre-selected delivery address
locationNo{ "lat", "long" } for AI context
SANAL_CART_SYNC#
Replaces the widget cart. Your cart is always the source of truth.
{
  "type": "SANAL_CART_SYNC",
  "payload": {
    "merchantId": "YOUR_MERCHANT_ID",
    "items": [
      {
        "id": "line-1",
        "uuid": "stable-uuid-per-line",
        "product_id": "prod_123",
        "variant_sku": null,
        "modifiers": [],
        "name": "Product name",
        "price": 25.0,
        "image": "https://...",
        "quantity": 1,
        "merchantId": "YOUR_MERCHANT_ID",
        "merchantName": "Store name"
      }
    ]
  }
}
Send again after every cart change on your side.
SANAL_SEND_MESSAGE (optional)#
{
  "type": "SANAL_SEND_MESSAGE",
  "payload": { "message": "Show me popular items" }
}

โฌ…๏ธ Widget โ†’ Host#

MessagePayloadHost action
SANAL_READYโ€”Send SANAL_INIT, then SANAL_CART_SYNC
SANAL_CART_ADDuuid, quantity, modifiers, variant_groups, variant_combinationsAdd to your cart โ†’ SANAL_CART_SYNC
SANAL_CART_REMOVEuuidRemove line โ†’ SANAL_CART_SYNC
SANAL_CART_CHANGEuuid, quantity, variantSku, modifiersUpdate line โ†’ SANAL_CART_SYNC
SANAL_CART_REQUESTโ€”Reply with SANAL_CART_SYNC
SANAL_NAVIGATE_CHECKOUTโ€”Open your checkout
SANAL_NAVIGATE_PRODUCTproductIdOpen product screen
SANAL_NAVIGATE_MERCHANTmerchantIdOpen merchant screen
SANAL_NAVIGATE_ADDRESSESโ€”Open address screen
SANAL_CLOSE_REQUESTEDโ€”Close chat screen

SANAL_CART_ADD (production)#
In production, the widget sends only the fields below. Product name, price, image, and merchant details are not included โ€” resolve them in your app from your own catalog using the line uuid and variant/modifier data.
{
  "type": "SANAL_CART_ADD",
  "payload": {
    "uuid": "new-line-uuid",
    "quantity": 1,
    "modifiers": [],
    "variant_groups": [],
    "variant_combinations": []
  }
}
FieldTypeDescription
uuidstringCart line ID assigned by the widget; use for SANAL_CART_REMOVE / SANAL_CART_CHANGE
quantitynumberUnits to add
modifiersarraySelected modifier IDs
variant_groupsarraySelected variant group data
variant_combinationsarraySelected variant combination data
After handling the add in your cart, send SANAL_CART_SYNC with your full cart (including display fields your app owns).

๐Ÿงฉ Flutter โ€” send and receive#

Send to widget:
Handle from widget:
Implement _onCartAdd, _onCartRemove, _onCartChange, _sendInit, _syncCart, and navigation using your own cart and routing.

๐Ÿ“‹ Message summary#

Host โ†’ widget: SANAL_INIT, SANAL_CART_SYNC, SANAL_SEND_MESSAGE
Widget โ†’ host: SANAL_READY, SANAL_CART_ADD, SANAL_CART_REMOVE, SANAL_CART_CHANGE, SANAL_CART_REQUEST, SANAL_NAVIGATE_*, SANAL_CLOSE_REQUESTED
If you have any questions, please reach out to our technical team.
Previous
โœ… Checklist
Next
๐Ÿ› ๏ธ Data Sync
Built with