---
title: Configuration
description: Every option createKeys takes, its default, and when to change it.
---

Every option is optional. `createKeys()` with no arguments gives you unprefixed 32-character keys, SHA-256 hashes, and in-memory storage.

```ts
import { createKeys } from 'keypal'
import Redis from 'ioredis'

const redis = new Redis()

const keys = createKeys({
  prefix: 'sk_live_',
  salt: process.env.API_KEY_SALT,
  storage: 'redis',
  redis,
  cache: true,
  impliedScopes: { admin: ['read', 'write'] },
  auditLogs: true,
})
```

## Key format and hashing

| Option | Default | What it does |
| --- | --- | --- |
| `prefix` | none | Added to the start of every generated key, such as `sk_live_`. `verify` rejects keys without it as `INVALID_FORMAT` before reading storage, and header extraction prefers a header holding a key with this prefix. |
| `length` | `32` | Random characters after the prefix. Must be a positive integer. |
| `alphabet` | `A–Z`, `a–z`, `0–9` | Characters the random part is drawn from. Needs 2 to 256 characters, at least 2 of them distinct. |
| `algorithm` | `'sha256'` | `'sha256'` or `'sha512'`. |
| `salt` | none | Secret mixed into every hash, so a leaked table of hashes can't be checked against guessed keys without it. Load it from an environment variable. |

`createKeys` throws a `RangeError` for a bad `length` or `alphabet`.

:::warning
Changing `salt` or `algorithm` changes every key's hash, and changing `prefix` makes existing keys fail the prefix check. Existing keys stop verifying. Set these once, before you issue real keys.
:::

## Storage

| Option | Default | What it does |
| --- | --- | --- |
| `storage` | `'memory'` | Where keys live. See below. |
| `redis` | none | An ioredis client. Required when `storage` or `cache` is `'redis'`. |
| `revokedKeyTtl` | `604800` (7 days) | Seconds a revoked key's record stays in Redis before Redis deletes it. `0` keeps it. Applies only with `storage: 'redis'`. |

`storage` takes one of four things:

- `'memory'`: keys live in the process and are lost when it exits. For tests and local development.
- `'redis'`: keypal creates a Redis store from the `redis` client.
- A storage adapter such as `new DrizzleStore(...)`, `new KyselyStore(...)`, `new PrismaStore(...)`, or your own object implementing `Storage`. See [Storage](/docs/storage/overview).
- A lookup, `{ findByHash, findById?, update? }`, for keys kept in your own table. `verify` runs its checks against your rows and returns them with your type; key management methods throw `STORAGE_NOT_SUPPORTED`. See [the quick start](/docs/getting-started/quick-start) for an example.

After `revokedKeyTtl` passes, Redis has deleted the record, so `verify` returns `INVALID_KEY` for that key instead of `REVOKED`.

## Caching

| Option | Default | What it does |
| --- | --- | --- |
| `cache` | `false` | `true` for an in-memory cache, `'redis'` for a Redis cache, or your own `Cache` object. |
| `cacheTtl` | `60` | Seconds a cache entry lives. |

The cache maps a key's hash to its id, and `verify` still reads the key by id on every call. It only saves work with Redis storage and `cache: true`. Leave it off otherwise. [Caching](/docs/concepts/caching) explains why.

## Reading keys from headers

| Option | Default | What it does |
| --- | --- | --- |
| `headerNames` | `['authorization', 'x-api-key']` | Headers checked, in order. Names are case-insensitive. |
| `extractBearer` | `true` | Strip `Bearer ` from header values. |

With a `prefix` set, the first header whose value starts with the prefix wins, so an unrelated `Authorization: Basic ...` header does not hide a key sent in `x-api-key`. Without a match, keypal uses the first non-empty header. Both options can be overridden per call in `verify` and `extractKey`.

## Usage tracking

| Option | Default | What it does |
| --- | --- | --- |
| `autoTrackUsage` | `true` | Write the key's `lastUsedAt` after each successful `verify`. |
| `usageTrackingInterval` | `60` | Minimum seconds between `lastUsedAt` writes for the same key. `0` writes on every verify. |

Writes happen in the background and never delay or fail a `verify`; a failed write is logged and retried on a later verify. Each manager instance tracks its own write times, so several servers can each write once per interval. One manager tracks up to 10,000 keys at a time; past that, a key not yet tracked is skipped until a slot frees up.

Tracking is off when the storage has no `update` method, as with a lookup that leaves it out. Pass `skipTracking: true` to `verify` to skip it for one call.

## Scopes

| Option | Default | What it does |
| --- | --- | --- |
| `impliedScopes` | none | Scopes that grant other scopes, such as `{ admin: ['read', 'write'] }`. Applied transitively by the manager's scope checks. |

See [Scopes and permissions](/docs/concepts/scopes-and-permissions).

## Audit logging

| Option | Default | What it does |
| --- | --- | --- |
| `auditLogs` | `false` | Record who created, revoked, rotated, enabled, or disabled each key. |
| `auditContext` | none | Context added to every log entry, such as `{ userId: 'system', metadata: { service: 'api' } }`. Per-call context overrides it. |

The storage must support logs. See [Audit logging](/docs/concepts/audit-logging).
