Skip to main content

Overview

The Football Kit Archive API implements rate limiting to protect against abuse and ensure fair usage for all users. Rate limits are applied per IP address and are enforced through middleware.

Default Rate Limit

By default, the API allows:
  • 100 requests per hour per IP address
  • Rate limit window: 1 hour (3600 seconds)
From core/middleware.py:32-38:

Configuration

Custom Rate Limit

Configure the rate limit in your Django settings:

Environment Variable

You can also set the rate limit via environment variable:

Examples

How It Works

IP-Based Tracking

Rate limits are tracked per client IP address:

Cache-Based Storage

Rate limit counters are stored in Django’s cache system:

Rolling Window

The rate limit uses a rolling 1-hour window. The counter resets after 3600 seconds:

Rate Limit Response

When the rate limit is exceeded, the API returns: Status Code: 403 Forbidden Response:
From core/middleware.py:70-71:

Error Response Format

The error handler in fkapi/api.py:247-248 formats rate limit errors:

Handling Rate Limits

Exponential Backoff

Implement exponential backoff when rate limited:

Check Rate Limit Status

Monitor your usage by tracking response headers (if implemented) or by counting requests:

JavaScript Example

IP Whitelisting

Exempt specific IP addresses from rate limiting:
From core/middleware.py:41-44:
Whitelisted IPs bypass rate limiting entirely:

Proxy Configuration

X-Forwarded-For Header

If your API is behind a proxy or load balancer, enable X-Forwarded-For header trust:
Only enable TRUST_X_FORWARDED_FOR if your API is behind a trusted proxy. Otherwise, clients can spoof their IP address.

Testing Rate Limits

Simple Test Script

Python Test Script

Best Practices

For API Consumers

  1. Implement Retry Logic: Use exponential backoff when rate limited
  2. Track Your Usage: Monitor request counts to avoid hitting limits
  3. Batch Requests: Use bulk endpoints when available (e.g., /api/kits/bulk)
  4. Cache Responses: Cache API responses locally to reduce request volume
  5. Handle 403 Errors: Always check for rate limit errors and wait before retrying

For API Administrators

  1. Monitor Usage: Track API metrics to identify abuse patterns
  2. Adjust Limits: Increase limits for trusted users or applications
  3. Use Whitelisting: Exempt internal services from rate limiting
  4. Set Up Alerts: Get notified when users hit rate limits frequently
  5. Document Limits: Clearly communicate rate limits to API users

Monitoring Rate Limit Usage

Check API metrics to see rate limit statistics:
Response:
The 403 status codes indicate rate limit violations.

Error Handling

Learn how to handle rate limit errors

Authentication

Combine rate limiting with API authentication