keypal

keypal

Secure API key management for TypeScript

A TypeScript library for secure API key management with cryptographic hashing, expiration, scopes, and pluggable storage.

Features

  • Secure by Default: SHA-256/SHA-512 hashing with optional salt and timing-safe comparison
  • Smart Key Detection: Automatically extracts keys from Authorization, x-api-key, or custom headers
  • Built-in Caching: Optional cache from key hash to key id; with Redis storage, the in-memory cache saves a round trip per verify
  • Flexible Storage: Memory, Redis, Drizzle ORM, Prisma, and Kysely adapters included
  • Scope-based Permissions: Fine-grained access control with resource-specific scopes
  • Tags: Organize and find keys by tags
  • Key Management: Enable/disable, rotate, and soft-revoke keys with audit trails
  • Audit Logging: Track who did what, when, and why (opt-in)
  • TypeScript: Full type safety
  • Zero Config: Works out of the box with sensible defaults

Quick Start

import { createKeys } from 'keypal'

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

// Create a key
const { key, record } = await keys.create({
  ownerId: 'user_123',
  scopes: ['read', 'write'],
})

// Verify from headers
const result = await keys.verify(request.headers)
if (result.valid) {
  console.log('Authenticated:', result.record.ownerId)
}

Get Started

Documentation