Add API key management and rate limiting to your API with Unkey — open-source API authentication infrastructure. Use when someone asks to "add API keys to my API", "rate limit my API", "API key management", "Unkey", "usage-based API billing", "issue API keys to customers", or "per-customer rate limits". Covers key creation, verification, rate limiting, usage tracking, and analytics.
You are an expert in Unkey, the open-source API key management platform. You help developers create, validate, and manage API keys with built-in rate limiting, usage tracking, temporary keys, key rotation, and per-key permissions — providing the complete API authentication layer for developer platforms, SaaS APIs, and internal services without building custom key infrastructure.
import { Unkey } from "@unkey/api";
const unkey = new Unkey({ rootKey: process.env.UNKEY_ROOT_KEY! });
// Create API key for a customer
const { result } = await unkey.keys.create({
apiId: process.env.UNKEY_API_ID!,
prefix: "sk_live", // Key prefix for identification
name: "Acme Corp Production Key",
ownerId: "customer-42", // Link to your user
meta: { // Custom metadata
plan: "pro",
team: "engineering",
},
roles: ["api.read", "api.write"], // RBAC permissions
ratelimit: {
type: "fast",
limit: 100, // 100 requests
duration: 60000, // Per minute
},
remaining: 10000, // Total usage limit (optional)
expires: Date.now() + 30 * 24 * 60 * 60 * 1000, // 30 days (optional)
});
console.log(result.key); // sk_live_abc123... (show once!)
console.log(result.keyId); // key_xxx (for management)
import { verifyKey } from "@unkey/api";
// Middleware — verify API key on every request
async function authMiddleware(req: Request) {
const key = req.headers.get("Authorization")?.replace("Bearer ", "");
if (!key) return new Response("Missing API key", { status: 401 });
const { result, error } = await verifyKey({
key,
apiId: process.env.UNKEY_API_ID!,
});
if (error || !result.valid) {
return new Response(JSON.stringify({
error: "Invalid API key",
code: result?.code, // "NOT_FOUND" | "RATE_LIMITED" | "USAGE_EXCEEDED" | "EXPIRED"
}), { status: result?.code === "RATE_LIMITED" ? 429 : 403 });
}
// Key is valid — access metadata
const { ownerId, meta, roles, remaining, ratelimit } = result;
console.log(`Customer: ${ownerId}, Plan: ${meta.plan}, Remaining: ${remaining}`);
// Check permissions
if (!roles.includes("api.write") && req.method !== "GET") {
return new Response("Insufficient permissions", { status: 403 });
}
// Rate limit info in response headers
return {
ownerId,
meta,
headers: {
"X-RateLimit-Limit": String(ratelimit?.limit),
"X-RateLimit-Remaining": String(ratelimit?.remaining),
"X-RateLimit-Reset": String(ratelimit?.reset),
},
};
}
// Update key (change limits, roles)
await unkey.keys.update({
keyId: "key_xxx",
ratelimit: { type: "fast", limit: 500, duration: 60000 }, // Upgrade limit
roles: ["api.read", "api.write", "api.admin"],
meta: { plan: "enterprise" },
});
// Revoke key
await unkey.keys.delete({ keyId: "key_xxx" });
// List keys for a customer
const { result: keys } = await unkey.apis.listKeys({
apiId: process.env.UNKEY_API_ID!,
ownerId: "customer-42",
});
// Usage analytics
const { result: verifications } = await unkey.keys.getVerifications({
keyId: "key_xxx",
start: Date.now() - 7 * 24 * 60 * 60 * 1000, // Last 7 days
end: Date.now(),
granularity: "day",
});
npm install @unkey/api
sk_live_, sk_test_ prefixes; identifies key type at a glanceremaining for prepaid/quota models; key auto-invalidates when exhaustedmeta; use for analytics and feature flagsexpires for trial keys, temporary access; auto-expire without cronnpx skills add TerminalSkills/unkey下载完整 Skill 目录,包含 SKILL.md 及所有相关文件
Search for places (restaurants, cafes, etc.) via Google Places API proxy on localhost.
Interact with GitHub using the `gh` CLI. Use `gh issue`, `gh pr`, `gh run`, and `gh api` for issues, PRs, CI runs, and advanced queries.
Create or update AgentSkills. Use when designing, structuring, or packaging skills with scripts, references, and assets.
Start voice calls via the OpenClaw voice-call plugin.
Notion API for creating and managing pages, databases, and blocks.
Gemini CLI for one-shot Q&A, summaries, and generation.
Category:developer