Skip to main content

Overview

The postMessage API provides secure, real-time cross-origin communication between your parent window and the embedded webview. This is the recommended method for handling sensitive authentication data.
Examples use the production host. On sandbox, both the origin you verify and the targetOrigin you post to must be https://sandbox.embed.doshi.app. See Environments.

How It Works

1

Generate Custom Token

Your backend calls the Doshi API to generate a custom token
2

Webview Sends PING

The embedded webview sends a PING message to signal it’s ready
3

Parent Receives PING

Parent window listens for the PING and prepares authentication data
4

Parent Sends AUTH

Parent sends the custom token and user data back to the webview
5

Webview Processes AUTH

Webview receives and processes the authentication, handling 2FA if needed

TypeScript Interfaces

React Implementation

Complete Example with API Call

Usage Example

Vanilla JavaScript Implementation

Parent Window

HTML Structure

Backend Implementation

Your backend should handle the API key securely:

Child Window (Webview) Implementation

This section is for reference only. The Doshi iframe already implements this functionality.

Sending PING

Receiving AUTH Data

Advanced: Retry Logic

Add retry logic for more robust authentication:

Message Types

type
string
required
The message type identifier

Testing postMessage Flow

Common Issues

Solutions:
  1. Check that origin validation matches exactly
  2. Verify iframe has loaded before sending messages
  3. Ensure JSON.stringify is used when sending data
  4. Check browser console for errors
Solutions:
  1. Ensure backend API call completed successfully
  2. Check network tab for API response
  3. Verify error handling in token fetch logic
  4. Check that API key is valid
Solutions:
  1. Verify is2FaEnabled is set to true
  2. Ensure all required 2FA fields are provided
  3. Check that organization has 2FA enabled
  4. Verify user phone number is registered

Next Steps

Security Guidelines

Review security best practices

Best Practices

Optimize your implementation