[ PROMPT_NODE_24288 ]
Secrets Store API 参考
[ SKILL_DOCUMENTATION ]
# API 参考
## 绑定 API
### 基本访问
**关键**: 必须使用异步 `.get()` - 密钥无法直接获取。
**`.get()` 在出错时会抛出异常** - 不会返回 null。请务必使用 try/catch。
typescript
interface Env {
API_KEY: { get(): Promise };
}
export default {
async fetch(request: Request, env: Env): Promise {
const apiKey = await env.API_KEY.get();
return fetch("https://api.example.com", {
headers: { "Authorization": `Bearer ${apiKey}` }
});
}
}
### 错误处理
typescript
export default {
async fetch(request: Request, env: Env): Promise {
try {
const apiKey = await env.API_KEY.get();
return fetch("https://api.example.com", {
headers: { "Authorization": `Bearer ${apiKey}` }
});
} catch (error) {
console.error("密钥访问失败:", error);
return new Response("配置错误", { status: 500 });
}
}
}
### 多密钥与模式
typescript
// 并行获取
const [stripeKey, sendgridKey] = await Promise.all([
env.STRIPE_KEY.get(),
env.SENDGRID_KEY.get()
]);
// ❌ 缺少 .get()
const key = env.API_KEY;
// ❌ 模块级缓存
const CACHED_KEY = await env.API_KEY.get(); // 失败
// ✅ 请求级缓存
const key = await env.API_KEY.get(); // 正确 - 在请求内复用
## REST API
基准地址: `https://api.cloudflare.com/client/v4`
### 认证
bash
curl -H "Authorization: Bearer $CF_TOKEN"
https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/secrets_store/stores
### 存储操作
bash
# 列出
GET /accounts/{account_id}/secrets_store/stores
# 创建
POST /accounts/{account_id}/secrets_store/stores
{"name": "my-store"}
# 删除
DELETE /accounts/{account_id}/secrets_store/stores/{store_id}
### 密钥操作
bash
# 列出
GET /accounts/{account_id}/secrets_store/stores/{store_id}/secrets
# 创建 (单个)
POST /accounts/{account_id}/secrets_store/stores/{store_id}/secrets
{
"name": "my_secret",
"value": "secret_value",
"scopes": ["workers"],
"comment": "可选"
}
# 创建 (批量)
POST /accounts/{account_id}/secrets_store/stores/{store_id}/secrets
[
{"name": "secret_one", "value": "val1", "scopes": ["workers"]},
{"name": "secret_two", "value": "val2", "scopes": ["workers", "ai-gateway"]}
]
# 获取元数据
GET /accounts/{account_id}/secrets_store/stores/{store_id}/secrets/{secret_id}
# 更新
PATCH /accounts/{account_id}/secrets_store/stores/{store_id}/sec