跳转到内容

设置指南

本指南涵盖了你从零开始到完成 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_GATEWAY_URL=https://app-gatewayv2.imbrace.co

获取 API Key

选项 1 — Imbrace 门户: 登录后转到 设置 → API 密钥

选项 2 — 通过 API(需要现有的访问令牌):

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,
env: "stable", // 或使用 env: "stable"
});
// 客户端 — Access Token(例如来自之前的登录,或你自己存储的 token)
const client = new ImbraceClient({
accessToken: process.env.IMBRACE_ACCESS_TOKEN,
env: "stable",
});
// 密码登录(POST /v1/login/sign_in)— 终端用户应用最常用的方式。
// 在一次调用中返回 { token: "login_acc_...", organizations: [...] };用
// selectOrganization() 选择其中一个,SDK 会自动切换为该组织的 acc_ token。
const anon = new ImbraceClient({ env: "stable" });
const { organizations } = await anon.login("user@example.com", "password");
await anon.selectOrganization(organizations[0].id);
// OTP 登录流程(替代方案 — 用 email-OTP 体验替代密码)
await anon.requestOtp("user@example.com");
await anon.loginWithOtp("user@example.com", "123456");
// 令牌自动存储在客户端上

CLI 安装

Terminal window
# 全局安装
npm install -g @imbrace/cli
# 登录开始使用
imbrace login --api-key api_xxx...

其他安装方法请参见 CLI 安装,完整命令参考请参见 CLI 命令


快速使用示例

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.list()
// 发送消息
await client.messages.send({ type: 'text', text: 'Hello!' })
// AI 补全
const result = await client.ai.complete({
model: 'gpt-4o',
messages: [{ role: 'user', content: 'Summarize this.' }],
})
// AI 流式传输
for await (const chunk of client.ai.stream({ model: 'gpt-4o', messages: [...] })) {
process.stdout.write(chunk.choices[0]?.delta?.content ?? '')
}
// AI 代理 — 流式聊天
const response = await client.aiAgent.streamChat({
id: 'chat_id',
assistant_id: 'asst_abc',
messages: [{ role: 'user', content: 'Hello' }],
})
const reader = response.body!.getReader()
const decoder = new TextDecoder()
while (true) {
const { done, value } = await reader.read()
if (done) break
console.log(decoder.decode(value))
}

覆盖服务 URL

当微服务运行在不同地址时使用(例如本地开发、专用预发布环境)。

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
backendbackend旧版单体(/v1/backend)— 用于身份验证 signin、模板和少数遗留路由
ipsipsIPS service
aiaiAI service
marketplacesmarketplacesMarketplace service
file_servicefileServiceFile service
message_suggestionmessageSuggestionMessage Suggestion service
predictpredictPredict service
workflow_engineworkflowEngineWorkflows
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 for files in 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 文件。