快工助手跨境电商知识与商机助手

Integrate Node.js SDK

TikTok Shop 官方资料 · TikTok Shop Partner Center 开发者文档 · 适合开发者

stable本次发布有变化全部展示

来自 TikTok Shop 官方资料快照 ·

打开官方原文 ↗
  1. 当前资料结构化阅读页
  2. 固定快照已留存,可追溯
  3. 官方原文可核对
查看技术与溯源信息
平台 / profile
TikTok Shop / profile.tiktok.docs_api
语言
en-US
发布版本
cn-20260909-2
标签
zhuge/sourceplatform/tiktok_shopaudience/developercategory/api_doctopic/compliancetopic/developer

资料正文

§1 Overview

Follow this guide to install the TikTok Shop Node.js SDK, retrieve a seller access token, get an authorized shop's shop_cipher, and make your first product API call with Search Products. The SDK signs TikTok Shop API requests for you. Use Sign your API request only when you call OpenAPI endpoints with your own HTTP client instead of the SDK. This guide uses:

TaskSDK API or helperOpenAPI endpoint
Exchange an authorization code for an access tokenAccessTokenTool.getAccessToken(authCode)Authorization token exchange helper in the SDK
Get authorized shops and shop_cipherAuthorizationV202309Api.ShopsGetGET /authorization/202309/shops
Search productsProductV202502Api.ProductsSearchPostPOST /product/202502/products/search

Version note: API groups do not always share the same version. For example, this guide uses AuthorizationV202309Api for Get Authorized Shops because the current endpoint is /authorization/202309/shops, while the Search Products example uses ProductV202502Api.

#

§2 Prerequisites

Before integrating the SDK, you need:

  1. A TikTok Shop app and a test seller account. See Create a test seller account.
  2. A seller authorization code from the redirect URL configured for your test app. See Generate a test access token.
  3. The latest Node.js SDK package downloaded from Partner Center. SDK packages are generated for your app's enabled scopes and API versions. See Update SDK.

Do not commit app_key, app_secret, access_token, refresh_token, authorization codes, or seller data to source control. Load secrets from environment variables or your secret manager.

#

§3 Environment

Use a supported Node.js LTS release. As of July 2026, Node.js 22.x and 24.x are supported LTS lines; Node.js 16.x is end-of-life. Check the Node.js release schedule before publishing a new version of this guide. Recommended baseline:

ComponentRecommendation
Node.js22.x LTS or 24.x LTS
TypeScriptCurrent supported TypeScript major used by your project
HTTP clientUse the SDK's generated client. For custom HTTP calls, use Node's built-in fetch or another maintained client. Do not add request to new projects.
#

§4 Project layout

After downloading and unzipping the SDK, place it in your project and import it by the relative path from your code file to the SDK entry point. Example layout:

my-project/
  package.json
  tsconfig.json
  src/
    index.ts
  tiktok-shop-node-sdk/
    index.ts
    api/
    model/
    ...

If your demo file is src/index.ts, import from the SDK folder with:

import {
  AccessTokenTool,
  ClientConfiguration,
  TikTokShopNodeApiClient,
} from "../tiktok-shop-node-sdk";

If you place the demo file directly inside the SDK root, from "." works because it points to that folder's index.ts. In an application project, prefer importing from the explicit SDK folder path.

#

§5 Installation

Install the dependencies listed in the SDK package you downloaded. If the generated SDK includes its own package.json, use that file as the source of truth. For a new TypeScript project around the SDK, start with a maintained baseline similar to this:

{
  "type": "module",
  "scripts": {
    "start": "tsx src/index.ts"
  },
  "dependencies": {
    "tslib": "^2.8.1"
  },
  "devDependencies": {
    "@types/node": "^24",
    "tsx": "^4",
    "typescript": "^6"
  }
}

Do not add request or @types/request to new integrations. The request package is deprecated and should not be used for new custom HTTP code. If your downloaded SDK still depends on request, download the latest SDK from Partner Center and check whether the generated client has been updated; otherwise keep the dependency only as a compatibility requirement for that specific SDK package. Add the following to tsconfig.json:

{
  "compilerOptions": {
    "esModuleInterop": true,
    "moduleResolution": "node",
    "target": "ES2022",
    "module": "ESNext",
    "strict": true
  }
}

Install dependencies:

npm install

You can also use yarn install or pnpm install if your project standardizes on Yarn or pnpm.

#

§6 Configuration

Configure the SDK from environment variables:

const requiredEnv = (name: string): string => {
  const value = process.env[name];
  if (!value) {
    throw new Error(`Missing required environment variable: ${name}`);
  }
  return value;
};

ClientConfiguration.globalConfig.app_key = requiredEnv("TTS_APP_KEY");
ClientConfiguration.globalConfig.app_secret = requiredEnv("TTS_APP_SECRET");

const client = new TikTokShopNodeApiClient({
  config: {
    sandbox: process.env.TTS_SANDBOX === "true",
  },
});

sandbox: false sends requests to the production environment. Use sandbox: true only when you are testing against the sandbox or Development Shop environment supported by your SDK package.

#

§7 Get Access Token

Use the SDK helper to exchange the one-time seller authorization code for an access token.

const authCode = requiredEnv("TTS_AUTH_CODE");

const { body: tokenBody } = await AccessTokenTool.getAccessToken(authCode);
console.log("getAccessToken response:", JSON.stringify(tokenBody, null, 2));

const accessToken = tokenBody.data?.access_token;
if (!accessToken) {
  throw new Error("Failed to get access token");
}

In production, store the access token, refresh token, and their expiration timestamps in a secure server-side data store. Do not store tokens in frontend code.

