How long should an API key be? 32 random bytes, and store the hash
128 random bits are already out of reach of guessing, so 32 bytes is plenty. What matters more is how you store the key: in our test, bcrypt took 76 ms per request where SHA-256 took 1.3 µs.
Make an API key from 32 random bytes (256 bits) out of a cryptographic random generator, and store only its SHA-256 hash. 16 bytes would already be safe from guessing, so the extra length is a cheap margin, not a requirement. The bigger mistake we see is storing keys with bcrypt, which is the right tool for passwords and the wrong one for keys: in our test it made every request about 58,000 times slower to check.
Everything below comes from examples/api-key-length. run.sh runs both scripts, and results.txt is the raw output.
What the usual one-liners give you
We ran each command on Ubuntu 24.04 (Node 22.23.5, Python 3.13.18, OpenSSL 3.0.13) and counted the characters:
| Command | Characters | Random bits |
|---|---|---|
openssl rand -hex 16 | 32 | 128 |
openssl rand -hex 32 | 64 | 256 |
openssl rand -base64 32 | 44 | 256 |
Python secrets.token_hex() | 64 | 256 |
Python secrets.token_urlsafe() | 43 | 256 |
Python secrets.token_urlsafe(16) | 22 | 128 |
Node crypto.randomBytes(32).toString('base64url') | 43 | 256 |
Node crypto.randomBytes(32).toString('hex') | 64 | 256 |
Node crypto.randomUUID() | 36 | 122 |
Three things stand out:
- The number you pass is bytes, not characters.
openssl rand -hex 32prints 64 characters, because hex spends two characters on each byte. Base64 spends about 1.33. - Python's defaults are already 32 bytes.
secrets.token_urlsafe()with no argument gave 43 characters, which decode back to 32 bytes. - A UUID v4 has 122 random bits, not 128. Six bits are fixed to say "version 4". That is still a lot, but a UUID is made to be unique, not secret, so use
randomBytesfor keys.
base64url (or token_urlsafe) is the most convenient encoding: it is the shortest of these and has no +, / or = to escape in URLs, headers and .env files.
Why 128 bits is enough and 256 is a cheap margin
A key made from 16 random bytes has 2128 possible values. If an attacker could try a trillion keys a second against your API, which no API allows, finding one would take about 1019 years on average. That is arithmetic, not a measurement: 2128 ÷ 1012 per second ÷ 3.16 × 107 seconds per year ≈ 1.1 × 1019.
So length is not where keys fail. They fail because they leak: committed to a repository, pasted in a support ticket, logged in a URL. We still use 32 bytes because it costs 21 extra characters and leaves room if part of the key is ever reused for something else, such as a prefix you show in a dashboard.
How to store it: the measurement
When a request arrives with a key, the server has to find out whose key it is. We timed four ways of doing that in Node 22.23.5 on a 4 vCPU Intel Xeon (2.10 GHz) Linux container with 16 GB of RAM. The database index is stood in for by a Map of 100,000 keys, so these numbers are the hashing cost alone, without a database round trip. Mean of three runs:
| Method | Time to check one key |
|---|---|
| SHA-256 of the key, then look up the hash | 1.3 µs |
| HMAC-SHA-256 with a server-side secret, then look up | 1.8 µs |
timingSafeEqual against one stored secret | 0.14 µs |
| bcrypt cost 10 (bcryptjs 3.0.3) | 76.9 ms |
| bcrypt cost 12 (bcryptjs 3.0.3) | 309.8 ms |
bcrypt at cost 10 took about 58,000 times as long as SHA-256. At cost 12, one core can check about three keys a second. bcryptjs is a pure JavaScript library, and a native bcrypt binding is faster, but it is still slow on purpose: that is its whole job.
bcrypt has a second problem for keys. Each hash has its own salt, so you can't look a key up by its hash. You need to give every key a public id, find the row by id, and then run one bcrypt compare. SHA-256 has no salt, so the hash itself is the lookup key in an ordinary unique index.
Why a fast hash is fine here
bcrypt is slow so that guessing passwords from a stolen database is expensive. Passwords are short and people choose them, so an attacker can try the likely ones first. A 32-byte random key has no likely values. Even with the SHA-256 hashes in hand, an attacker faces the same 2256 search as before, and slowing each guess down adds nothing useful.
If you want a little more, use HMAC-SHA-256 with a secret kept outside the database (an environment variable or a secrets manager). Then a stolen copy of the table alone isn't enough even to test a guess. It cost 0.5 µs more per check in our run.
What to do
- Generate 32 bytes from
crypto.randomBytes,secrets.token_urlsafe()oropenssl rand. NeverMath.random()or a UUID. - Add a prefix such as
sk_live_before the random part. It tells people which system a key belongs to, and secret scanners can spot your keys in a leaked repository. - Show the key once, when it's created, and store only
sha256(key)(or the HMAC) in a unique index. Keep the first few characters in plain text if you want to show "sk_live_Wr…" in a dashboard. - On each request, hash the key that came in and look it up. Don't use bcrypt, scrypt or Argon2 for random keys.
- Let people rotate: allow two active keys per account so they can swap one without downtime.
Need a key now? The API key generator makes prefixed keys in your browser, and the key and secret generators page has the rest: encryption keys, webhook secrets and salts. For passwords, which do need bcrypt, the bcrypt hash generator shows what the cost setting does.
Limits of this test
The timings are for hashing in one Node process on one machine, with no database or network. A real request adds a database lookup that will usually take longer than SHA-256 itself. The brute-force figure is arithmetic from an assumed guessing rate, not a measurement.
Part of the team at Horizech — we build Flutter apps, AI integrations, web platforms, and games. If you want this done for you, get in touch.