Overview
This guide covers everything you need to protect your Express.js API with Unkey, including basic setup, reusable middleware, permission checks, and production-ready patterns. What you’ll learn:- Quick setup with the Unkey SDK
- Reusable middleware patterns
- Permission-based access control
- Rate limiting integration
- Error handling best practices
Prerequisites
- Unkey account (free)
- API created in your Unkey dashboard
- Node.js 18+
Quick Start
Add your root key
Get a root key from Settings → Root Keys and create a
.env file:.env
Test it
Reusable Middleware
For cleaner code, extract verification into middleware:middleware/auth.js
TypeScript Version
For TypeScript projects:middleware/auth.ts
Rate Limit Headers
Include rate limit info in response headers:middleware/auth.ts
Permission-Based Access
Create middleware that requires specific permissions:middleware/permissions.ts
Protect Entire Router
Apply middleware to an entire router:app.ts
Verification Response Data
After successful verification,data contains:
| Field | Type | Description |
|---|---|---|
valid | boolean | Whether the key passed all checks |
code | string | Status code (VALID, NOT_FOUND, RATE_LIMITED, etc.) |
keyId | string | The key’s unique identifier |
name | string? | Human-readable name of the key |
meta | object? | Custom metadata associated with the key |
expires | number? | Unix timestamp (in milliseconds) when the key will expire |
credits | number? | Remaining uses (if usage limits set) |
enabled | boolean | Whether the key is enabled |
roles | string[]? | Permissions attached to the key |
permissions | string[]? | Permissions attached to the key |
identity | object? | Identity info if externalId was set |
ratelimits | object[]? | Rate limit states (if rate limiting configured) |
Next Steps
Add rate limiting
Limit requests per key
Set usage limits
Cap total requests per key
Add permissions
Fine-grained access control
SDK Reference
Full TypeScript SDK docs
Troubleshooting
Getting 401 even with a valid key?
Getting 401 even with a valid key?
- Ensure the key hasn’t expired or been revoked
- Verify the
Authorizationheader format:Bearer YOUR_KEY(note the space) - Check that your root key has the
verify_keypermission
Getting 500 errors?
Getting 500 errors?
- Check that
UNKEY_ROOT_KEYis 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