API Reference
Brij.init(options)
Initializes the singleton with your host and API key. Must be called before open().
Brij.init({
host: 'https://app.yourco.com',
apiKey: 'pk_live_abc123'
})
Options
| Property | Type | Required | Description |
|---|---|---|---|
host | string | Yes | Base URL of your backend. Trailing slashes are stripped. |
apiKey | string | Yes | Passed as X-Brij-API-Key header on every request. |
fetch | typeof fetch | No | Custom fetch implementation. Useful for testing or non-browser environments. |
init() is synchronous and makes no network calls. Calling it again replaces the current config.
Brij.open(slug, options?)
Fetches the schema for slug and renders the form. Returns a Promise<void> that resolves once the form is mounted (not after submission).
await Brij.open('contact', {
mode: 'panel',
onSuccess(data) { /* ... */ },
onError(err) { /* ... */ },
onClose() { /* ... */ }
})
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
slug | string | Yes | Identifies the form on your host. Maps to /brij/schema/:slug and /brij/submit/:slug. |
options | BrijOpenOptions | No | Display and callback options (see below). |
BrijOpenOptions
| Property | Type | Default | Description |
|---|---|---|---|
mode | 'panel' or 'modal' | 'panel' | How the form is displayed. panel slides in from the right, modal centers on screen. |
onSuccess | (data: unknown) => void | - | Called after a successful submission with the response body. May return a Promise. |
onError | (error: Error) => void | - | Called when a network or validation error occurs. May return a Promise. |
onClose | () => void | - | Called when the user closes the form without submitting. May return a Promise. |
Calling open() while a form is already open silently closes the current one first.
Throws BrijError if init() has not been called, or if the schema request fails.
Brij.close()
Closes the currently open form and fires the onClose callback.
Brij.close()
Safe to call when no form is open - it is a no-op.
Brij.destroy()
Closes the form and removes all DOM nodes and injected styles created by brij.js.
Brij.destroy()
Use this for cleanup in single-page apps when unmounting the host component that owns the brij instance.
createBrij(options?)
Factory that returns a new BrijClient instance. Use when you need multiple independent instances on the same page, or when you want a non-singleton lifecycle.
import { createBrij } from 'brij'
const brij = createBrij({
host: 'https://app.yourco.com',
apiKey: 'pk_live_abc123'
})
await brij.open('contact')
The returned instance has the same init, open, close, and destroy methods as the singleton Brij.
Error codes
BrijError extends Error with a code property.
| Code | When it’s thrown |
|---|---|
BRIJ_NOT_INITIALIZED | open() called before init(), or called outside a browser. |
BRIJ_FETCH_FAILED | Schema request returned a non-2xx status. |
BRIJ_INVALID_SCHEMA | Schema response is missing steps or steps array is empty. |
BRIJ_SUBMIT_FAILED | Submit request returned non-2xx, or response body has success: false. |
BRIJ_NO_ACTIVE_SESSION | Internal - no session when one was expected. |