Skip to main content

Initialization

This page covers installing the SDK, initializing it in your project, and the available configuration options.

Installation

npm install --save @basis-theory/web-elements

Initialization

ES module:

import BasisTheory from '@basis-theory/web-elements';

const bt = BasisTheory('<PUBLIC_API_KEY>');

const cardNumber = bt.createElement('cardNumber', {
placeholder: '4111 1111 1111 1111',
});

await cardNumber.mount('#card-number-container');

BasisTheory() is synchronous — it returns the SDK instance immediately with no await needed.

CDN / script tag:

1
<script>
document.addEventListener('DOMContentLoaded', async () => {
const bt = window.BasisTheory('<PUBLIC_API_KEY>');

const cardNumber = bt.createElement('cardNumber', {
placeholder: '4111 1111 1111 1111',
});

await cardNumber.mount('#card-number-container');
});
</script>

SDK options

These options are passed as the second argument to BasisTheory(), or as the options prop on BasisTheoryProvider.

OptionTypeDefaultDescription
debugbooleanfalseEnable verbose debug logging to the browser console.
allowHttpClientbooleanfalseAllow bt.client.* to send element data directly to a third-party destination (dual writing). Frozen at initialization. Destinations must also be allowlisted by Basis Theory.
sessionAuthorizationUrlstringEndpoint on your backend that authorizes the session used by cardDisplay. Required by that element and ignored by the others. Must be https://, except on localhost and 127.0.0.1; relative paths resolve against your page's origin.
themeMode'light' | 'dark' | 'auto''auto'Controls which theme tokens are applied. 'auto' follows the OS prefers-color-scheme.
themeThemeTokensLight-mode design tokens. See Theming.
darkThemeThemeTokensDark-mode design tokens. If omitted and dark mode is active, falls back to theme.
direction'ltr' | 'rtl''ltr'Writing direction applied to every element. See Right-to-Left.
languagestringBCP 47 tag applied to every element, for assistive technology. See Right-to-Left.

Framework-specific setup

Next.js

Elements require browser APIs and DOM access, so they must only be initialized on the client side. Mark any component that uses elements with "use client":

// components/PaymentForm.tsx
"use client";

import { useRef } from 'react';
import {
BasisTheoryProvider,
CardNumberElement,
ExpiryElement,
CVVElement,
useBasisTheory,
} from '@basis-theory/react-elements';

function PaymentFormInner() {
const { bt } = useBasisTheory();
const cardNumberRef = useRef(null);
const expiryRef = useRef(null);
const cvvRef = useRef(null);

const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
const token = await bt!.tokens.create({
type: 'card',
data: {
number: cardNumberRef.current,
expiration_month: expiryRef.current,
expiration_year: expiryRef.current,
cvc: cvvRef.current,
},
});
console.log(token.id);
};

return (
<form onSubmit={handleSubmit}>
<CardNumberElement id="card-number" ref={cardNumberRef} />
<ExpiryElement id="expiry" ref={expiryRef} />
<CVVElement id="cvv" ref={cvvRef} />
<button type="submit">Pay</button>
</form>
);
}

export default function PaymentForm() {
return (
<BasisTheoryProvider apiKey={process.env.NEXT_PUBLIC_BASIS_THEORY_KEY!}>
<PaymentFormInner />
</BasisTheoryProvider>
);
}

Nuxt.js

Register Elements as a client-only plugin to prevent server-side rendering errors:

// plugins/basistheory.client.js
import BasisTheory from '@basis-theory/web-elements';

export default defineNuxtPlugin(() => {
const bt = BasisTheory(process.env.BASIS_PUBLIC_API_KEY);
return {
provide: { bt },
};
});

Then access the SDK in your components via useNuxtApp().$bt.

Don't see your framework? Reach out to support@basistheory.com and we'll help you integrate.


Best practices

  1. Single instance — Create one SDK instance and share it throughout your application rather than calling BasisTheory() in multiple places.
  2. Public keys only — Never use Private API keys in frontend code. Only pk_ keys belong in the browser.
  3. CSP headers — If you use Content Security Policy, allow frame-src and script-src for *.basistheory.com. See the security guide.
  4. SRI for CDN usage — When loading the SDK via a CDN <script> tag, add an integrity attribute to verify the file has not been tampered with. See Subresource Integrity.