频道3 类型脚本 SDK
官方类型SDK 频道3 API ——通用产品数据库为AI购物应用和代理提供动力.
文档
API 文档和指南全文见 channel3.docs.build withfern.com 互联网档案馆的存檔,存档日期2013-12-02。 。 。 .
运行时间兼容性
| 所需经费 | 版本 |
|---|---|
| 节点.js | >= 18.0.0 |
| 类型脚本 | >= 4.5 (可选——在普通联合来文中工作) |
| 模块系统 | 无害环境管理和共同JS(双包) |
| 浏览器 | 任何环境 fetch (或通过多填充) |
| 边缘运行时间 | Cloudflare 工人,韦尔塞尔边缘,德诺 |
| 原生反应 | 支助 |
安装
npm install @channel3/sdk
# or
yarn add @channel3/sdk
# or
pnpm add @channel3/sdk
认证
SDK通过一个 x-api-key 头曰. 初始化客户端时通过您的密钥 :
import { Channel3Client } from "@channel3/sdk";
const client = new Channel3Client({ apiKey: "YOUR_API_KEY" });
您也可以为动态密钥解析提供函数( 用于密钥旋转 ):
const client = new Channel3Client({
apiKey: async () => getKeyFromVault(),
});
使用量
搜索产品
const response = await client.products.search({
query: "organic cotton t-shirt",
limit: 10,
});
for (const product of response.products) {
console.log(product.title, product.offers?.[0]?.price);
}
通过结果粘贴
搜索答复包括a nextPageToken 用于光标平面图 :
let pageToken: string | undefined;
do {
const response = await client.products.search({
query: "sneakers",
pageToken,
});
for (const product of response.products) {
console.log(product.title);
}
pageToken = response.nextPageToken ?? undefined;
} while (pageToken);
通过图像进行视觉搜索
const response = await client.products.imageSearch({
imageUrl: "https://example.com/photo.jpg",
});
查找类似产品
const response = await client.products.similar({
productId: "prod-12345",
});
通过 URL 查找产品
const product = await client.products.lookup({
url: "https://example.com/product/abc",
});
浏览品牌和类别
标注品牌和类别:
const brands = await client.brands.list({ limit: 50 });
console.log(brands.items); // Brand[]
console.log(brands.nextCursor); // pass as `cursor` for the next page
const categories = await client.categories.list({ pageSize: 50 });
轨价
// Start tracking
await client.priceTracking.start({ canonicalProductId: "prod-12345" });
// Get price history
const history = await client.priceTracking.getHistory({ canonicalProductId: "prod-12345" });
// List active subscriptions
const subs = await client.priceTracking.listSubscriptions();
// Stop tracking
await client.priceTracking.stop({ canonicalProductId: "prod-12345" });
类型智能
所有请求和响应类型均导出于 Channel3 方便发现的命名空间 :
import { Channel3 } from "@channel3/sdk";
// Full autocomplete on request fields
const request: Channel3.SearchRequest = {
query: "wireless headphones",
limit: 20,
filters: { gender: "female" },
config: { country: "US", currency: "USD" },
};
// Response types are also typed
const response: Channel3.SearchResponse = await client.products.search(request);
资源客户端可通过点标记访问——类型 client. 在编辑器中发现 :
| 访问器 | 方法 |
|---|---|
client.products | search, (中文(简体) ), imageSearch, (中文(简体) ), similar, (中文(简体) ), lookup, (中文(简体) ), retrieve, (中文(简体) ), enrich |
client.brands | list, (中文(简体) ), search, (中文(简体) ), get |
client.categories | list, (中文(简体) ), search |
client.priceTracking | start, (中文(简体) ), stop, (中文(简体) ), getHistory, (中文(简体) ), listSubscriptions |
client.websites | list |
处理错误
SDK 每 HTTP 状态代码丢入打入错误 :
import { Channel3Error, Channel3TimeoutError } from "@channel3/sdk";
try {
await client.products.search({ query: "test" });
} catch (err) {
if (err instanceof Channel3Error) {
console.log(err.statusCode); // 401, 402, 422, etc.
console.log(err.message);
console.log(err.body);
}
}
高级
检索
在 5xx 和网络错误( 默认: 2 重试) 上自动回转 :
const response = await client.products.search(
{ query: "shoes" },
{ maxRetries: 5 },
);
超时
默认超时为60秒. 覆盖每个客户或每个请求:
const client = new Channel3Client({
apiKey: "YOUR_API_KEY",
timeoutInSeconds: 30,
});
// Per-request
await client.products.search({ query: "shoes" }, { timeoutInSeconds: 10 });
中止请求
const controller = new AbortController();
await client.products.search({ query: "shoes" }, { abortSignal: controller.signal });
原始反应访问
访问 HTTP 头和状态并附解析数据 :
const response = await client.products.search({ query: "shoes" });
console.log(response.rawResponse.status);
console.log(response.rawResponse.headers);
参考文献
参见全部 API 参考文献 : 页:1。 。 。 .
捐款
见 捐款额 (c) 发展结构和准则.