Skip to main content

Express Middleware

Build a reusable Express middleware for Unkey API key authentication. Verify keys automatically on every request with error handling.
1 min read

A production-ready middleware pattern for Express applications.

Install#

Basic Middleware#

middleware/auth.ts

Use the Middleware#

app.ts

With 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

Use it:


Configurable Middleware Factory#

Create a flexible middleware that can be configured per-route:

middleware/auth.ts

Error Handling#

Graceful degradation when Unkey is unavailable:

middleware/auth.ts

TypeScript Setup#

types/express.d.ts

Make sure to include this in your tsconfig.json: