OpenViking 支持通过 API 接口进行操作,调用 API 接口的前提条件是获取 API Key 并完成鉴权配置。本文介绍 API Key 的获取方法并提供完整调用示例。
在调用 OpenViking 的各项能力之前,请确保您已获取 API Key。
请按照以下步骤获取您的 API Key:
个人版用户在注册后系统会自动为其创建一个默认管理员账户,并生成对应的 API Key,可直接在控制台查看和使用。
每次调用 API 时,需在 HTTP 请求头中携带以下鉴权参数:
Header | 是否必选 | 示例 | 说明 |
|---|---|---|---|
Authorization | 是 |
| API Key 鉴权 |
X-OpenViking-Agent | 否 |
| Agent ID,用于指定本次请求操作的 Agent |
Content-Type | 是 |
| 请求体类型。JSON 接口使用 |
使用
curl -F或 SDK 的文件上传能力时,通常无需手动设置multipart/form-data的 boundary,由客户端自动生成即可。
以下为调用 /api/v1/resources 接口的多语言示例代码。请将示例中的占位符替换为您在控制台获取的实际值。
import json import requests BASE_URL = "https://api.vikingdb.cn-beijing.volces.com/openviking" API_KEY = "your-api-key" # 替换为您的 API Key AGENT_ID = "your-agent-id" # 替换为您的 Agent ID headers = { "Content-Type": "application/json", "Authorization": f"Bearer {API_KEY}", "X-OpenViking-Agent": AGENT_ID, } result = requests.post( f"{BASE_URL}/api/v1/resources", headers=headers, json={ "path": "http://example.com", "to": "viking://resources", "reason": "导入外部文档", }, timeout=120.0, ) result.raise_for_status() print(json.dumps(result.json(), ensure_ascii=False, indent=2))
curl -X POST https://xxx/api/v1/resources \ -H "Content-Type: application/json" \ -H "Authorization: Bearer {api_key}" \ -H "X-OpenViking-Agent: {agent_id}" \ -d '{"path": "http://example.com", "to": "viking://resources", "reason": "导入外部文档"}'
import fetch from "node-fetch"; const BASE_URL = "https://api.vikingdb.cn-beijing.volces.com/openviking"; // 替换为实际服务地址 const API_KEY = "your-api-key"; // 替换为您的 API Key const AGENT_ID = "your-agent-id"; // 替换为您的 Agent ID const resp = await fetch(`${BASE_URL}/api/v1/resources`, { method: "POST", headers: { "Content-Type": "application/json", "Authorization": `Bearer ${API_KEY}`, "X-OpenViking-Agent": AGENT_ID, }, body: JSON.stringify({ path: "http://example.com", to: "viking://resources", reason: "导入外部文档", }), }); console.log(await resp.json());
package main import ( "bytes" "encoding/json" "fmt" "net/http" ) const ( baseURL = "https://xxx" // 替换为实际服务地址 apiKey = "your-api-key" // 替换为您的 API Key agentID = "your-agent-id" // 替换为您的 Agent ID ) func main() { body, _ := json.Marshal(map[string]string{ "path": "http://example.com", "to": "viking://resources", "reason": "导入外部文档", }) req, _ := http.NewRequest("POST", baseURL+"/api/v1/resources", bytes.NewReader(body)) req.Header.Set("Content-Type", "application/json") req.Header.Set("Authorization", "Bearer "+apiKey) req.Header.Set("X-OpenViking-Agent", agentID) resp, _ := http.DefaultClient.Do(req) defer resp.Body.Close() var result map[string]interface{} json.NewDecoder(resp.Body).Decode(&result) fmt.Println(result) }
HTTP 状态码 | error.code | 描述 |
|---|---|---|
401 | AuthenticationError / UNAUTHENTICATED | 缺少 API Key、API Key 无效,或请求中的鉴权格式不正确 |
403 | PERMISSION_DENIED | API Key 权限不足 |