Initialization

Installation

npm install @btai/web --save
 
npm install @btai/react --save

React Provider Setup

import { btAi } from '@btai/js-sdk';
 
// ... get signed jwt from your backend
 
// initialize sdk
const btAi = await btAi.init(jwt);

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

ParameterTypeRequiredDescription
jwtstringYesJWT token for authentication (despite the name, this should be a JWT)
environmentstringNoEnvironment: "production" or "sandbox" (default: "production")
childrenReactNodeYesReact components that will have access to the SDK

useBasisTheory Hook Returns

PropertyTypeDescription
btobjectBasisTheory SDK instance (null until initialized)
isInitializedbooleanWhether the SDK has finished initializing
isLoadingbooleanWhether the SDK is currently initializing
initErrorError | nullError object if initialization failed
environmentstringCurrent environment setting
getSessionKey()functionReturns the session key from the initialized SDK
isReady()functionReturns true if SDK is ready for use
getStatus()functionReturns current status: “loading”, “error”, “ready”, or “idle”
verifyPurchaseIntent(projectId, purchaseIntentId)functionTriggers customer verification for a purchase intent

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

  1. JWT Token: Despite the prop name apiKey, you must provide a valid JWT token, not a static API key
  2. Script Dependency: The BasisTheory SDK script must be loaded before the provider initializes
  3. Single Instance: The provider prevents duplicate initialization and reuses existing instances
  4. Automatic Cleanup: The provider handles cleanup when components unmount
  5. Environment: Use “sandbox” for development and testing, “production” for live applications