Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Comprehensive Cloudflare platform skill covering Workers, D1, R2, KV, AI, Durable Objects, and security.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/secrets-store/api.md
1# API Reference23## Binding API45### Basic Access67**CRITICAL**: Async `.get()` required - secrets NOT directly available.89**`.get()` throws on error** - does NOT return null. Always use try/catch.1011```typescript12interface Env {13API_KEY: { get(): Promise<string> };14}1516export default {17async fetch(request: Request, env: Env): Promise<Response> {18const apiKey = await env.API_KEY.get();19return fetch("https://api.example.com", {20headers: { "Authorization": `Bearer ${apiKey}` }21});22}23}24```2526### Error Handling2728```typescript29export default {30async fetch(request: Request, env: Env): Promise<Response> {31try {32const apiKey = await env.API_KEY.get();33return fetch("https://api.example.com", {34headers: { "Authorization": `Bearer ${apiKey}` }35});36} catch (error) {37console.error("Secret access failed:", error);38return new Response("Configuration error", { status: 500 });39}40}41}42```4344### Multiple Secrets & Patterns4546```typescript47// Parallel fetch48const [stripeKey, sendgridKey] = await Promise.all([49env.STRIPE_KEY.get(),50env.SENDGRID_KEY.get()51]);5253// ❌ Missing .get()54const key = env.API_KEY;5556// ❌ Module-level cache57const CACHED_KEY = await env.API_KEY.get(); // Fails5859// ✅ Request-scope cache60const key = await env.API_KEY.get(); // OK - reuse within request61```6263## REST API6465Base: `https://api.cloudflare.com/client/v4`6667### Auth6869```bash70curl -H "Authorization: Bearer $CF_TOKEN" \71https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/secrets_store/stores72```7374### Store Operations7576```bash77# List78GET /accounts/{account_id}/secrets_store/stores7980# Create81POST /accounts/{account_id}/secrets_store/stores82{"name": "my-store"}8384# Delete85DELETE /accounts/{account_id}/secrets_store/stores/{store_id}86```8788### Secret Operations8990```bash91# List92GET /accounts/{account_id}/secrets_store/stores/{store_id}/secrets9394# Create (single)95POST /accounts/{account_id}/secrets_store/stores/{store_id}/secrets96{97"name": "my_secret",98"value": "secret_value",99"scopes": ["workers"],100"comment": "Optional"101}102103# Create (batch)104POST /accounts/{account_id}/secrets_store/stores/{store_id}/secrets105[106{"name": "secret_one", "value": "val1", "scopes": ["workers"]},107{"name": "secret_two", "value": "val2", "scopes": ["workers", "ai-gateway"]}108]109110# Get metadata111GET /accounts/{account_id}/secrets_store/stores/{store_id}/secrets/{secret_id}112113# Update114PATCH /accounts/{account_id}/secrets_store/stores/{store_id}/secrets/{secret_id}115{"value": "new_value", "comment": "Updated"}116117# Delete (single)118DELETE /accounts/{account_id}/secrets_store/stores/{store_id}/secrets/{secret_id}119120# Delete (batch)121DELETE /accounts/{account_id}/secrets_store/stores/{store_id}/secrets122{"secret_ids": ["id-1", "id-2"]}123124# Duplicate125POST /accounts/{account_id}/secrets_store/stores/{store_id}/secrets/{secret_id}/duplicate126{"name": "new_name"}127128# Quota129GET /accounts/{account_id}/secrets_store/quota130```131132### Responses133134Success:135```json136{137"success": true,138"result": {139"id": "secret-id-123",140"name": "my_secret",141"created": "2025-01-11T12:00:00Z",142"scopes": ["workers"]143}144}145```146147Error:148```json149{150"success": false,151"errors": [{"code": 10000, "message": "Name exists"}]152}153```154155## TypeScript Helpers156157Official types available via `@cloudflare/workers-types`:158159```typescript160import type { SecretsStoreSecret } from "@cloudflare/workers-types";161162interface Env {163STRIPE_API_KEY: SecretsStoreSecret;164DATABASE_URL: SecretsStoreSecret;165WORKER_SECRET: string; // Regular Worker secret (direct access)166}167```168169Custom helper type:170171```typescript172interface SecretsStoreBinding {173get(): Promise<string>;174}175176// Fallback helper177async function getSecretWithFallback(178primary: SecretsStoreBinding,179fallback?: SecretsStoreBinding180): Promise<string> {181try {182return await primary.get();183} catch (error) {184if (fallback) return await fallback.get();185throw error;186}187}188189// Batch helper190async function getAllSecrets(191secrets: Record<string, SecretsStoreBinding>192): Promise<Record<string, string>> {193const entries = await Promise.all(194Object.entries(secrets).map(async ([k, v]) => [k, await v.get()])195);196return Object.fromEntries(entries);197}198```199200See: [configuration.md](./configuration.md), [patterns.md](./patterns.md), [gotchas.md](./gotchas.md)201