Skip to main content

Express

Add API key authentication to your Express API using Unkey. Protect routes by verifying keys on every incoming request with minimal setup.
2 min read

What you'll build#

An Express server with a protected /secret route that requires a valid API key. Requests without a valid key get rejected with a 401.

Time to complete: ~5 minutes

Prerequisites#

Want to skip ahead?

Clone the complete example and run it locally.

Create your Express app#

Add your root key#

Get a root key from Settings → Root Keys and create a .env file:

.env
Warning
Never commit your .env file. Add it to .gitignore.

Create your server#

Create index.js with a protected route:

index.js

Start your server#

Test it#

First, create a test key in your Unkey dashboard, then:

Test with valid key

You should see:

Now try without a key:

Test without key

You'll get:

What's in data?#

After successful verification, data contains:

FieldTypeDescription
validbooleanWhether the key passed all checks
codestringStatus code (VALID, NOT_FOUND, RATE_LIMITED, etc.)
keyIdstringThe key's unique identifier
namestring?Human-readable name of the key
metaobject?Custom metadata associated with the key
expiresnumber?Unix timestamp (in milliseconds) when the key will expire. (if set)
creditsnumber?Remaining uses (if usage limits set)
enabledbooleanWhether the key is enabled
rolesstring[]?Permissions attached to the key
permissionsstring[]?Permissions attached to the key
identityobject?Identity info if externalId was set when creating the key
ratelimitsobject[]?Rate limit states (if rate limiting configured)

Using as middleware#

For cleaner code, extract verification into middleware:

middleware/auth.js

Then use it on any route:

Next steps#

Troubleshooting#

Getting 401 even with a valid key?
  • Ensure the key hasn't expired or been revoked - Verify the Authorization header format: Bearer YOUR_KEY (note the space) - Check that your root key has the verify_key permission
Getting 500 errors?
  • Check that UNKEY_ROOT_KEY is set correctly in your .env - Make sure you're calling require("dotenv").config() before using env vars - Check the Unkey dashboard for any service issues
TypeScript version?

The code above uses CommonJS. For TypeScript, install types and use imports: