A simple FastAPI-based REST API for generating JSON Web Tokens (JWT). Perfect for testing, development, and prototyping applications that need JWT functionality.
- 🔐 Generate JWTs with custom payloads
- ⚙️ Configurable JWT options (algorithm, expiration)
- 📝 Automatic payload enrichment (iat, exp timestamps)
- 🚀 Fast and lightweight FastAPI implementation
- 📖 Auto-generated API documentation
- ✅ Easy testing with Hurl
- Python 3.7+
- pip or uv (recommended)
-
Install uv if you haven't already:
# macOS/Linux curl -LsSf https://astral.sh/uv/install.sh | sh # Windows powershell -c "irm https://astral.sh/uv/install.ps1 | iex" # Or via pip pip install uv
-
Clone or download the project files
-
Install dependencies and run:
# Install dependencies uv pip install -r requirements.txt # Or create a virtual environment and install uv venv source .venv/bin/activate # On Windows: .venv\Scripts\activate uv pip install -r requirements.txt # Run the API python main.py
-
Clone or download the project files
-
Install dependencies:
pip install -r requirements.txt
-
Run the API:
python main.py
The API will be available at http://localhost:8000
Once the server is running, you can view the interactive API documentation at:
- Swagger UI:
http://localhost:8000/docs - ReDoc:
http://localhost:8000/redoc
Generates a JWT token with the provided key and payload.
{
"key": "your_secret_key",
"body": {
"user_id": 123,
"username": "john_doe",
"role": "admin"
},
"options": {
"algorithm": "HS256",
"add_exp": true
}
}| Field | Type | Required | Description |
|---|---|---|---|
key |
string | ✅ Yes | Secret key used to sign the JWT |
body |
object | ❌ No | JWT payload/claims (default: {}) |
options |
object | ❌ No | JWT generation options |
| Option | Type | Default | Description |
|---|---|---|---|
algorithm |
string | "HS256" |
JWT signing algorithm |
add_exp |
boolean | true |
Auto-add expiration time (1 hour) |
{
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoxMjMsInVzZXJuYW1lIjoiam9obl9kb2UiLCJyb2xlIjoiYWRtaW4iLCJpYXQiOjE2OTQ1MjEyMDAsImV4cCI6MTY5NDUyNDgwMH0.signature"
}curl -X POST "http://localhost:8000/generate_jwt" \
-H "Content-Type: application/json" \
-d '{
"key": "my_secret_key",
"body": {
"user_id": 123,
"username": "testuser"
}
}'curl -X POST "http://localhost:8000/generate_jwt" \
-H "Content-Type: application/json" \
-d '{
"key": "my_secret_key",
"body": {
"service": "api",
"permissions": ["read", "write"]
},
"options": {
"algorithm": "HS512",
"add_exp": false
}
}'A Hurl test file is included for easy API testing:
# Run the simple JWT generation test
hurl --test simple_jwt_capture.hurlThe test will:
- Generate a JWT token
- Capture it in a variable for potential reuse
- Validate the response format
The API automatically adds these fields to your JWT payload if not present:
iat(issued at): Current UTC timestampexp(expiration): Current UTC timestamp + 1 hour (ifadd_exp: true)
- HS256 (HMAC SHA-256) - Default
- HS384 (HMAC SHA-384)
- HS512 (HMAC SHA-512)
- RS256 (RSA SHA-256)
- RS384 (RSA SHA-384)
- RS512 (RSA SHA-512)
- ES256 (ECDSA SHA-256)
- ES384 (ECDSA SHA-384)
- ES512 (ECDSA SHA-512)
Note: RSA and ECDSA algorithms require appropriate key formats
The API returns appropriate HTTP status codes and error messages:
400 Bad Request: Invalid request format or missing required fields500 Internal Server Error: Server-side errors (invalid algorithms, etc.)
Example error response:
{
"detail": "Internal server error: Invalid algorithm specified"
}jwt-generator-api/
├── main.py # FastAPI application
├── requirements.txt # Python dependencies
├── simple_jwt_capture.hurl # Hurl test file
└── README.md # This file
- FastAPI: Modern web framework for APIs
- uvicorn: ASGI server for running FastAPI
- PyJWT: JWT implementation for Python
-
Secret Key Management: Never hardcode secret keys in production. Use environment variables or secure key management systems.
-
Key Strength: Use strong, randomly generated secret keys (minimum 256 bits for HS256).
-
Algorithm Selection: Be explicit about allowed algorithms to prevent algorithm confusion attacks.
-
Production Use: This API is designed for development/testing. For production, consider additional security measures like rate limiting, input validation, and secure key storage.
This project is provided as-is for educational and development purposes.