Handle custom event from miniapp
Handle custom event from miniapp
The custom events system is a powerful feature that enables bidirectional data communication between a miniapp and the host app. The miniapp can send data that the host app can recognize. Additionally, certain operations can be implemented at the native app level for enhanced functionality.Calling custom event from miniapp with promise:Receiving custom event sent from host app:
Examples
Calling custom event from miniapp:Boxo Event Bridge
Boxo Event Bridge
Introduction
Introduction
Boxo Event Bridge is a bidirectional communication functionality that enables host apps and miniapps to exchange events and information seamlessly within the Boxo ecosystem.
Event Bridge flow
Here is a sequence flow for event communication process between a miniapp and a host app.1
Create event
An app (either hostapp or miniapp) creates an event with a specific event type and payload
2
Send to Boxo Platform
The app sends the event to the Boxo Platform via the Event Bridge API
3
Validate and authenticate
The Boxo Platform validates the event and authenticates the sender
4
Forward to recipient
The Boxo Platform forwards the event to the recipient’s event receiver URL
5
Process event
The recipient processes the event and returns a response
6
Capture response
The Boxo Platform captures the response and forwards it to the original sender
7
Receive response
The original sender receives the response and can take appropriate actions
Setting up the backend
Setting up the backend
*Note: Feature must be enabled in DashboardData formatJSON is used for data exchange. Event payloads can contain any valid JSON structure that both the sender and receiver agree upon.
1. Event Receiver Endpoint
1. Event Receiver Endpoint
This endpoint allows your app to receive events from the Boxo Platform.URL and METHOD:This endpoint must handle a HTTPS POST request
URL to endpoint must be provided in DashboardHeaders:
Response:
Key | Value |
---|---|
Authorization | <prefix> <base64 encoded(hostapp_client_id:hostapp_secret_key)> |
Content-type | application/json |
- Default
<prefix>
:Token
. Access token prefix can be set in Boxo Connect. hostapp_client_id
andhostapp_secret_key
must be provided in Dashboard
Field | Data type | Description |
---|---|---|
app_id | String | Miniapp identifier |
client_id | String | Hostapp identifier |
event_type | String | Event type identifying the purpose of the event |
payload | Object | Event data containing information specific to the event |
- Response status must be
200
in all cases - Response body can contain any valid JSON that will be forwarded to the event sender
2. Sending Events API
2. Sending Events API
This endpoint allows your app to send events to miniapps through the Boxo Platform.URL and METHODThis is HTTPS POST
Response
/api/v1/event-bridge/hostapp/
endpointHeadersKey | Value |
---|---|
Authorization | <prefix> <base64 encoded(hostapp_client_id:hostapp_secret_key)> |
Content-type | application/json |
- Default
<prefix>
:Token
. Access token prefix can be set in Boxo Connect. hostapp_client_id
andhostapp_secret_key
must be provided in Dashboard
Field | Data type | Description |
---|---|---|
app_id | String | Miniapp identifier (recipient) |
client_id | String | Hostapp identifier (sender) |
event_type | String | Event type identifying the purpose of the event |
payload | Object | Event data containing information specific to the event |
- Response status will be
200
if the event was delivered successfully - Response body will contain the response from the miniapp’s event receiver endpoint
Common Event Types
Common Event Types
Here are some common event types that can be used for communication between host apps and miniapps:
Host App to Miniapp Events
Event Type | Description | Example Payload |
---|---|---|
user_profile_updated | User profile information has been updated | {"user_reference": "123", "email": "example@example.com"} |
Miniapp to Host App Events
Event Type | Description | Example Payload |
---|---|---|
order_status_update | Miniapp order fulfillment status | {"order_payment_id": "order_123", "fulfillment_status": "delivering"} |
Error Handling
Error Handling
If an error occurs during event processing, the Boxo Platform will return an appropriate error response:
Error Code | Description |
---|---|
EVENT_RECEIVER_DISABLED | Event receiver is not enabled for the recipient |
INVALID_EVENT_DATA | The event data format is invalid |
EVENT_DELIVERY_FAILED | Failed to deliver the event to the recipient |
UNAUTHORIZED | Sender is not authorized to send events |
Best Practices
Best Practices
- Event Types: Use consistent event types that clearly indicate the purpose of the event
- Minimal Payloads: Keep event payloads as small as possible, including only necessary information
- Error Handling: Implement proper error handling for event processing failures
- Idempotency: Design event handlers to be idempotent to handle potential duplicate events
- Timeouts: Implement reasonable timeouts for event processing to avoid blocking operations