Programmatic Access to Your Application
API keys provide a secure way to authenticate programmatic access to your application’s resources. Thezopio
framework includes a comprehensive API key management system integrated with Clerk.
Overview
The API key system inzopio
is built on top of Clerk’s API key functionality and consists of:
- A core package (
@repo/api-key
) that handles API key creation and validation - API endpoints for managing API keys
- Authentication middleware for protecting routes with API key authentication
Core Package
The@repo/api-key
package provides the fundamental API key functionality:
API Endpoints
The framework provides RESTful endpoints for API key management in theapi
app:
Endpoint | Method | Description |
---|---|---|
/api-keys | GET | List all API keys for the authenticated user |
/api-keys | POST | Create a new API key |
/api-keys | DELETE | Delete an API key by ID |
Authentication Middleware
TheapiKeyAuthMiddleware
function provides a simple way to protect routes with API key authentication:
Creating API Keys
To create a new API key, make a POST request to the/api-keys
endpoint with the following payload:
Using API Keys
To use an API key in API requests, include it in the Authorization header:Security Considerations
- API keys should be treated as sensitive credentials and never exposed in client-side code
- Use HTTPS for all API requests to prevent key interception
- Assign the minimum necessary permissions to each API key
- Implement key rotation policies for long-lived API keys
- Monitor API key usage for suspicious activity
Implementation Details
The API key system is implemented with a clean separation of concerns:- Core Package: Handles the fundamental operations of creating and validating API keys
- API Layer: Exposes endpoints for managing API keys
- Authentication Middleware: Protects routes with API key authentication