跳到內容

設定指南

本指南涵蓋從零到運作 Imbrace SDK 整合所需的一切 — 安裝、憑證、環境和服務 URL 覆蓋。


系統需求

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

安裝

從 npm 註冊表安裝:

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

單一倉儲 / 本機開發:

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 金鑰

選項 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 金鑰
const client = new ImbraceClient({
apiKey: process.env.IMBRACE_API_KEY,
organizationId: process.env.IMBRACE_ORGANIZATION_ID,
env: "stable", // 或使用 env: "stable"
});
// 客戶端 — 存取令牌(例如來自先前的登入,或你自己儲存的 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 Agent — 串流聊天
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 指向 dist/ 中的檔案

套件尚未建置:

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

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}'
UserWarning: ImbraceClient: no credentials provided

未傳遞 api_keyaccess_token。如果是有意的(例如僅登入流程),請忽略此警告。否則請檢查您的 .env 檔案。