Back to Blog

API key design that does not embarrass you

Most API key implementations have at least one of these problems. Here is how to avoid them.

API keys are the simplest form of authentication, which is exactly why they are so easy to get wrong. A bad key design creates support burden, security exposure, and developer frustration. Here is what good looks like.

Use a prefix

A key that starts with ak_live_ or sk_test_ is immediately identifiable. Developers know at a glance which environment it belongs to. Secret scanners GitHub's, GitGuardian's, your own can detect and alert on leaked keys automatically.

A random 32-character string with no prefix is opaque. You cannot tell if it is a live key or a test key, yours or someone else's. Prefixes cost nothing and pay off immediately.

Never store the full key

You should not be able to retrieve a key after creation. Store a hash. Show the full key once, at creation time, and never again.

If a user loses their key, they generate a new one. This is the right behaviour. It means a leaked key that gets rotated is actually gone not just "changed in your dashboard but still sitting in your database."

Scope your keys

A key that can do everything is a liability. If it leaks, everything is exposed. If you need to give a third party read access, you should not have to give them write access too.

Scoped keys let you issue the minimum privilege required for a given integration. They also make audit logs meaningful you can see which key performed which action, and revoke only what needs revoking.

Make revocation instant

Key revocation should take effect immediately, not after a cache TTL. If a developer suspects a key is compromised, they need to be able to kill it now.

This means your verification path cannot rely on a cache with a long TTL. Check revocation status on every request, or use a short enough TTL that the window of exposure is acceptable.

Show enough to identify, not enough to use

After creation, show the last four characters of the key. ak_live_...f3a2. Enough for a developer to identify which key is which in a list. Not enough to reconstruct the full key.

This is the pattern Access uses. Keys are prefixed, hashed at rest, scoped, and immediately revocable. The full key is shown once.