Generative AI REST API built with Rust and Rocket web framework with call quotas
Integrate Unkey to your API for managing call quotas. With usage-limited keys, this API enables you to control access while implementing automatic refill strategies—daily or monthly—ensuring seamless user experiences without exceeding limits.
Quota limiting for APIs is essential for preventing abuse, managing costs, ensuring fair access, optimizing performance, and upholding security and compliance.
This template showcases a generative AI REST API built with Rust and Rocket web framework. It demonstrates a secure method for storing created API keys using HTTP-only cookies. The /generate-image
endpoint accepts a prompt message as payload, verifies the API key (decrementing remaining credits), and requests images from OpenAI. If the key becomes invalid, the user receives an error message indicating that the quota has been exceeded.
create_key
, read_key
, encrypt_key
and decrypt_key
.1git clone git@github.com:unrenamed/unkey-rust-rocket2cd unkey-rust-rocket
.env
file in the root directory and populate it with the following environment variables:1OPENAI_API_KEY=your-openai-api-key2UNKEY_ROOT_KEY=your-unkey-root-key3UNKEY_API_ID=your-unkey-api-id
Ensure you replace your-*
with your actual Unkey credentials.
1cargo run
The server will start and listen on the port specified in the .env
file (default is 8000
).
/authorize
route to generate a new API key which will be saved to cookies:1 curl -X POST http://localhost:8000/authorize
/me
route to ensure you've successfully authorized:1 curl http://localhost:8000/me
/generate-image
route to receive a URL to the generated image:1 curl -X POST http://localhost:8000/generate_image \2 -H "Content-Type: application/json" \3 -d '{"prompt": "A sunset over a mountain range"}'
2500 verifications and 100K successful rate‑limited requests per month. No CC required.