Skip to content
keypal
Esc
↑↓navigate↵open⌘Jpreview
On this page

keypal

API keys for TypeScript. Generate, hash, verify, scope, rotate, and revoke them, stored wherever you already keep data.

keypal issues API keys, stores only their hashes, and checks incoming requests against them. It reads the key from the request headers, rejects keys that are unknown, revoked, expired, or disabled, and gives you the stored record so you can check its scopes.

Keys can live in memory, Redis, or a SQL database through Drizzle, Kysely, or Prisma. If you already have an API keys table, keypal can verify against it without moving your data.

import { createKeys } from 'keypal'

const keys = createKeys({ prefix: 'sk_' })

// Issue a key. `key` is the secret: show it to the user once, then discard it.
const { key, record } = await keys.create({
  ownerId: 'user_123',
  scopes: ['read'],
})

// Check a request.
export async function handler(request: Request) {
  const result = await keys.verify(request.headers)
  if (!result.record) {
    return Response.json({ error: result.errorCode }, { status: 401 })
  }
  if (!keys.hasScope(result.record, 'read')) {
    return Response.json({ error: 'forbidden' }, { status: 403 })
  }
  return Response.json({ owner: result.record.ownerId })
}

Get started

Go deeper

Was this page helpful?