Learn how to setup Fast API with Unkey and protect your routes
This project is a Unkey FastAPI Boilerplate to protect API routes. We have three routes:
api/hello
(GET): This is an unprotected route that will return just hello worldapi/create
(GET): This will create a key using unkey and return key as well as keyidapi/protected
(POST): This route is protected and requires a key that you just created using create a route in the header.create_key
and read_key
cp .env.example .env
and then set the rootkey and the API ID we just created.1UNKEY_ROOT_KEY=""2UNKEY_API_ID=""
Clone the repo
1git clone https://github.com/harshsbhat/unkey-fastapi-boilerplate.git2cd unkey-fastapi-boilerplate
Install the dependencies
1pip install -r requirements.txt
Run the project
1uvicorn main:app --reload
This should start the project on port http://127.0.0.1:8000
Unprotected Route
1curl http://127.0.0.1:8000/api/hello
Create key
This should return the key and keyId
1curl http://127.0.0.1:8000/api/create
Protected Route
This will verify the key using Unkey and then grant access or deny it.
1curl -X POST http://127.0.0.1:8000/api/protected \2-H "Authorization: Bearer <UNKEY_ROOT_KEY>
2500 verifications and 100K successful rate‑limited requests per month. No CC required.