Tích hợp Node.js SDK
Làm theo hướng dẫn để cài đặt Node.js SDK vào dự án của bạn và thực hiện lệnh gọi API đầu tiên bằng SDK. API thường được dùng Search Products được sử dụng làm ví dụ.
Điều kiện tiên quyết
Trước khi tích hợp TikTok Shop API SDK vào dự án của bạn và thực hiện lệnh gọi API đầu tiên bằng SDK, bạn phải tạo một tài khoản người bán thử nghiệm.
Sau khi tài khoản được tạo, hãy truy cập vào tài khoản của bạn, đồng ý chia sẻ dữ liệu với TikTok Shop App thử nghiệm của bạn, và lấy authorization code trong URL trả về mà bạn đã chỉ định khi tạo TikTok Shop App thử nghiệm. Để được hướng dẫn, hãy tham khảo Tạo access token thử nghiệm.
Môi trường
Đảm bảo dự án của bạn đáp ứng tất cả các điều kiện sau:
- Node.js 16+
Cài đặt
Tham khảo các bước sau để tích hợp SDK vào dự án của bạn.
- Thêm thư mục đã giải nén vào thư mục dự án của bạn.
- Cấu hình các dependency trong
package.json:
JSONWord Wrap
{
"dependencies": {
"request":"2.88.2"
},
"devDependencies": {
"@types/request":"2.48.12",
"@types/node": "16",
"tslib":"2.6.2",
"typescript": "^4.9.5",
}
}
- Thêm nội dung sau vào file
tsconfig.jsoncủa bạn:
JSONWord Wrap
{
"esModuleInterop": true,
}
- Cài đặt các dependency bằng trình quản lý package mà bạn ưa thích:
SHELLWord Wrap
// npm
npm install
// yarn
yarn install
// pnpm
pnpm i
Lấy Access Token
Bạn có thể sử dụng phương thức có sẵn trong SDK để lấy Access Token:
JAVASCRIPTWord Wrap
const auth_code = "your_auth_code";
const { body } = await AccessTokenTool.getAccessToken(auth_code);
console.log('getAccessToken resp data := ', JSON.stringify(body, null, 2));
const access_token = body.data?.access_token;
if (!access_token) {
throw new Error("Failed to get access token");
}
Lấy Shop Cipher
shop_cipher là một parameter trong query của API request của Search Products. Bạn sẽ cần sử dụng thuộc tính này để truyền thông tin shop khi gửi yêu cầu tìm kiếm sản phẩm.
Để lấy shop_cipher, bạn sẽ cần gọi API Get Authorized Shops, API này cũng được bao gồm trong SDK dưới dạng một phương thức.
JAVASCRIPTWord Wrap
const contentType = 'application/json';
const { body: shopsGetBody } = await client.api.AuthorizationV202309Api.ShopsGet(access_token, contentType);
console.log('ShopsGet resp data := ', JSON.stringify(shopsGetBody, null, 2));
const shopList = shopsGetBody.data.shops || [];
if (shopList.length === 0) {
throw new Error("No authorized shops found.");
}
shopList.forEach((shop, index) => {
const shopId = shop.id;
const shopCipher = shop.cipher;
console.log(`shop_id: ${shopId}, shop_cipher: ${shopCipher}`);
});
Search Products
Với Access Token và Shop Cipher, bạn có thể gọi API để lấy danh sách các sản phẩm đáp ứng các điều kiện đã chỉ định. Mỗi khi bạn thực hiện một API request, TikTok Shop sẽ gửi lại cho bạn một response. Hãy truy cập response và xử lý các lỗi có thể xảy ra.
JAVASCRIPTWord Wrap
const result = await client.api.ProductV202502Api.ProductsSearchPost(1,access_token,'application/json',undefined,undefined, shop_cipher);
console.log('resp data := ',JSON.stringify(result.body, null, 2));
Code Demo
JAVASCRIPTWord Wrap
import { ClientConfiguration, TikTokShopNodeApiClient,AccessTokenTool } from ".";
ClientConfiguration.globalConfig.app_key = "XXXXXXXXX";
ClientConfiguration.globalConfig.app_secret =
"XXXXXXXXX";
const access_token = "XXXXXXXXX"
const shop_cipher="XXXXXXXXX"
const client = new TikTokShopNodeApiClient({
config: {
sandbox: false,
},
});
const main = async () => {
// 1. get access token
const auth_code = "your_auth_code";
const { body } = await AccessTokenTool.getAccessToken(auth_code);
console.log('getAccessToken resp data := ', JSON.stringify(body, null, 2));
const access_token = body.data?.access_token;
if (!access_token) {
throw new Error("Failed to get access token");
}
// 2. get shop cipher
const contentType = 'application/json';
const { body: shopsGetBody } = await client.api.AuthorizationV202309Api.ShopsGet(access_token, contentType);
console.log('ShopsGet resp data := ', JSON.stringify(shopsGetBody, null, 2));
const shopList = shopsGetBody.data.shops || [];
if (shopList.length === 0) {
throw new Error("No authorized shops found.");
}
shopList.forEach((shop, index) => {
const shopId = shop.id;
const shopCipher = shop.cipher;
console.log(`shop_id: ${shopId}, shop_cipher: ${shopCipher}`);
});
// 3. search products
const result = await client.api.ProductV202502Api.ProductsSearchPost(1,access_token,'application/json',undefined,undefined, shop_cipher);
console.log('resp data := ',JSON.stringify(result.body, null, 2));
};
main();