Persona Inquiry JavaScript SDK
The `persona` package offers the official JavaScript SDK for integrating Persona's identity verification Inquiry Flow into web applications. As of version 5.8.0, it provides a robust client library designed to embed and manage the entire inquiry process directly within the user's browser environment. Key functionalities include configuring inquiry templates, selecting runtime environments (e.g., sandbox, production), and handling critical lifecycle events through callbacks for completion, cancellation, and errors. The SDK abstracts away the complexities of direct API interaction, offering a streamlined integration experience for front-end developers. It ships with built-in TypeScript types, enhancing developer experience and type safety. Persona maintains an active development pace, regularly releasing updates and fixes, and explicitly recommends staying up-to-date. Its primary differentiation lies in being the official, fully supported client for Persona services, ensuring compatibility with the latest features and security standards.
Common errors
-
ReferenceError: Inquiry is not defined
cause Attempting to import `Inquiry` from 'persona' when it has been moved to 'persona-react' (since v5.0.0).fixIf using React, install `persona-react` (`npm install persona-react`) and update your import to `import Inquiry from 'persona-react'`. -
TypeError: Cannot read properties of undefined (reading 'inquiryId')
cause Using the old positional argument signature for the `onComplete` callback after upgrading to `persona@5.0.0` or later.fixUpdate your `onComplete` callback to use destructuring keyword arguments: `onComplete: ({ inquiryId, status, fields }) => { ... }`. -
TypeError: Client is not a constructor
cause Incorrectly importing `Client` in a CommonJS environment or mixing CommonJS `require()` syntax with ES Modules `import` syntax.fixFor CommonJS, use `const { Client } = require('persona');`. Ensure your build setup correctly handles ES Modules if you intend to use `import` statements. -
Persona: Invalid configuration. Missing 'templateId'.
cause The `templateId` option is a required parameter for initializing the Persona `Client`.fixEnsure the `templateId` property is correctly provided in the `Client` constructor options, for example: `templateId: 'your_template_id'`.
Warnings
- breaking Starting with `persona@5.0.0`, the `Inquiry` export (previously used for React integration) has been moved to a separate package, `persona-react`. Direct imports of `Inquiry` from `persona` will fail.
- breaking The `onComplete` callback signature was significantly changed in `persona@5.0.0`. It now uses keyword arguments `{ inquiryId, status, fields }` instead of positional arguments. The `onFail` callback was removed entirely.
- breaking The `onStart` callback was removed in `persona@5.0.0`. `onExit` and `client.exit()` were renamed to `onCancel` and `client.cancel()` respectively. The `accessToken` parameter was renamed to `sessionToken`.
- deprecated Features such as `prefill`, `lockedAttributes`, `themeId`, and the `InquiryAttributes` type export, which were deprecated in `persona@4.0.0`, have been completely removed in `persona@5.0.0`.
- gotcha The package `persona` provides its own TypeScript type definitions. Installing `@types/persona` is unnecessary and the `@types` package is deprecated, which can lead to type conflicts or confusion.
Install
-
npm install persona -
yarn add persona -
pnpm add persona
Imports
- Client
const Client = require('persona');import { Client } from 'persona'; - PersonaInquiryOptions
import type { PersonaInquiryOptions } from 'persona'; - ClientCallbacks
import type { ClientCallbacks } from 'persona';
Quickstart
import { Client } from 'persona';
// Initialize the Persona client for an embedded inquiry flow.
const client = new Client({
// Replace with your actual template ID, or use the provided demo template for testing.
templateId: 'itmpl_Ygs16MKTkA6obnF8C3Rb17dm',
// Use 'sandbox' for development and testing, 'production' for live applications.
environment: 'sandbox',
// Callback executed when the Persona flow is ready to be opened.
onReady: () => {
console.log('Persona client is ready, opening flow...');
client.open();
},
// Callback for when the inquiry process is successfully completed.
onComplete: ({ inquiryId, status, fields }) => {
console.log('Inquiry completed:', { inquiryId, status, fields });
// Handle post-completion logic, e.g., notify your backend, update UI.
},
// Callback for when the user cancels the inquiry flow.
onCancel: ({ inquiryId, sessionToken }) => {
console.log('Inquiry cancelled:', { inquiryId, sessionToken });
// Handle cancellation, e.g., log, return user to previous state.
},
// Callback for any errors encountered during the inquiry process.
onError: (error) => {
console.error('Persona inquiry error:', error);
// Implement robust error handling and user feedback.
},
// Optional: Set allowed events for the onEvent callback (e.g., ['step-transitioned', 'click']).
// eventsAllowlist: ['all']
});
console.log('Persona client initialized.');