設定指南
本指南涵蓋從零到運作 Imbrace SDK 整合所需的一切 — 安裝、憑證、環境和服務 URL 覆蓋。
系統需求
| 需求 | 最低版本 |
|---|---|
| Node.js | 18.0.0+ |
| npm | 8.0.0+ |
| Python | 3.9+ |
| pip | 23.0+ |
安裝
從 npm 註冊表安裝:
npm install @imbrace/sdk# oryarn add @imbrace/sdk# orpnpm add @imbrace/sdk單一倉儲 / 本機開發:
# 步驟 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# oruv add imbrace單一倉儲 / 本機開發(editable 模式):
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_GATEWAY_URL=https://app-gatewayv2.imbrace.co取得 API 金鑰
選項 1 — Imbrace 入口網站: 登入後前往設定 → API 金鑰。
選項 2 — 透過 API(需要現有的存取令牌):
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 金鑰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");// 令牌自動儲存在客戶端上import osfrom imbrace import ImbraceClient
# 伺服器端 — API 金鑰client = ImbraceClient( api_key=os.environ["IMBRACE_API_KEY"], organization_id=os.environ.get("IMBRACE_ORGANIZATION_ID"), env="stable",)
# 客戶端 — 存取令牌client = ImbraceClient( access_token=os.environ["IMBRACE_ACCESS_TOKEN"], organization_id="org_xxx",)
# 密碼登入(POST /platform/v1/login/authenticate)— 終端使用者應用最常見的方式。# login() 回傳使用者所屬的組織;選擇其一後用 select_organization()# 將短期 login_acc_ token 換成綁定組織的 acc_ token。anon = ImbraceClient(env="stable")res = anon.login("user@example.com", "password")anon.select_organization(res["organizations"][0]["organization_id"])
# OTP 登入流程(替代方案 — 用 email-OTP 體驗取代密碼)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=os.environ["IMBRACE_API_KEY"]) as client: me = await client.platform.get_me() print(me)CLI 安裝
# 全域安裝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))}from imbrace import ImbraceClientfrom imbrace.types.ai import CompletionInput, CompletionMessage
with ImbraceClient(api_key=os.environ["IMBRACE_API_KEY"]) as client: # 取得目前使用者 me = client.platform.get_me()
# 列出頻道和 boards channels = client.channel.list() boards_res = client.boards.list() boards_data = boards_res.get("data", []) if boards_data: board_id = boards_data[0].get("_id") or boards_data[0]["id"] items = client.boards.list_items(board_id)
# AI 補全 result = client.ai.complete(CompletionInput( model="gpt-4o", messages=[CompletionMessage(role="user", content="Hello")], ))
# AI Agent — 串流聊天 response = client.ai_agent.stream_chat({ "id": "chat_id", "assistant_id": "asst_abc", "messages": [{"role": "user", "content": "Hello"}], }) for line in response.iter_lines(): print(line)覆蓋服務 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", },});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 | 舊版單體(/v1/backend)— 用於身份驗證 signin、模板和少數遺留路由 |
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 |
workflow_engine | workflowEngine | Workflows |
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 金鑰已過期或無效。產生一個新的:
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 檔案。