@basis-theory/web-threeds

中文文档
2.2.2发布于 2026/09/15Apache-2.0TypeScriptESM + CJS零依赖构建来源已验证近 30 天下载 8万

The web-threeds package is a JavaScript/TypeScript library designed to simplify the implementation of 3DS (3D Secure) authentication processes in web applications. It provides a streamlined interface for handling both the session creation and challenge st

原始使用文档
文档目录13 节
  1. Overview
  2. Methods
  3. createSession
  4. startChallenge
  5. Usage Examples
  6. Creating a 3DS Session
  7. Start Challenge
  8. Error Handling
  9. Error Structure
  10. Common Error Scenarios
  11. Example: Comprehensive Error Handling
  12. Configuration Options
  13. Challenge Request

web-threeds

Overview

The web-threeds package is a JavaScript/TypeScript library designed to simplify the implementation of 3DS (3D Secure) authentication processes in web applications. It provides a streamlined interface for handling both the session creation and challenge steps of the 3DS process.

The web-threeds package exposes two main methods: createSession and startChallenge. These methods facilitate the initiation and handling of 3DS sessions and challenges.

Methods

createSession

createSession({ pan }: Create3dsSessionRequest): Promise<Create3dsSessionResponse>

This method initiates a 3DS session by making a request to the server with the provided PAN (Primary Account Number). It returns a promise that resolves to a Create3dsSessionResponse object containing session details.

startChallenge

startChallenge(options: ThreeDSChallengeRequest): Promise<void>

This method starts a 3DS challenge by making a request to the ACS (Access Control Server) with the necessary parameters. It handles the challenge process and returns a promise.

Usage Examples

Creating a 3DS Session

import { BasisTheory3ds } from '3ds-web';

const apiKey = 'your_api_key';
const { createSession } = BasisTheory3ds(apiKey);

const session = await createSession({
  pan: '<BASIS_THEORY_TOKEN_ID>',
});

Start Challenge

import { BasisTheory3ds } from '3ds-web';

const apiKey = 'your_api_key';
const { startChallenge } = BasisTheory3ds(apiKey);

await startChallenge({
  sessionId: 'session_id',
  acsTransactionId: 'acs_transaction_id',
  acsChallengeUrl: 'acs_challenge_url',
  threeDSVersion: '2.2.0',
  windowSize: '05', // Optional, defaults to 'THREE'
});

Error Handling

Both createSession and startChallenge may throw errors that include detailed information about what went wrong.

  • API errors (from createSession) are returned as Error objects with additional properties containing the full error details from the 3DS service
  • Challenge errors (from startChallenge) are returned as standard Error objects with descriptive messages

Error Structure

try {
  const session = await createSession({ tokenId: 'token_id' });
} catch (error) {
  // Error object properties:
  console.log(error.message); // Human-readable error message
  console.log(error.title); // Error title (e.g., "3DS Service Error")
  console.log(error.status); // HTTP status code (e.g., 424)
  console.log(error.detail); // Detailed description

  // Nested error object with additional context (normalized to camelCase):
  if (error.error) {
    console.log(error.error.serviceStatus); // HTTP status from 3DS service (e.g., "403")
    console.log(error.error.sessionId); // Session ID for debugging
    console.log(error.error.errorSource); // Which service failed (e.g., "3DS Server", "ACS")
    console.log(error.error.message); // Detailed error message
    console.log(error.error.detail); // Additional error context
  }
}

Common Error Scenarios

1. 3DS Service Errors (Status 424)

These errors occur when the 3DS service or card issuer returns an error:

try {
  const session = await createSession({ tokenId: 'token_id' });
} catch (error) {
  if (error.status === 424) {
    // Handle 3DS service errors
    switch (error.error?.serviceStatus) {
      case '403':
        // Access denied by issuer
        console.log('Merchant configuration issue:', error.error.detail);
        break;
      case '401':
        // Authentication failed
        console.log('Authentication failed:', error.error.message);
        break;
      default:
        console.log('3DS service error:', error.message);
    }
  }
}

