跳转到内容

安装指南

本指南涵盖 Imbrace SDK 集成所需的一切 — 从安装、凭证配置、环境设置到服务 URL 覆盖。


系统要求

要求最低版本
Node.js18.0.0+
npm8.0.0+
Python3.9+
pip23.0+

安装

从 npm 安装:

Terminal window
npm install @imbrace/sdk
# 或
yarn add @imbrace/sdk
# 或
pnpm add @imbrace/sdk

Monorepo / 本地开发:

Terminal window
# 步骤 1 — 安装依赖并构建
cd ts
npm install
npm run build
# 步骤 2(可选)— 全局链接以在其他项目中使用
npm link

在外部项目中:

Terminal window
npm link @imbrace/sdk

验证:

import { ImbraceClient } from '@imbrace/sdk'
console.log('SDK loaded:', typeof ImbraceClient) // 'function'

配置凭证

创建 .env 文件

SDK 不会自动读取环境变量 — 需要直接将凭证传入构造函数。.env 文件是用户约定;使用 dotenv 或框架的环境加载器读取。

# 凭证
IMBRACE_API_KEY=your_api_key_here
IMBRACE_ACCESS_TOKEN=your_jwt_token_here
# 组织 ID — 随每个请求发送
IMBRACE_ORGANIZATION_ID=your_org_id_here
# 可选:直接覆盖网关 URL
IMBRACE_BASE_URL=https://app-gatewayv2.imbrace.co

获取 API Key

方式一 — Imbrace Portal: 登录并进入 Settings → API Keys

方式二 — 通过 API(需要现有 access token):

Terminal window
curl -X POST https://app-gatewayv2.imbrace.co/private/backend/v1/third_party_token \
-H "x-access-token: <your_existing_token>" \
-H "Content-Type: application/json" \
-d '{"expirationDays": 30}'

所需值为 response.apiKey.apiKey


环境

名称网关 URL使用场景
develophttps://app-gateway.dev.imbrace.co内部开发
sandboxhttps://app-gateway.sandbox.imbrace.co集成测试
stablehttps://app-gatewayv2.imbrace.co生产(默认)

通过构造函数的 env 选项切换环境,或使用 baseUrl 直接覆盖 URL:

const client = new ImbraceClient({ env: 'sandbox' })

初始化客户端

import { ImbraceClient } from '@imbrace/sdk'
// 服务端 — API Key
const client = new ImbraceClient({
apiKey: process.env.IMBRACE_API_KEY,
organizationId: process.env.IMBRACE_ORGANIZATION_ID,
baseUrl: 'https://app-gatewayv2.imbrace.co', // 或使用 env: 'stable'
})
// 客户端 — Access Token(例如 OTP 登录后)
const client = new ImbraceClient({
accessToken: process.env.IMBRACE_ACCESS_TOKEN,
baseUrl: 'https://app-gatewayv2.imbrace.co',
})
// OTP 登录流程
const anon = new ImbraceClient({ baseUrl: 'https://app-gatewayv2.imbrace.co' })
await anon.requestOtp('user@example.com')
await anon.loginWithOtp('user@example.com', '123456')
// 令牌自动存储在客户端

快速使用示例

import { ImbraceClient } from '@imbrace/sdk'
const client = new ImbraceClient({ apiKey: process.env.IMBRACE_API_KEY })
// 获取当前用户信息
const me = await client.platform.getMe()
// 频道列表
const channels = await client.channel.listChannels()
// 发送消息
await client.channel.sendMessage('conv_123', { content: '你好!', type: 'text' })
// AI 补全
const result = await client.ai.complete({
model: 'gpt-4o',
messages: [{ role: 'user', content: '总结这份报告。' }],
})
// AI 流式输出
for await (const chunk of client.ai.stream({ model: 'gpt-4o', messages: [...] })) {
process.stdout.write(chunk.choices[0]?.delta?.content ?? '')
}
// AI Agent — 流式对话
const response = await client.aiAgent.streamChat({
id: 'chat_id',
assistant_id: 'asst_abc',
organization_id: 'org_abc',
messages: [{ role: 'user', content: '你好' }],
})

覆盖服务 URL

当某个微服务运行在不同地址时使用(例如本地开发、独立 staging)。

const client = new ImbraceClient({
env: 'develop',
services: {
aiAgent: 'http://localhost:4000/ai-agent',
dataBoard: 'http://localhost:3001/data-board',
channelService: 'http://localhost:3002/channel-service',
},
})

所有有效的服务键:

Python 键TypeScript 键服务
gatewaygatewayApp Gateway
platformplatformPlatform service
channel_servicechannelServiceChannel service
data_boarddataBoardData Board
backendbackendBoard/Items backend (/v1/backend)
ipsipsIPS service
aiaiAI service
marketplacesmarketplacesMarketplace service
file_servicefileServiceFile service
message_suggestionmessageSuggestionMessage Suggestion service
predictpredictPredict service
activepiecesactivepiecesActivePieces
ai_agentaiAgentAI Agent service

常见错误排查

Cannot find package '@imbrace/sdk'

包未链接。从 ts 重新运行:

Terminal window
npm link
cd /path/to/your-project && npm link @imbrace/sdk
ERR_MODULE_NOT_FOUND(dist/ 中的文件)

包尚未构建:

Terminal window
cd ts && npm run build
ModuleNotFoundError: No module named 'imbrace'
Terminal window
cd py && pip install -e ".[dev]"
401 Unauthorized

API Key 已过期或无效。生成新密钥:

Terminal window
curl -X POST https://app-gatewayv2.imbrace.co/private/backend/v1/third_party_token \
-H "x-access-token: <your_existing_token>" \
-H "Content-Type: application/json" \
-d '{"expirationDays": 30}'
UserWarning: ImbraceClient: no credentials provided

未传入 api_keyaccess_token。如果是有意为之(例如仅用于登录),可忽略此警告。否则请检查 .env 文件。