Skip to content

REST API built with Flask, featuring structured input validation, centralized logging, robust error handling, and simple IP-based rate limiting. The API includes performant request logging, safe JSON parsing, validated POST endpoints, and a modular architecture suitable for scaling into larger services.

Notifications You must be signed in to change notification settings

talhayilmazc/Secure-REST-API-with-Validation-Logging-Simple-Rate-Limiting

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Secure REST API with Validation, Logging & Simple Rate Limiting

πŸ“Œ Description

This project implements a small but realistic secure REST API using Flask. It focuses on:

  • Input validation for incoming JSON payloads
  • Structured error handling with JSON responses
  • Request/response logging to a file
  • Simple in-memory rate limiting per client IP
  • Clean, readable Python code suitable for learning, demos or small prototypes

The API exposes a minimal /api/v1/items resource that supports creating and listing items. Data is kept in memory for simplicity (no external database required).

🏷️ Suggested GitHub Topics

You can use the following topics/tags in your repository:

flask β€’ rest-api β€’ api-security β€’ input-validation β€’ rate-limiting β€’
logging β€’ python-project β€’ backend-development β€’ web-api

πŸš€ Features

  • POST /api/v1/items
    Creates an item with validated fields: name (string), quantity (int β‰₯ 1), price (float β‰₯ 0).

  • GET /api/v1/items
    Returns the list of all created items in memory.

  • GET /health
    Simple health-check endpoint.

Security-Oriented Behaviors

  • Rejects invalid JSON payloads with a clear error message
  • Validates field types and constraints
  • Logs each request with method, path, status code and client IP
  • Applies a basic, IP-based rate limit window to mitigate abuse
  • Hides internal error details behind generic 500 responses

πŸ“‚ Project Structure

  • src/app.py – Main Flask application (routes, validation, rate limiting, error handlers)
  • src/config.py – Configuration values (rate limit, log file path, etc.)
  • src/rate_limiter.py – Simple in-memory rate limiter implementation
  • src/validators.py – Input validation utilities
  • src/logging_conf.py – Central logging configuration
  • logs/app.log – Request and error logs (created at runtime)
  • requirements.txt – Python dependencies

πŸ”§ Installation & Usage

  1. (Optional) Create and activate a virtual environment:
python -m venv .venv
source .venv/bin/activate  # On Windows: .venv\Scripts\activate
  1. Install dependencies:
pip install -r requirements.txt
  1. Run the API:
python src/app.py
  1. Example requests (using curl):
# Health check
curl http://127.0.0.1:5000/health

# Create an item
curl -X POST http://127.0.0.1:5000/api/v1/items   -H "Content-Type: application/json"   -d '{ "name": "Laptop", "quantity": 2, "price": 1499.90 }'

# List items
curl http://127.0.0.1:5000/api/v1/items

This project is intentionally small but written in a clean and extensible way, so you can easily grow it into a larger service or use it as a teaching example for secure API design basics.

About

REST API built with Flask, featuring structured input validation, centralized logging, robust error handling, and simple IP-based rate limiting. The API includes performant request logging, safe JSON parsing, validated POST endpoints, and a modular architecture suitable for scaling into larger services.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages