A backend Django application that implements a simple IP-based API rate limiting mechanism using a fixed time window strategy. This protects APIs from abuse or spam by throttling excessive requests from the same IP.
- Tracks API requests per IP address
- Allows configurable request limits and time windows
- Blocks requests exceeding the limit
- Sends meaningful error responses to the client
- Logs and stores request data in a SQLite database
- Unit tested using
pytest - Easily testable with Postman
- Language: Python
- Framework: Django, Django REST Framework (DRF)
- Database: SQLite
- Testing: Pytest
- Client Testing Tool: Postman
- Version Control: Git & GitHub
rate_limiter_project/ β βββ limiter/ # Django app β βββ models.py # IPRequestLog model for tracking β βββ views.py # Main API view with rate limiter logic β βββ tests.py # Unit tests β βββ urls.py # API route β βββ ... βββ rate_limiter_project/ β βββ settings.py βββ db.sqlite3 # SQLite database βββ manage.py βββ README.md
git clone https://github.com/your-username/your-repo-name.git
cd your-repo-name
2. Set Up Virtual Environment (optional but recommended)
python -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
3. Install Dependencies
pip install -r requirements.txt
4. Run Migrations
python manage.py migrate
5. Run Development Server
python manage.py runserver
The API will be available at: http://127.0.0.1:8000/api/
π§ͺ Running Tests
Make sure pytest is installed:
pip install pytest
Then run tests:
pytest
π§ͺ API Endpoint Example
POST /api/limit/
Use Postman or curl to make repeated requests. After 5 requests (default limit), further requests from the same IP will be blocked temporarily.
π How It Works
The user sends a request to the /api/limit/ endpoint.
The app checks how many requests this IP has made in the last N seconds.
If it's within the allowed limit, it logs the request and sends a success response.
If the limit is exceeded, it returns 429 Too Many Requests.
π§ Concepts Used
Custom DRF views
Request throttling logic (manual)
IP extraction from headers
Query filtering with timestamps
Unit testing API views
SQLite ORM models
Serializers to control output
π Requirements
If you're creating requirements.txt, hereβs a minimal version:
Django>=4.0
djangorestframework
pytest
π©βπ» Author
Sri Kaaviya
GitHub: @srikaaviya
π License
This project is licensed under the MIT License.