Rate Limiting and Access Control Middleware

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.
x-forwarded-for header for accurate user identification./api/v2 access to users with valid Unkey API keys./api/v1 and root / endpoints.| 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 |
create_keyread_keyencrypt_keydecrypt_keydart.rest.apisuperuser30<token> in /api/v2 routes.Clone the repository to your local machine:
1git clone git@github.com:unrenamed/unkey-dart2cd unkey-dart
Create a .env file in the root directory and populate it with the following environment variables:
1UNKEY_ROOT_KEY=your-unkey-root-key2UNKEY_API_ID=your-unkey-api-id3UNKEY_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:
1$ dart run bin/server.dart2Server listening on port 8080
If you have Docker Desktop installed, you can build and run with the docker command:
1$ docker build . -t myserver2$ docker run -it -p 8080:8080 myserver3Server listening on port 8080
150,000 requests per month. No CC required.