Anon Frontend SDK
The@anon-dot-com/frontend package provides a lightweight JavaScript library for embedding the Anon Link directly into your web application. This SDK handles link token authentication, link status checking, and provides seamless integration through a secure iframe modal.
Overview
The SDK creates a responsive iframe modal that displays the Anon Link interface within any web application. It provides event-driven communication and handles the complete link lifecycle using a secure token-based authentication flow.Install the Package
You can install the package using your preferred package manager:Quick Start
Basic Integration
The SDK provides two main methods for integrating the Anon Link:Advanced Configuration
Configure the link with additional options for more control over the link process:Global Script Usage
If you’re not using a module bundler, include the library as a global script:API Reference
Methods
init(options: AnonInitOptions)
init(options: AnonInitOptions)
Initializes the SDK and finds any existing link instance for the user. This method checks localStorage for previous link options and attempts to continue an existing link session if found.Parameters:
options: Configuration object of typeAnonInitOptions
- Promise that resolves with link metadata if an existing link is found, or undefined
start(options: AnonLinkOptions)
start(options: AnonLinkOptions)
Creates a new link session and immediately shows the iframe modal. This method requires a link token obtained from your backend.Parameters:
options: Configuration object of typeAnonLinkOptions
- Promise that resolves when the link session is started
Configuration Options
AnonInitOptions
TheAnonInitOptions interface accepts the following properties:
| Property | Type | Required | Description |
|---|---|---|---|
onEvent | (event: EventData) => void | No | Callback for link events |
target | string | No | CSS selector for target element (defaults to “body”) |
baseURL | string | No | Base URL for API requests (defaults to “https://worker.anon.com”) |
AnonLinkOptions
TheAnonLinkOptions interface extends AnonInitOptions and accepts additional properties:
| Property | Type | Required | Description |
|---|---|---|---|
linkToken | string | Yes | Link token obtained from your backend’s /api/v2/link/token endpoint |
Event Structure
TheonEvent callback receives events with the following structure:
Common event types include
link.opened, link.closed, job_run.completed, job_run.failed, workflow_run.completed, and workflow_run.failed.Authentication Flow
Your backend must generate a link token that the SDK uses to authenticate. Your backend must implement an endpoint that generates link tokens:- Next.js
- Express.js
Link Persistence
The SDK automatically persists link state in the browser’s localStorage. When you callinit(), it checks for any existing link options and attempts to continue where the user left off.
Architecture Flow
The following diagram illustrates the authentication flow:Backend Requirements
Required Endpoints
Your backend needs to implement the following endpoint:| Endpoint | Method | Purpose |
|---|---|---|
/api/anon/link-token | POST | Generate a link token for frontend authentication |
Security Considerations
- Store your Anon API key securely in environment variables
- Implement proper authentication/authorization for the link token endpoint
- Consider rate limiting the link token endpoint to prevent abuse
- Link tokens are short-lived (60 seconds) and single-use for security
Event Handling
The SDK provides comprehensive event handling for link lifecycle events:Migration from v1
If you’re migrating from v1 SDK:Before (v1)
After (v2)
Breaking Changes
startModal()has been renamed tostart()userIdparameter is no longer needed;external_idis included in the link token- Event types have changed:
migration_modal.*events are nowlink.*,job_run.*, andworkflow_run.* - API endpoint changed from
/api/v1/link-tokento/api/v2/link/token - Link token request now requires
external_idandjob_idinstead of just user context
Next Steps
Embedding Link
Learn more about embedding the Anon Link in your application
Webhooks
Set up webhooks to receive link and job run status updates
Sending Links
Learn about creating and sending links via email or SMS
API Reference
Explore the complete Anon API documentation