@commercetools/checkout-sdk

中文文档
2.2.0发布于 2026/11/02MITTypeScriptESM + CJS构建来源已验证近 30 天下载 1.1万

商务工具的打字机 SDK 检查 API

中文使用文档
文档目录3 节
  1. 用法实例
  2. 浏览器环境
  3. 节点环境

商务工具检出 API 的打字机 SDK .

用法实例

浏览器环境

<script src="https://unpkg.com/@commercetools/ts-client@latest/dist/commercetools-sdk-client-v3.umd.js"></script>
<script src="https://unpkg.com/@commercetools/checkout-sdk@latest/dist/commercetools-checkout-sdk.umd.js"></script>
<script>
  // global: @commercetools/ts-client
  // global: @commercetools/checkout-sdk
  ;(function () {
    //  We can now access the ts-client and checkout-sdk object as:
    //  const { ClientBuilder } = this['@commercetools/ts-client']
    //  const { createApiBuilderFromCtpClient } = this['@commercetools/checkout-sdk']
    //  or
    //  const { ClientBuilder } = window['@commercetools/ts-client']
    //  const { createApiBuilderFromCtpClient } = window['@commercetools/checkout-sdk']
  })()
</script>

节点环境

npm install --save @commercetools/ts-client
npm install --save @commercetools/checkout-sdk
const {
  ClientBuilder,
  createAuthForClientCredentialsFlow,
  createHttpClient,
} = require('@commercetools/ts-client')
const { createApiBuilderFromCtpClient } = require('@commercetools/checkout-sdk')

const projectKey = 'mc-project-key'
const authMiddlewareOptions = {
  host: 'https://auth.europe-west1.gcp.commercetools.com',
  projectKey,
  credentials: {
    clientId: 'mc-client-id',
    clientSecret: 'mc-client-secrets',
  },
  oauthUri: '/oauth/token', // - optional: custom oauthUri
  scopes: [`manage_project:${projectKey}`],
  fetch,
}

const httpMiddlewareOptions = {
  host: 'https://checkout.europe-west1.gcp.commercetools.com',
  fetch,
}

const client = new ClientBuilder()
  .withProjectKey(projectKey)
  .withMiddleware(createAuthForClientCredentialsFlow(authMiddlewareOptions))
  .withMiddleware(createHttpClient(httpMiddlewareOptions))
  .withUserAgentMiddleware()
  .build()

// or
const client = new ClientBuilder()
  .withProjectKey(projectKey)
  .withClientCredentialsFlow(authMiddlewareOptions)
  .withHttpMiddleware(httpMiddlewareOptions)
  .withUserAgentMiddleware()
  .build()

const apiRoot = createApiBuilderFromCtpClient(client)

// calling the checkout-sdk functions
// create a new transaction using the checkout sdk
apiRoot
  .withProjectKey({ projectKey })
  .transactions()
  .post({
    body: {
      application: {
        typeId: 'application',
        key: 'checkout-application-key',
      },
      transactionItems: [
        {
          paymentIntegration: {
            typeId: 'payment-integration',
            id: 'payment-integration-id',
          },
          amount: {
            centAmount: 1000,
            currencyCode: 'EUR',
          },
        },
      ],
      cart: {
        typeId: 'cart',
        id: 'cart-id',
      },
    },
  })
  .execute()
  .then(...)
  .catch(...)

-----------------------------------------------------------------------
// The ts-client also has support for the old syntax
import {
  createClient,
  createHttpClient,
  createAuthForClientCredentialsFlow,
} from '@commercetools/ts-client'
import { createApiBuilderFromCtpClient } from '@commercetools/checkout-sdk'

const projectKey = 'some_project_key'

const authMiddleware = createAuthForClientCredentialsFlow({
  host: 'https://auth.europe-west1.gcp.commercetools.com',
  projectKey,
  credentials: {
    clientId: 'some_id',
    clientSecret: 'some_secret',
  },
  httpClient: fetch,
})

const httpMiddleware = createHttpClient({
  host: 'https://checkout.europe-west1.gcp.commercetools.com',
  httpClient: fetch,
})

const ctpClient = createClient({
  middlewares: [authMiddleware, httpMiddleware],
})

const apiRoot = createApiBuilderFromCtpClient(ctpClient)

apiRoot
  .withProjectKey({ projectKey })
  .transactions()
  .post({
    body: {
      application: {
        typeId: 'application',
        key: 'checkout-application-key',
      },
      transactionItems: [
        {
          paymentIntegration: {
            typeId: 'payment-integration',
            id: 'payment-integration-id',
          },
          amount: {
            centAmount: 1000,
            currencyCode: 'EUR',
          },
        },
      ],
      cart: {
        typeId: 'cart',
        id: 'cart-id',
      },
    },
  })
  .execute()
  .then(...)
  .catch(...)

版本与发布记录

@commercetools/checkout-sdk 共发布 10 个版本,首次发布于 2025/10/02;过去 12 个月发布了 9 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
2.2.0latest2026/11/028 天前
2.1.02026/10/101 个月前
2.0.12026/08/053 个月前
2.0.02026/06/154 个月前
1.9.02026/06/015 个月前
1.8.02026/06/015 个月前

关于 @commercetools/checkout-sdk 的常见问题

@commercetools/checkout-sdk 是什么?

商务工具的打字机 SDK 检查 API

如何安装 @commercetools/checkout-sdk?

在项目目录运行 npm install @commercetools/checkout-sdk,或使用 pnpm add / yarn add / bun add。

@commercetools/checkout-sdk 支持 TypeScript 吗?

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

@commercetools/checkout-sdk 支持 ESM 还是 CommonJS?

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

@commercetools/checkout-sdk 需要什么版本的 Node.js?

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

@commercetools/checkout-sdk 可以商用吗?

@commercetools/checkout-sdk 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@commercetools/checkout-sdk 的下载量有多少?

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

@commercetools/checkout-sdk 还在维护吗?

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