快速上手
只需几分钟,即可从零开始完成首次经过身份验证的调用。
1. 创建 API 密钥
在应用中打开 Settings -> Integrations -> API,并以集成所需的最小作用域集合(例如 items:read)创建租户 API 密钥。完整密钥(pk_live_... 或 pk_test_...)仅显示一次,请将其存放在服务端的密钥管理器或部署环境中,切勿放入客户端代码或版本控制。
2. 进行首次调用
curl https://api.fabhub.app/v1 \
-H "X-API-Key: $FABHUB_API_KEY"
GET /v1 返回你的凭据可访问的能力。若要检查凭据本身,其作用域、套餐和速率限制层级,请调用 GET /v1/auth/context:
curl https://api.fabhub.app/v1/auth/context \
-H "X-API-Key: $FABHUB_API_KEY"
{
"data": {
"tenant": { "id": "0e2b...c1", "plan": "pro" },
"api_key": {
"id": "key_8f...",
"prefix": "pk_live_8f3a",
"environment": "production",
"scopes": ["items:read", "items:write", "webhooks:write"],
"rate_limit_tier": "standard",
"expires_at": null
}
}
}
3. 安装 SDK
npm install @fabhub/sdk
import { FabHubClient } from '@fabhub/sdk';
const fabhub = new FabHubClient({ apiKey: process.env.FABHUB_API_KEY });
// List the first page of items
const items = await fabhub.listItems({ pageSize: 20 });
console.log(items.data.length, 'of', items.pagination.total);
// Read your organization
const { data: organization } = await fabhub.getOrganization();
console.log(organization.name, organization.plan);
4. 创建内容(带幂等性)
写调用接受 Idempotency-Key,因此重试的请求永远不会创建重复项:
const { data: item } = await fabhub.createItem(
{ name: 'Widget', itemType: 'product', salePrice: 19.99 },
{ idempotencyKey: crypto.randomUUID() },
);
5. 处理错误
import { FabHubApiError } from '@fabhub/sdk';
try {
await fabhub.listItems();
} catch (err) {
if (err instanceof FabHubApiError) {
console.error(err.status, err.code, err.message);
}
}