Hashed at rest.
SHA-256 or SHA-512, an optional salt, and timing-safe comparison.
API keys that live inside your app. Issue them, check them on every request, scope them, and revoke them everywhere at once. One dependency, typed end to end, with only key hashes stored in the database you already run.
npm install keypalpnpm add keypalbun add keypalAdd API key auth to this app with keypalhttps://keypal.dev/llms-full.txtSHA-256 or SHA-512, an optional salt, and timing-safe comparison.
Reads Bearer or x-api-key and tells you exactly why a key failed.
Global scopes, per-resource scopes, and broad scopes that imply narrow ones.
verify reads the stored key every time. Revoked on one server, rejected on all.
A new key with the same settings. The old one stops working at once.
Keys stop working at the date you set. An unreadable date counts as expired.
lastUsedAt, written at most once a minute so busy keys stay cheap.
Who created, rotated, disabled or revoked each key, queryable later.
Already store keys? Pass findByHash and verify returns your rows, typed.
Everything below is the real API. Nothing to configure in a dashboard.
import { createKeys } from "keypal";
import { RedisStore } from "keypal/redis";
export const keys = createKeys({
prefix: "sk_live_",
storage: new RedisStore({ client: redis }),
});
const { key, record } = await keys.create({
ownerId: "org_42",
name: "Production server",
scopes: ["read", "write"],
expiresAt: "2027-01-01T00:00:00Z",
});export async function requireKey(req: Request) {
const { record, errorCode } = await keys.verify(req.headers);
if (!record) {
// MISSING_KEY, INVALID_KEY, REVOKED, EXPIRED, DISABLED
return Response.json({ errorCode }, { status: 401 });
}
if (!keys.hasScope(record, "write")) {
return Response.json({ error: "forbidden" }, { status: 403 });
}
return record;
}export const keys = createKeys({
prefix: "sk_live_",
storage: {
findByHash: async (keyHash) =>
(await db.query.apiKeys.findFirst({
where: eq(apiKeys.keyHash, keyHash),
})) ?? null,
update: async (id, { lastUsedAt }) => {
await db.update(apiKeys)
.set({ lastUsedAt: new Date(lastUsedAt) })
.where(eq(apiKeys.id, id));
},
},
});// Stop a leaked key everywhere, now.
await keys.revoke(keyId, { userId: "ana@acme.dev" });
// Replace a key, keeping its scopes and expiry.
const { key, record } = await keys.rotate(keyId);
// Pause a key without deleting it.
await keys.disable(keyId);
await keys.enable(keyId);The secret comes back once. keypal saves only its hash, with the owner, scopes and expiry you set.
The core has no database dependencies. Each adapter has its own import path, and its client is an optional peer you already have installed.
keypal is MIT licensed and built in the open. Read the code, file an issue, or send a pull request; the contributing guide covers running every database test locally.
Ship API keys you'd trust in production, this afternoon.