#

§8 Get Shop Cipher

shop_cipher identifies the authorized shop for shop-level APIs such as Search Products. Call Get Authorized Shops with the seller accessToken and use data.shops[].cipher from the response.

const contentType = "application/json";

const { body: shopsGetBody } =
  await client.api.AuthorizationV202309Api.ShopsGet(accessToken, contentType);

console.log("ShopsGet response:", JSON.stringify(shopsGetBody, null, 2));

const shopList = shopsGetBody.data?.shops ?? [];
if (shopList.length === 0) {
  throw new Error("No authorized shops found.");
}

const selectedShop = shopList[0];
const shopId = selectedShop.id;
const shopCipher = selectedShop.cipher;

if (!shopCipher) {
  throw new Error(`No shop_cipher found for shop_id: ${shopId}`);
}

console.log(`Using shop_id: ${shopId}, shop_cipher: ${shopCipher}`);

Do not hard-code shop_cipher. Always use the cipher for the shop whose products you want to access.

#

§9 Search Products

Call Search Products after you have both accessToken and shopCipher. OpenAPI endpoint:

ItemValue
Method and pathPOST /product/202502/products/search
Required query parameterpage_size, valid range: 1 to 100
Optional query parameterspage_token, shop_cipher
Required headersx-tts-access-token, Content-Type: application/json
Optional request body filtersstatus, seller_skus, sku_ids, category_version, create_time_ge, create_time_le, update_time_ge, update_time_le, and other fields shown in the endpoint reference

Node.js SDK method call used in this guide:

PositionArgumentMeaning
1pageSizeMaps to query parameter page_size.
2accessTokenSent as x-tts-access-token.
3contentTypeUsually application/json.
4pageTokenOptional pagination token. Use undefined for the first page.
5searchProductsRequestBodyOptional JSON body for filters. Use {} or undefined when you do not need filters.
6shopCipherMaps to query parameter shop_cipher.

Different SDK languages may generate different positional argument orders. Do not copy the Java or Go method argument order into Node.js code. Check the generated Node.js SDK method signature in your downloaded SDK when you update the SDK.

const pageSize = 10;
const pageToken = undefined;
const searchProductsRequestBody = {};

const result = await client.api.ProductV202502Api.ProductsSearchPost(
  pageSize,
  accessToken,
  contentType,
  pageToken,
  searchProductsRequestBody,
  shopCipher
);

console.log("Search Products response:", JSON.stringify(result.body, null, 2));
#

§10 Complete Demo

import {
  AccessTokenTool,
  ClientConfiguration,
  TikTokShopNodeApiClient,
} from "../tiktok-shop-node-sdk";

const requiredEnv = (name: string): string => {
  const value = process.env[name];
  if (!value) {
    throw new Error(`Missing required environment variable: ${name}`);
  }
  return value;
};

ClientConfiguration.globalConfig.app_key = requiredEnv("TTS_APP_KEY");
ClientConfiguration.globalConfig.app_secret = requiredEnv("TTS_APP_SECRET");

const client = new TikTokShopNodeApiClient({
  config: {
    sandbox: process.env.TTS_SANDBOX === "true",
  },
});

const main = async () => {
  const contentType = "application/json";

  const authCode = requiredEnv("TTS_AUTH_CODE");
  const { body: tokenBody } = await AccessTokenTool.getAccessToken(authCode);
  console.log("getAccessToken response:", JSON.stringify(tokenBody, null, 2));

  const accessToken = tokenBody.data?.access_token;
  if (!accessToken) {
    throw new Error("Failed to get access token");
  }

  const { body: shopsGetBody } =
    await client.api.AuthorizationV202309Api.ShopsGet(accessToken, contentType);
  console.log("ShopsGet response:", JSON.stringify(shopsGetBody, null, 2));

  const shopList = shopsGetBody.data?.shops ?? [];
  if (shopList.length === 0) {
    throw new Error("No authorized shops found.");
  }

  const selectedShop = shopList[0];
  const shopId = selectedShop.id;
  const shopCipher = selectedShop.cipher;
  if (!shopCipher) {
    throw new Error(`No shop_cipher found for shop_id: ${shopId}`);
  }

  console.log(`Using shop_id: ${shopId}, shop_cipher: ${shopCipher}`);

  const pageSize = 10;
  const pageToken = undefined;
  const searchProductsRequestBody = {};

  const result = await client.api.ProductV202502Api.ProductsSearchPost(
    pageSize,
    accessToken,
    contentType,
    pageToken,
    searchProductsRequestBody,
    shopCipher
  );

  console.log("Search Products response:", JSON.stringify(result.body, null, 2));
};

main().catch((error) => {
  console.error(error);
  process.exitCode = 1;
});

Run the demo with environment variables:

TTS_APP_KEY="your_app_key" \
TTS_APP_SECRET="your_app_secret" \
TTS_AUTH_CODE="your_auth_code" \
TTS_SANDBOX="true" \
npm run start

Use TTS_SANDBOX="false" or omit TTS_SANDBOX for production.

#

§11 SDK Updates

SDK packages are app-specific and generated from the scopes and API versions available to your app. Record the SDK download date or package version in your project so you can reproduce generated method signatures. Update the SDK when:

TriggerAction
You enable new API scopes for the appDownload the latest SDK from the SDK download page.
An API adds a new version or sunsets an old versionRegenerate the SDK and check generated API class names such as ProductV202502Api.
The SDK framework changesReinstall dependencies from the SDK package and rerun your build.
Method parameter order changes after regenerationUpdate the code according to the generated Node.js method signature instead of copying examples from Java or Go.

For SDK update behavior, see Update SDK.

#