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@beta

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.
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.

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.