Overview
Where keypal keeps API keys, and how to choose an adapter
keypal keeps keys in a storage adapter. Without a storage option it uses memory, which loses every key when the process exits. For anything real, pick Redis or one of the SQL adapters.
The core package has no storage dependencies. Each adapter has its own import path, and its database client is an optional peer dependency that you install yourself.
| Adapter | Import | Install | Databases |
|---|---|---|---|
| Memory | keypal/memory (or the default) |
nothing | none, in-process |
| Redis | keypal/redis (or storage: "redis") |
ioredis 5 or 6 |
Redis, single node or Sentinel |
| Drizzle | keypal/drizzle |
drizzle-orm |
PostgreSQL, MySQL, MariaDB, SQLite |
| Kysely | keypal/kysely |
kysely |
PostgreSQL, MySQL, MariaDB, SQLite |
| Prisma | keypal/prisma |
@prisma/client |
PostgreSQL |
Choosing an adapter
- Memory for tests, scripts and local development.
- Redis when Redis is already your shared state. Revoked keys expire on their own after
revokedKeyTtl. - Drizzle, Kysely or Prisma when you want keys in the database you already run, next to your other tables. Use the one your app already uses.
- Your own table if you already have an API keys table: pass a lookup with
findByHashand keypal verifies against it. See Custom storage.
How the SQL adapters store keys
Drizzle, Kysely and Prisma share one storage layer, so they behave the same way.
By default a key is one row with three columns: id, keyHash, and metadata, a JSON column holding every other field (ownerId, name, scopes, tags, expiresAt, revokedAt, and the rest). Each adapter’s page shows the table to create.
keyHash should be unique and indexed: verify looks keys up by it on every call.
Updates
update(id, fields) writes only the fields it is given. A background lastUsedAt write therefore can’t undo a revoke that happened at the same moment. How each database does this:
- PostgreSQL merges the change into the JSON column in one statement.
- MySQL, MariaDB and SQLite use
json_setandjson_removein one statement. - Prisma compares the stored JSON before writing and retries if it changed.
- With
flattenMetadata, only the columns of the given fields are written.
The exceptions are noted on the Kysely and Prisma pages.
Options shared by every SQL adapter
| Option | What it does |
|---|---|
schema.apiKeyColumns |
Renames id, keyHash and metadata, and (with metadataColumns) the per-field columns |
schema.flattenMetadata |
Stores each field in its own column instead of the JSON column |
schema.auditLogColumns |
Renames the audit log columns |
debugLogs |
Logs every storage call and its result, with key hashes redacted |
The audit log table (auditLogTable on Drizzle and Kysely, auditLogModel on Prisma) is optional. Without it, auditLogs: true records nothing and the log queries throw STORAGE_NOT_SUPPORTED.
Owner and tag queries
On PostgreSQL, finding keys by owner or tag runs in SQL against the JSON column. On MySQL, MariaDB and SQLite, list(ownerId), findByTag and revokeAll read the whole table and filter in your process. For large tables on those databases, turn on flattenMetadata and index the owner column, so owner lookups run in SQL.
Caching
The key cache (cache: true) saves a round trip only with Redis storage. With a SQL adapter, finding a key by hash costs the same as finding it by id, so leave the cache off. See Caching.