Expiration
Make keys stop working at a set time, and check a key's status yourself.
A key with expiresAt stops verifying once that time passes. Keys without it never expire; there is no default lifetime.
Set an expiry
expiresAt takes a Date or an ISO 8601 string. To expire a key 90 days from now:
const DAY_MS = 24 * 60 * 60 * 1000
const { key, record } = await keys.create({
ownerId: 'user_123',
scopes: ['read'],
expiresAt: new Date(Date.now() + 90 * DAY_MS),
})
Leave expiresAt out, or pass null, for a key that never expires.
What happens at expiry
From the moment expiresAt is reached, verify returns { valid: false, errorCode: 'EXPIRED' }. The record stays in storage: keypal does not delete expired keys, so you can still list them and show users which keys lapsed.
A value that can’t be parsed as a date counts as expired, so a corrupt field never turns into a key that works forever. If a key is both revoked and expired, verify reports REVOKED.
Change an expiry
Rotate the key to issue a replacement with a new expiry. Fields you pass replace the old key’s, and null clears the expiry:
const { key: newKey } = await keys.rotate(record.id, {
expiresAt: new Date(Date.now() + 90 * DAY_MS),
})
This gives the client a new secret and revokes the old one at once. To extend a key without changing its secret, call update(id, { expiresAt }) on your storage adapter.
Check expiry yourself
import { getKeyStatus, isExpired } from 'keypal'
keys.isExpired(record) // true once expiresAt has passed
isExpired(record.expiresAt) // the same, on the raw value
getKeyStatus(record) // 'active' | 'revoked' | 'expired' | 'disabled'
getKeyStatus is the rule verify uses, so use it when you load keys some other way, such as listing an owner’s keys for a dashboard. It reads only revokedAt, expiresAt, and enabled (each a Date, string, or null), so it works on rows from your own table too.
Choosing lifetimes
Shorter lifetimes limit the damage from a leaked key but mean more rotations for your users. Common choices are 30 to 90 days for keys used by people and up to a year for keys in long-running services. To warn users before a key lapses, list their keys and compare expiresAt with the current date, then let them rotate ahead of time.