功能定价关于文章文档
开发者

API、SDK、MCP 和 webhook。

开发者平台快速开始身份验证API 参考SDKMCPWebhook错误分页速率限制幂等性更新日志迁移与版本控制策略
原始 API 文档OpenAPI YAMLAsyncAPI YAML
  1. 首页
  2. /
  3. 开发者
  4. /
  5. SDK

SDK

官方 TypeScript SDK(@fabhub/sdk)依据公共 OpenAPI 契约生成并测试,因此其方法绝不会与公共路由产生偏离。

安装

npm install @fabhub/sdk

需要 Node.js 22+。


身份验证

使用 API 密钥构造客户端。OAuth 签发的访问令牌本身就是一个 pk_* 密钥,因此将其作为相同的 apiKey 选项传入即可。

import { FabHubClient } from '@fabhub/sdk';

export const fabhub = new FabHubClient({
  apiKey: process.env.FABHUB_API_KEY,
});

方法

Client methodEndpointRequired scope
getCapabilitiesGET /v1authenticated
getAuthContextGET /v1/auth/contextauthenticated
listItemsGET /v1/itemsitems:read
createItemPOST /v1/itemsitems:write
getItemGET /v1/items/{item_id}items:read
listItemIngredientsGET /v1/items/{item_id}/ingredientsitems:read
listItemSuppliersGET /v1/items/{item_id}/suppliersitems:read
createItemIngredientPOST /v1/items/{item_id}/ingredientsitems:write
updateItemIngredientPATCH /v1/items/{item_id}/ingredients/{ingredient_id}items:write
createItemSupplierPOST /v1/items/{item_id}/suppliersitems:write
updateItemSupplierPATCH /v1/items/{item_id}/suppliers/{supplier_id}items:write
deleteItemIngredientDELETE /v1/items/{item_id}/ingredients/{ingredient_id}items:write
deleteItemSupplierDELETE /v1/items/{item_id}/suppliers/{supplier_id}items:write
getOrderGET /v1/orders/{order_id}orders:read
listOrderLinesGET /v1/orders/{order_id}/linesorders:read
createOrderPOST /v1/ordersorders:write
updateOrderPATCH /v1/orders/{order_id}orders:write
updateItemPATCH /v1/items/{item_id}items:write
deleteItemDELETE /v1/items/{item_id}items:write
listOrdersGET /v1/ordersorders:read
listContactsGET /v1/contactscontacts:read
getContactGET /v1/contacts/{contact_id}contacts:read
createContactPOST /v1/contactscontacts:write
updateContactPATCH /v1/contacts/{contact_id}contacts:write
deleteContactDELETE /v1/contacts/{contact_id}contacts:write
getOrganizationGET /v1/organizationorganization:read
getUsageSummaryGET /v1/usageusage:read
listStockLevelsGET /v1/stock-levelsinventory:read
listStockDocumentsGET /v1/stock-documentsinventory:read
getStockDocumentGET /v1/stock-documents/{document_id}inventory:read
createStockDocumentPOST /v1/stock-documentsinventory:write
listStockDocumentLinesGET /v1/stock-documents/{document_id}/linesinventory:read
createStockDocumentLinePOST /v1/stock-documents/{document_id}/linesinventory:write
updateStockDocumentLinePATCH /v1/stock-documents/{document_id}/lines/{line_id}inventory:write
submitStockDocumentPOST /v1/stock-documents/{document_id}/submitinventory:write
cancelStockDocumentPOST /v1/stock-documents/{document_id}/cancelinventory:write
approveStockDocumentPOST /v1/stock-documents/{document_id}/approveinventory:write
deleteStockDocumentDELETE /v1/stock-documents/{document_id}inventory:write
deleteStockDocumentLineDELETE /v1/stock-documents/{document_id}/lines/{line_id}inventory:write
updateStockDocumentPATCH /v1/stock-documents/{document_id}inventory:write
listWebhooksGET /v1/webhookswebhooks:read
createWebhookPOST /v1/webhookswebhooks:write
getWebhookGET /v1/webhooks/{webhook_id}webhooks:read
updateWebhookPATCH /v1/webhooks/{webhook_id}webhooks:write
deleteWebhookDELETE /v1/webhooks/{webhook_id}webhooks:delete
listWebhookDeliveriesGET /v1/webhooks/{webhook_id}/deliverieswebhooks:deliveries:read
listAuditEventsGET /v1/audit/eventsaudit:read

分页

let page = 1;
let totalPages = 1;
do {
  const result = await fabhub.listItems({ page, pageSize: 100 });
  // handle result.data
  totalPages = result.pagination.totalPages;
  page += 1;
} while (page <= totalPages);

幂等写入

const { data: item } = await fabhub.createItem(
  { name: 'Widget', itemType: 'product' },
  { idempotencyKey: crypto.randomUUID() },
);

类型化错误

import { FabHubApiError } from '@fabhub/sdk';

try {
  await fabhub.getItem('does-not-exist');
} catch (err) {
  if (err instanceof FabHubApiError) {
    console.error(err.status, err.code, err.message); // e.g. 404 NOT_FOUND ...
  }
}

Webhook 签名验证

import { verifyFabHubWebhookSignature } from '@fabhub/sdk';

const result = verifyFabHubWebhookSignature({
  signingSecret: process.env.FABHUB_WEBHOOK_SECRET,
  rawBody, // the exact raw request body string/bytes, before JSON parsing
  timestamp: req.headers['x-fabhub-timestamp'],
  signature: req.headers['x-fabhub-signature'],
});

if (!result.ok) {
  // reject: result.reason explains why (e.g. 'invalid_signature')
}

偏离保护

SDK 操作映射会在 CI 中对照 OpenAPI 进行检查,因此公共 SDK 方法不会悄然偏离公共路由,也不会指向内部接口。

首页功能定价关于文章文档开发者
© FabHub隐私与 Cookie条款无障碍