Initialization
Installation
npm install @basis-theory-ai/react@beta --save
React Provider Setup
import { BasisTheoryProvider, useBasisTheory } from '@basis-theory-ai/react';
function App() {
// Get JWT token from your backend
const apiKey = 'YOUR_JWT_TOKEN'; // This should be a JWT, not a static key
return (
<BasisTheoryProvider
apiKey={apiKey}
environment="production" // or "sandbox" for testing
>
<YourApp />
</BasisTheoryProvider>
);
}
function YourApp() {
const { bt, isInitialized, isLoading, initError } = useBasisTheory();
// Handle loading state
if (isLoading) {
return <div>Loading BasisTheory SDK...</div>;
}
// Handle initialization errors
if (initError) {
return <div>Error initializing SDK: {initError.message}</div>;
}
// Handle not yet initialized
if (!isInitialized) {
return <div>Initializing...</div>;
}
// SDK is ready to use
return (
<div>
<h1>BasisTheory SDK Ready!</h1>
<p>Session Key: {bt?.sessionKey}</p>
</div>
);
}
Hook Usage Example
import { useBasisTheory } from '@basis-theory/react-sdk';
function PaymentComponent() {
const {
bt, // BasisTheory SDK instance
isInitialized, // Boolean: is SDK ready
isLoading, // Boolean: is SDK initializing
initError, // Error object if initialization failed
apiKey, // Current API key/JWT
environment, // Current environment
getSessionKey, // Helper: get session key
isReady, // Helper: check if ready for use
getStatus // Helper: get current status string
} = useBasisTheory();
if (!isReady()) {
return <div>SDK Status: {getStatus()}</div>;
}
return (
<div>
<p>SDK is ready with session key: {getSessionKey()}</p>
</div>
);
}
Configuration Options
BasisTheoryProvider Props
| Parameter | Type | Required | Description |
|---|
apiKey | string | Yes | JWT token for authentication |
environment | string | No | Environment: "production", "sandbox", or "local" (default: "sandbox") |
children | ReactNode | Yes | React components that will have access to the SDK |
useBasisTheory Hook Returns
| Property | Type | Description |
|---|
isInitialized | boolean | Whether the SDK has finished initializing |
isLoading | boolean | Whether the SDK is currently initializing |
initError | Error | null | Error object if initialization failed |
apiKey | string | Current API key/JWT |
environment | string | Current environment setting |
verifyPurchaseIntent(projectId, intentId) | function | Triggers customer verification for a purchase intent |
Hook Usage Example
import { useBasisTheory } from '@basis-theory-ai/react';
function PaymentComponent() {
const {
bt, // BasisTheory SDK instance
isInitialized, // Boolean: is SDK ready
isLoading, // Boolean: is SDK initializing
initError, // Error object if initialization failed
apiKey, // Current API key/JWT
environment, // Current environment
getSessionKey, // Helper: get session key
isReady, // Helper: check if ready for use
getStatus // Helper: get current status string
} = useBasisTheory();
if (!isReady()) {
return <div>SDK Status: {getStatus()}</div>;
}
return (
<div>
<p>SDK is ready with session key: {getSessionKey()}</p>
</div>
);
}
Error Handling
The SDK handles several types of initialization errors:
Common Errors
- Invalid API key format: Empty or malformed JWT token
- Invalid environment: Environment not “production” or “sandbox”
- SDK not loaded: BasisTheory script not included in HTML
- Already initialized: SDK instance already exists (handled gracefully)
Error Handling Example
function ErrorHandlingExample() {
const { initError, isLoading, isInitialized } = useBasisTheory();
if (isLoading) return <div>Loading...</div>;
if (initError) {
// Handle specific error types
if (initError.message.includes('API key')) {
return <div>Please provide a valid JWT token</div>;
}
if (initError.message.includes('BasisTheory SDK not loaded')) {
return <div>Please include the BasisTheory script tag</div>;
}
return <div>Initialization error: {initError.message}</div>;
}
if (!isInitialized) return <div>Initializing...</div>;
return <div>SDK Ready!</div>;
}
Important Notes
- JWT Token: Despite the prop name
apiKey, you must provide a valid JWT token, not a static API key
- Script Dependency: The BasisTheory SDK script must be loaded before the provider initializes
- Single Instance: The provider prevents duplicate initialization and reuses existing instances
- Automatic Cleanup: The provider handles cleanup when components unmount
- Environment: Use “sandbox” for development and testing, “production” for live applications