跳到內容

安裝指南

本指南涵蓋 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 檔案。