2. Validation Errors (Status 400)

These errors occur when the request parameters are invalid:

try {
  const session = await createSession({ tokenId: 'invalid_token' });
} catch (error) {
  if (error.status === 400) {
    console.log('Validation error:', error.message);
  }
}

Example: Comprehensive Error Handling

import { BasisTheory3ds } from '@basis-theory/web-threeds';

const bt3ds = BasisTheory3ds('your_api_key');

try {
  const session = await bt3ds.createSession({ tokenId: 'token_id' });

  // Continue with challenge if needed
  try {
    await bt3ds.startChallenge({
      sessionId: session.id,
      acsTransactionId: 'acs_transaction_id',
      acsChallengeUrl: 'acs_challenge_url',
      threeDSVersion: '2.2.0',
    });
  } catch (challengeError) {
    // Handle challenge-specific errors
    console.error('Challenge error:', challengeError.message);
    if (challengeError.message.includes('Timed out')) {
      showErrorToUser('Challenge timed out. Please try again.');
    } else if (challengeError.message.includes('Invalid challenge request')) {
      showErrorToUser('Invalid challenge configuration.');
    } else {
      showErrorToUser('Challenge failed. Please try again.');
    }
    throw challengeError; // Re-throw to outer catch
  }
} catch (error) {
  // Log full error details for debugging
  console.error('3DS Error:', {
    message: error.message,
    title: error.title,
    status: error.status,
    detail: error.detail,
    error: error.error,
  });

  // Determine action based on error type
  if (error.status === 424 && error.error?.errorSource === '3DS Server') {
    // Merchant configuration issue - contact support
    showErrorToUser(
      'Payment authentication unavailable. Please try a different card.'
    );
  } else if (error.status === 424 && error.error?.errorSource === 'ACS') {
    // Issuer authentication issue
    showErrorToUser('Card authentication failed. Please contact your bank.');
  } else if (error.status === 400) {
    // Validation error
    showErrorToUser('Invalid card information. Please check and try again.');
  } else {
    // Generic error (could be challenge error or session error)
    showErrorToUser('An error occurred. Please try again later.');
  }
}

Configuration Options

Challenge Request

challengeContainerOptions allows you to customize the id property of the 3DS challenge container so you can target it and apply styling using CSS. It enables precise positioning of the 3DS challenge interface within your app's layout.

const { startChallenge } = BasisTheory3ds(apiKey, {
  challengeConfigurationOptions: {
    id: 'customId',
  },
});

版本与发布记录

@basis-theory/web-threeds 共发布 19 个版本,首次发布于 2025/02/04;过去 12 个月发布了 5 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
2.2.2latest2026/09/151 个月前
2.2.12026/09/121 个月前
2.1.22026/03/197 个月前
2.1.12026/02/198 个月前
2.0.02025/11/2111 个月前
1.3.52025/10/231 年前

关于 @basis-theory/web-threeds 的常见问题

@basis-theory/web-threeds 是什么?

The web-threeds package is a JavaScript/TypeScript library designed to simplify the implementation of 3DS (3D Secure) authentication processes in web applications. It provides a streamlined interface for handling both the session creation and challenge st

如何安装 @basis-theory/web-threeds?

在项目目录运行 npm install @basis-theory/web-threeds,或使用 pnpm add / yarn add / bun add。

@basis-theory/web-threeds 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@basis-theory/web-threeds 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@basis-theory/web-threeds 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=18.x。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@basis-theory/web-threeds 可以商用吗?

@basis-theory/web-threeds 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@basis-theory/web-threeds 的下载量有多少?

根据 npm 官方下载统计,@basis-theory/web-threeds 在 2026/10/08 – 2026/11/06 这 30 天内被下载 79,791 次,日均约 2,660 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@basis-theory/web-threeds 还在维护吗?

最新版本 2.2.2 发布于 2026/09/15(1 个月前);至今共发布 19 个版本,其中过去 12 个月发布了 5 个;首次发布于 2025/02/04。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。