[ PROMPT_NODE_24848 ]
Heygen Best Practices 认证
[ SKILL_DOCUMENTATION ]
# HeyGen 认证
所有 HeyGen API 请求都需要使用在 `X-Api-Key` 请求头中传递的 API 密钥进行认证。
## 获取你的 API 密钥
1. 登录你的 HeyGen 账户:https://app.heygen.com
2. 导航至 Settings > API
3. 复制你的 API 密钥
## 环境设置
将你的 API 密钥安全地存储为环境变量:
bash
export HEYGEN_API_KEY="your-api-key-here"
对于 `.env` 文件:
HEYGEN_API_KEY=your-api-key-here
## 发起认证请求
### curl
bash
curl -X GET "https://api.heygen.com/v2/avatars"
-H "X-Api-Key: $HEYGEN_API_KEY"
### TypeScript/JavaScript (fetch)
typescript
const response = await fetch("https://api.heygen.com/v2/avatars", {
headers: {
"X-Api-Key": process.env.HEYGEN_API_KEY!,
},
});
const { data } = await response.json();
### TypeScript/JavaScript (axios)
typescript
import axios from "axios";
const client = axios.create({
baseURL: "https://api.heygen.com",
headers: {
"X-Api-Key": process.env.HEYGEN_API_KEY,
},
});
const { data } = await client.get("/v2/avatars");
### Python (requests)
python
import os
import requests
response = requests.get(
"https://api.heygen.com/v2/avatars",
headers={"X-Api-Key": os.environ["HEYGEN_API_KEY"]}
)
data = response.json()
### Python (httpx)
python
import os
import httpx
async with httpx.AsyncClient() as client:
response = await client.get(
"https://api.heygen.com/v2/avatars",
headers={"X-Api-Key": os.environ["HEYGEN_API_KEY"]}
)
data = response.json()
## 创建可复用的 API 客户端
### TypeScript
typescript
class HeyGenClient {
private baseUrl = "https://api.heygen.com";
private apiKey: string;
constructor(apiKey: string) {
this.apiKey = apiKey;
}
async request(endpoint: string, options: RequestInit = {}): Promise {
const response = await fetch(`${this.baseUrl}${endpoint}`, {
...options,
headers: {
"X-Api-Key": this.apiKey,
"Content-Type": "application/json",
...options.headers,
},
});
if (!response.ok) {
const error = await response.json();
throw new Error(error.message || `HTTP ${response.status}`);
}
return response.json();
}
get(endpoint: string): Promise {
return this.request(endpoint);
}
post(endpoint: string, body: unknown): Promise {
return this.request(endpoint, {
method: "POST",
body: JSON.string