安装指南
本指南涵盖 Imbrace SDK 集成所需的一切 — 从安装、凭证配置、环境设置到服务 URL 覆盖。
系统要求
| 要求 | 最低版本 |
|---|---|
| Node.js | 18.0.0+ |
| npm | 8.0.0+ |
| Python | 3.9+ |
| pip | 23.0+ |
安装
从 npm 安装:
npm install @imbrace/sdk# 或yarn add @imbrace/sdk# 或pnpm add @imbrace/sdkMonorepo / 本地开发:
# 步骤 1 — 安装依赖并构建cd tsnpm installnpm run build
# 步骤 2(可选)— 全局链接以在其他项目中使用npm link在外部项目中:
npm link @imbrace/sdk验证:
import { ImbraceClient } from '@imbrace/sdk'console.log('SDK loaded:', typeof ImbraceClient) // 'function'从 PyPI 安装:
pip install imbrace# 或uv add imbraceMonorepo / 本地开发(可编辑模式):
cd pypip install -e ".[dev]"[dev] 标志额外安装:pytest, pytest-asyncio, pytest-httpx, ruff, mypy。
验证:
from imbrace import ImbraceClientprint("SDK loaded:", ImbraceClient)配置凭证
创建 .env 文件
SDK 不会自动读取环境变量 — 需要直接将凭证传入构造函数。.env 文件是用户约定;使用 dotenv 或框架的环境加载器读取。
# 凭证IMBRACE_API_KEY=your_api_key_hereIMBRACE_ACCESS_TOKEN=your_jwt_token_here
# 组织 ID — 随每个请求发送IMBRACE_ORGANIZATION_ID=your_org_id_here
# 可选:直接覆盖网关 URLIMBRACE_BASE_URL=https://app-gatewayv2.imbrace.co获取 API Key
方式一 — Imbrace Portal: 登录并进入 Settings → API Keys。
方式二 — 通过 API(需要现有 access token):
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 | 使用场景 |
|---|---|---|
develop | https://app-gateway.dev.imbrace.co | 内部开发 |
sandbox | https://app-gateway.sandbox.imbrace.co | 集成测试 |
stable | https://app-gatewayv2.imbrace.co | 生产(默认) |
通过构造函数的 env 选项切换环境,或使用 baseUrl 直接覆盖 URL:
const client = new ImbraceClient({ env: 'sandbox' })client = ImbraceClient(env="sandbox")初始化客户端
import { ImbraceClient } from '@imbrace/sdk'
// 服务端 — API Keyconst 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 osfrom imbrace import ImbraceClient
# 服务端 — API Keyclient = ImbraceClient( api_key=os.environ["IMBRACE_API_KEY"], organization_id=os.environ.get("IMBRACE_ORGANIZATION_ID"), env="stable",)
# 客户端 — Access Tokenclient = ImbraceClient( access_token="eyJhbGci...", organization_id="org_xxx",)
# OTP 登录流程anon = ImbraceClient(env="stable")anon.request_otp("user@example.com")anon.login_with_otp("user@example.com", "123456")from imbrace import AsyncImbraceClient
async def main(): async with AsyncImbraceClient(api_key="sk-...") as client: me = await client.platform.get_me() print(me)快速使用示例
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: '你好' }],})from imbrace import ImbraceClient
with ImbraceClient(api_key="sk-...") as client: # 获取用户信息 me = client.platform.get_me()
# 频道和看板列表 channels = client.channel.list_channels() boards = client.boards.list() items = client.boards.get_items(boards[0]["id"])
# AI 补全 result = client.ai.complete( model="gpt-4o", messages=[{"role": "user", "content": "你好"}], )
# AI Agent — 流式对话 response = client.ai_agent.stream_chat({ "id": "chat_id", "assistant_id": "asst_abc", "organization_id": "org_abc", "messages": [{"role": "user", "content": "你好"}], }) for line in response.iter_lines(): print(line)覆盖服务 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', },})client = ImbraceClient( env="develop", services={ "ai_agent": "http://localhost:4000/ai-agent", "data_board": "http://localhost:3001/data-board", "channel_service": "http://localhost:3002/channel-service", },)所有有效的服务键:
| Python 键 | TypeScript 键 | 服务 |
|---|---|---|
gateway | gateway | App Gateway |
platform | platform | Platform service |
channel_service | channelService | Channel service |
data_board | dataBoard | Data Board |
backend | backend | Board/Items backend (/v1/backend) |
ips | ips | IPS service |
ai | ai | AI service |
marketplaces | marketplaces | Marketplace service |
file_service | fileService | File service |
message_suggestion | messageSuggestion | Message Suggestion service |
predict | predict | Predict service |
activepieces | activepieces | ActivePieces |
ai_agent | aiAgent | AI Agent service |
常见错误排查
Cannot find package '@imbrace/sdk'
包未链接。从 ts 重新运行:
npm linkcd /path/to/your-project && npm link @imbrace/sdkERR_MODULE_NOT_FOUND(dist/ 中的文件)
包尚未构建:
cd ts && npm run buildModuleNotFoundError: No module named 'imbrace'
cd py && pip install -e ".[dev]"401 Unauthorized
API Key 已过期或无效。生成新密钥:
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_key 或 access_token。如果是有意为之(例如仅用于登录),可忽略此警告。否则请检查 .env 文件。