This Dart application implements middleware for the Shelf web server that enables rate limiting for all /api/ endpoints and restricts access to specific /api/v2 endpoints based on authorization tokens validated through the Unkey API. The solution is designed to enhance API security and manage user access while providing seamless integration for developers.
- Rate Limiting
Limits requests to /api/ endpoints to prevent abuse. - IP-Based Rate Limiting
Uses thex-forwarded-forheader for accurate user identification. - Authorization for Premium Features
Restricts/api/v2access to users with valid Unkey API keys. - Public API Access
Allows unrestricted use of/api/v1and root/endpoints.
- API Management
Controls access and prevents abuse of API endpoints. - Feature Flagging
Enables features for testers without public exposure. - Dynamic User Access Control
Simplifies permission management across API versions.
| Method | Endpoint | Required Headers | Description |
|---|---|---|---|
| GET | / |
None | Returns the welcome message |
| GET | /api/v1/echo/<message> |
x-forwarded-for: <ip> |
Returns the echoed message |
| GET | /api/v1/users |
x-forwarded-for: <ip> |
Retrieves a list of users |
| GET | /api/v1/users/<id> |
x-forwarded-for: <ip> |
Retrieves a user by ID |
| POST | /api/v2/users |
x-forwarded-for: <ip>, Authorization: Bearer <token> |
Adds a new user |
| PUT | /api/v2/users/<id> |
x-forwarded-for: <ip>, Authorization: Bearer <token> |
Updates an existing user |
| DELETE | /api/v2/users/<id> |
x-forwarded-for: <ip>, Authorization: Bearer <token> |
Deletes a user by ID |
- Navigate to Unkey Root Keys and click "Create New Root Key".
- Name your root key.
- Select the following workspace permissions:
create_keyread_keyencrypt_keydecrypt_key
- Click "Create" and save your root key securely.
- Go to Unkey APIs and click "Create New API".
- Enter a name for the API.
- Click "Create".
- Go to Unkey Ratelimits and click "Create New Namespace".
- Enter a name for the namespace.
- Click "Create".
- From the Unkey APIs page, select your newly created API.
- Click "Create Key" in the top right corner.
- Fill out the form with the following suggested values:
- Prefix:
dart.rest.api - Owner:
superuser - Bytes:
30
- Prefix:
- Click "Create" and copy the generated key. You'll use it instead of
<token>in/api/v2routes.
-
Clone the repository to your local machine:
git clone [email protected]:unrenamed/unkey-dart cd unkey-dart
-
Create a
.envfile in the root directory and populate it with the following environment variables:UNKEY_ROOT_KEY=your-unkey-root-key UNKEY_API_ID=your-unkey-api-id UNKEY_NAMESPACE=your-unkey-namespace
Ensure you replace
your-unkey-*with your actual Unkey credentials. -
Start the server:
You can run the example with the Dart SDK like this:
$ dart run bin/server.dart Server listening on port 8080If you have Docker Desktop installed, you can build and run with the
dockercommand:$ docker build . -t myserver $ docker run -it -p 8080:8080 myserver Server listening on port 8080