# Cohere API Key 作为 Bearer Token 使用指南 ## 📌 重要说明 **Cohere API Key 本身就是 Bearer Token!** 你不需要"创建"一个新的 Bearer authorization key。Cohere API Key (`4pLcF0CGE7LeDmAudBQHdvAxGaKwNOKfxUGkHb5C`) 可以直接用作 Bearer token 在 HTTP 请求中进行身份验证。 --- ## 🔑 Bearer Token 格式 Bearer token 的标准格式是: ``` Authorization: Bearer ``` 对于 Cohere API,格式为: ``` Authorization: Bearer 4pLcF0CGE7LeDmAudBQHdvAxGaKwNOKfxUGkHb5C ``` --- ## 💻 在不同场景中使用 ### 1. Python 代码中使用 ```python import requests # Cohere API Key api_key = "4pLcF0CGE7LeDmAudBQHdvAxGaKwNOKfxUGkHb5C" # 设置请求头 headers = { "Authorization": f"Bearer {api_key}", "Content-Type": "application/json", } # 调用 Cohere API response = requests.get( "https://api.cohere.ai/v1/models", headers=headers, timeout=10, ) print(response.json()) ``` ### 2. cURL 命令中使用 ```bash curl -X GET "https://api.cohere.ai/v1/models" \ -H "Authorization: Bearer 4pLcF0CGE7LeDmAudBQHdvAxGaKwNOKfxUGkHb5C" \ -H "Content-Type: application/json" ``` ### 3. JavaScript/Node.js 中使用 ```javascript const apiKey = "4pLcF0CGE7LeDmAudBQHdvAxGaKwNOKfxUGkHb5C"; fetch("https://api.cohere.ai/v1/models", { method: "GET", headers: { "Authorization": `Bearer ${apiKey}`, "Content-Type": "application/json", }, }) .then((response) => response.json()) .then((data) => console.log(data)); ``` ### 4. n8n HTTP Request 节点中使用 #### 方法 1: 直接在 Header 中设置 1. 添加 **HTTP Request** 节点 2. 配置如下: - **Method**: `GET` 或 `POST` - **URL**: `https://api.cohere.ai/v1/models`(或其他 Cohere API 端点) - **Authentication**: `None` - **Send Headers**: ✅ 启用 - **Headers**: ``` Authorization: Bearer 4pLcF0CGE7LeDmAudBQHdvAxGaKwNOKfxUGkHb5C Content-Type: application/json ``` #### 方法 2: 使用表达式(推荐,更安全) 在 n8n 中使用表达式,避免硬编码 API Key: 1. 在 **Headers** 中设置: - **Name**: `Authorization` - **Value**: `Bearer {{ $env.COHERE_API_KEY }}` 或使用凭证 2. 或者使用 n8n 的凭证系统: - 创建 **Generic Credential Type** 凭证 - 存储 API Key - 在 HTTP Request 节点中引用 --- ## 🔧 在 n8n 中创建通用 Bearer Token 凭证 如果你想在 n8n 中创建一个通用的 Bearer Token 凭证(用于 HTTP Request 节点),可以这样做: ### 步骤 1: 创建 Generic Credential Type 1. 访问: https://n8n.citupro.com/home/credentials 2. 点击 **"Add Credential"** 3. 搜索 **"Generic Credential Type"** 或 **"HTTP Header Auth"** 4. 选择相应的凭证类型 ### 步骤 2: 配置凭证 **选项 A: Generic Credential Type** - **Name**: `Cohere Bearer Token` - **Credential Data**: ```json { "apiKey": "4pLcF0CGE7LeDmAudBQHdvAxGaKwNOKfxUGkHb5C" } ``` **选项 B: HTTP Header Auth**(如果可用) - **Name**: `Cohere Bearer Token` - **Header Name**: `Authorization` - **Header Value**: `Bearer 4pLcF0CGE7LeDmAudBQHdvAxGaKwNOKfxUGkHb5C` ### 步骤 3: 在 HTTP Request 节点中使用 1. 打开 HTTP Request 节点 2. 在 **Authentication** 字段中选择创建的凭证 3. 或者手动在 **Headers** 中添加: ``` Authorization: Bearer {{ $credentials.cohereBearerToken.apiKey }} ``` --- ## 📝 完整示例:调用 Cohere Rerank API ### Python 示例 ```python import requests api_key = "4pLcF0CGE7LeDmAudBQHdvAxGaKwNOKfxUGkHb5C" headers = { "Authorization": f"Bearer {api_key}", "Content-Type": "application/json", } # Rerank API 请求 data = { "model": "rerank-multilingual-v3.0", "query": "什么是人工智能?", "documents": [ "人工智能是计算机科学的一个分支", "机器学习是人工智能的核心技术", "深度学习是机器学习的一个子集", ], "top_n": 2, } response = requests.post( "https://api.cohere.ai/v1/rerank", headers=headers, json=data, timeout=30, ) print(response.json()) ``` ### n8n HTTP Request 节点配置 **节点配置**: - **Method**: `POST` - **URL**: `https://api.cohere.ai/v1/rerank` - **Authentication**: `None` - **Send Headers**: ✅ - **Headers**: ``` Authorization: Bearer 4pLcF0CGE7LeDmAudBQHdvAxGaKwNOKfxUGkHb5C Content-Type: application/json ``` - **Send Body**: ✅ - **Body Content Type**: `JSON` - **JSON Body**: ```json { "model": "rerank-multilingual-v3.0", "query": "{{ $json.query }}", "documents": {{ $json.documents }}, "top_n": 2 } ``` --- ## 🛠️ 实用脚本 我已经创建了一个测试脚本 `scripts/test_cohere_api_key.py`,它展示了如何使用 Bearer token: ```bash python scripts/test_cohere_api_key.py 4pLcF0CGE7LeDmAudBQHdvAxGaKwNOKfxUGkHb5C ``` 这个脚本会: 1. 使用 Bearer token 格式调用 Cohere API 2. 验证 API Key 是否有效 3. 测试 Rerank API 功能 --- ## 🔒 安全最佳实践 1. **不要在代码中硬编码 API Key** - 使用环境变量 - 使用配置文件(不提交到 Git) - 使用 n8n 凭证系统 2. **使用环境变量**: ```python import os api_key = os.environ.get("COHERE_API_KEY") ``` 3. **在 n8n 中使用表达式**: ``` Bearer {{ $env.COHERE_API_KEY }} ``` 4. **定期轮换 API Key** - 在 Cohere Dashboard 中生成新 Key - 更新所有使用该 Key 的地方 --- ## 📚 相关文档 - [Cohere API 文档](https://docs.cohere.com/) - [Cohere Rerank API](https://docs.cohere.com/docs/reranking) - [n8n HTTP Request 节点文档](https://docs.n8n.io/integrations/builtin/core-nodes/n8n-nodes-base.httprequest/) - [Bearer Token 认证说明](https://oauth.net/2/bearer-tokens/) --- ## ✅ 总结 **你不需要创建新的 Bearer authorization key!** - Cohere API Key 就是 Bearer token - 格式: `Authorization: Bearer ` - 直接在 HTTP 请求头中使用即可 - 在 n8n 中,可以使用 HTTP Request 节点 + Header 配置,或使用 Cohere 专用节点(已配置好凭证) **你的 Cohere API Key**: `4pLcF0CGE7LeDmAudBQHdvAxGaKwNOKfxUGkHb5C` **Bearer Token 格式**: `Bearer 4pLcF0CGE7LeDmAudBQHdvAxGaKwNOKfxUGkHb5C` --- **最后更新**: 2026-01-19