Audit logging
Record who created, revoked, rotated, enabled, or disabled each key, and query the history.
With audit logging on, keypal writes a log entry each time a key is created, revoked, rotated, enabled, or disabled. Use it to answer “who revoked this key, and why?” Verifications are not logged; lastUsedAt on the key covers usage.
Turn it on
import { createKeys } from 'keypal'
const keys = createKeys({
auditLogs: true,
auditContext: { userId: 'system', metadata: { service: 'api' } },
})
auditContext is optional and is added to every entry. Logs are stored by your storage adapter:
| Storage | Logs |
|---|---|
| Memory, Redis | Built in. |
| Drizzle, Kysely | Pass auditLogTable to the store. |
| Prisma | Pass auditLogModel to the store. |
| Custom | Implement saveLog, findLogs, countLogs, deleteLogs, and getLogStats. |
If the storage can’t save logs, actions still succeed and nothing is logged. A log write that fails is reported with console.error and doesn’t fail the action.
Say who did it
Every action method takes an optional context as its last argument:
await keys.revoke(record.id, {
userId: 'admin_456',
ip: request.headers.get('x-forwarded-for') ?? undefined,
userAgent: request.headers.get('user-agent') ?? undefined,
metadata: { reason: 'Leaked in a public repository' },
})
create(input, context), rotate(id, input, context), revoke, revokeAll, enable, and disable all accept it. Its fields override auditContext, and the two metadata objects are merged.
What an entry holds
{
id: 'V1StGXR8_Z5jdHi6B-myT',
action: 'revoked', // 'created' | 'revoked' | 'rotated' | 'enabled' | 'disabled'
keyId: 'hT3x9VqL2mPn8RkW4yJcZ',
ownerId: 'user_123',
timestamp: '2026-03-01T12:00:00.000Z',
data: {
userId: 'admin_456',
ip: '203.0.113.7',
userAgent: 'curl/8.7.1',
metadata: { service: 'api', reason: 'Leaked in a public repository' },
},
}
data is your merged context. keypal adds to metadata in two cases: created entries include the key’s name and scopes, and rotated entries include rotatedTo, the new key’s id. A rotation writes two entries: created for the new key and rotated for the old one.
Query logs
const history = await keys.getLogs({ keyId: record.id })
const recentRevocations = await keys.getLogs({
ownerId: 'user_123',
action: 'revoked',
startDate: '2026-01-01T00:00:00.000Z',
limit: 20,
offset: 0,
})
const created = await keys.countLogs({ action: 'created' })
const stats = await keys.getLogStats('user_123')
// { total, byAction: { created: 4, revoked: 1 }, lastActivity: '2026-...' }
Every filter is optional. startDate and endDate are ISO timestamps and include both ends. Results come newest first, 100 at a time unless you set limit.
Delete logs
await keys.deleteLogs({ endDate: '2025-01-01T00:00:00.000Z' }) // up to the start of 2025
await keys.clearLogs(record.id) // one key's history
Both return how many entries were deleted. deleteLogs needs at least one filter; an empty query deletes nothing and returns 0.
Errors
getLogs, countLogs, getLogStats, deleteLogs, and clearLogs throw AUDIT_LOGGING_DISABLED when auditLogs is off, and STORAGE_NOT_SUPPORTED when the storage has no log support.