TinySearch API provides secure authentication mechanisms and rate limiting features to protect API access and prevent excessive usage. This document explains how to configure and use these features.
- Configuring Authentication and Rate Limiting
- API Key Management
- Using API Keys in Requests
- Rate Limiting Mechanism
- Authentication in the Web UI
- Example Script
Set authentication and rate limiting parameters in the configuration file:
# API configuration
api:
# Authentication settings
auth_enabled: true # Enable API authentication
default_key: "your-secure-api-key" # Default API key
master_key: "your-master-key" # Master key for creating new API keys
# Rate limiting settings
rate_limit_enabled: true # Enable rate limiting
rate_limit: 60 # Maximum number of requests
rate_limit_window: 60 # Time window in secondsA complete configuration example can be found in examples/api_config.yaml.
Create a new API key using the master key:
curl -X POST "http://localhost:8000/api-key?expires_in_days=30" \
-H "master-key: your-master-key"Response:
{
"api_key": "generated-api-key-value",
"expires_at": "2023-12-31T23:59:59"
}Parameters:
expires_in_days: Days until the API key expires (optional, omit for non-expiring keys)
Add the X-API-Key header to all API requests:
# Query example
curl -X POST "http://localhost:8000/query" \
-H "X-API-Key: your-api-key" \
-H "Content-Type: application/json" \
-d '{"query": "search content", "top_k": 5}'
# Index building example
curl -X POST "http://localhost:8000/index/build" \
-H "X-API-Key: your-api-key" \
-H "Content-Type: application/json" \
-d '{"data_path": "/path/to/data", "recursive": true}'Rate limiting is implemented using a sliding window algorithm and can be adjusted in the configuration:
rate_limit: Maximum number of requests allowed within the time windowrate_limit_window: Size of the time window in seconds
When rate limits are exceeded, the API returns a 429 Too Many Requests status code with a Retry-After header indicating how long the client should wait.
HTTP/1.1 429 Too Many Requests
Retry-After: 5
Content-Type: application/json
{
"detail": "Rate limit exceeded. Try again in 5 seconds."
}
The TinySearch Web UI provides API authentication management features:
- Click on the "Authentication" tab in the navigation menu
- Set and save your API key
- Generate new API keys using the master key
The Web UI automatically includes authentication information in all API requests.
A complete authentication and rate limiting demonstration script is provided in examples/api_auth_demo.py, which includes:
- Starting an API server with authentication and rate limiting
- Testing different authentication scenarios (no key, invalid key, and valid key)
- Generating a new API key and testing it
- Demonstrating rate limiting functionality
Run the example script:
python examples/api_auth_demo.py- Always use strong keys and avoid defaults
- Rotate API keys regularly
- Enable TLS/HTTPS in production environments
- Use different API keys for different users for better tracking and revocation
- Configure reasonable rate limits based on expected usage patterns