Skip to content

Latest commit

Β 

History

36 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🎌 AnimeDex API

A FastAPI-based REST API for managing anime data, evolved from a simple CRUD application into a structured backend with SQLAlchemy, JWT authentication, API-key authorization, middleware, modular routing, and environment-based configuration.

AnimeDex V4 focuses on practical backend engineering concepts such as REST API design, database management, authentication, authorization, validation, dependency injection, and separation of concerns.


πŸš€ V4 Highlights

AnimeDex V4 introduces a major backend upgrade over V3:

  • πŸ” JWT-based user authentication
  • πŸ”‘ API-key based authorization
  • πŸ”’ Secure password hashing with pwdlib
  • πŸ›‘οΈ JWT + API-key ownership verification for sensitive operations
  • 🧩 Modular routing with FastAPI APIRouter
  • ⚑ Custom request-timing middleware
  • πŸ—„οΈ SQLAlchemy 2.0 ORM with SQLite
  • ✏️ Full and partial anime updates using PUT and PATCH
  • βš™οΈ Environment-based configuration with Pydantic Settings
  • βœ… Structured validation and error handling

✨ Features

πŸ“š Anime API

  • Get all anime
  • Get anime by ID
  • Get a random anime
  • Get top 10 highest-rated anime
  • Retrieve unique genres
  • Retrieve unique studios
  • Retrieve unique statuses
  • Filter anime by genre, studio, and status
  • Create anime
  • Fully update anime
  • Partially update anime
  • Delete anime

πŸ” Authentication

  • User registration
  • Secure password hashing
  • User login
  • JWT access-token generation
  • JWT validation
  • Bearer authentication
  • Protected endpoints

πŸ”‘ API Keys

  • Generate API keys for authenticated users
  • Cryptographically secure key generation
  • Store API-key secrets as hashes
  • Validate API keys
  • Check API-key status
  • Verify API-key ownership

⚑ Middleware

Custom TimerMiddleware measures request processing time and adds the result to the response:

X-Process-Time: 0.00421

πŸ›‘οΈ Authentication & Authorization

AnimeDex V4 uses two layers of security for sensitive operations.

JWT Authentication

After registering and logging in, the client receives a JWT access token.

Authorization: Bearer <access_token>

The JWT identifies the authenticated user.

API-Key Authorization

Authenticated users can generate an API key:

POST /api_key/api_keys

The API key is supplied using:

X-API-Key: <api_key>

The server validates the key, checks its status, and verifies that it belongs to the authenticated user.

Protected Request Flow

Client Request
      β”‚
      β–Ό
JWT Authentication
      β”‚
      β–Ό
Identify User
      β”‚
      β–Ό
API Key Validation
      β”‚
      β–Ό
Verify Key Ownership
      β”‚
      β–Ό
Perform Operation

This demonstrates the distinction between:

  • Authentication β€” Who are you?
  • Authorization β€” Are you allowed to perform this operation?

✏️ PUT vs PATCH

AnimeDex V4 supports both full and partial updates.

PUT

Used for a full update of an anime resource.

PUT /anime/{id}

PATCH

Used when only specific fields need to be changed.

PATCH /anime/{id}

For example:

{
  "rating": 9.3,
  "status": "Completed"
}

This allows partial resource modification without resending the entire anime object.


πŸ—„οΈ Database

AnimeDex uses SQLite for persistent storage and SQLAlchemy 2.0 as the ORM.

Key database concepts used:

  • SQLAlchemy ORM
  • Mapped and mapped_column
  • Database sessions
  • Dependency-injected sessions
  • CRUD abstraction
  • Model relationships
  • Enum-based fields
  • Persistent SQLite storage

πŸ› οΈ Tech Stack

Technology Purpose
Python Programming language
FastAPI REST API framework
SQLAlchemy 2.0 ORM
SQLite Database
Pydantic Validation & schemas
Pydantic Settings Configuration
pwdlib Password hashing
joserfc JWT handling
Starlette Middleware
Uvicorn ASGI server

πŸ“ Project Structure

Anime-Dex/
β”‚
β”œβ”€β”€ app/
β”‚   β”œβ”€β”€ routers/
β”‚   β”‚   β”œβ”€β”€ anime.py          # Anime routes
β”‚   β”‚   β”œβ”€β”€ user.py           # Authentication routes
β”‚   β”‚   └── apikey_route.py   # API-key routes
β”‚   β”‚
β”‚   β”œβ”€β”€ crud.py               # Database operations
β”‚   β”œβ”€β”€ database.py           # Database configuration
β”‚   β”œβ”€β”€ dependencies.py       # Shared dependencies & authentication
β”‚   β”œβ”€β”€ enums.py              # Enum definitions
β”‚   β”œβ”€β”€ main.py               # FastAPI application
β”‚   β”œβ”€β”€ middleware.py         # Request timing middleware
β”‚   β”œβ”€β”€ models.py             # SQLAlchemy models
β”‚   β”œβ”€β”€ schemas.py            # Pydantic schemas
β”‚   β”œβ”€β”€ config.py              # Application configuration
β”‚   └── utils.py               # Authentication utilities
β”‚
β”œβ”€β”€ seed.py
β”œβ”€β”€ requirements.txt
β”œβ”€β”€ README.md
β”œβ”€β”€ .gitignore
└── LICENSE

πŸ“‘ API Endpoints

General

Method Endpoint Description
GET / API home

Anime

Method Endpoint Auth
GET /anime/ β€”
GET /anime/{id} β€”
GET /anime/random β€”
GET /anime/top10 β€”
GET /anime/genres β€”
GET /anime/studios β€”
GET /anime/statuses β€”
POST /anime/ JWT
PUT /anime/{id} JWT
PATCH /anime/{id} JWT + API Key
DELETE /anime/{id} JWT + API Key

Users

Method Endpoint Description
POST /user/register Register a user
POST /user/login Login and receive JWT

API Keys

Method Endpoint Auth
POST /api_key/api_keys JWT

πŸ”Ž Filtering

Anime can be filtered using query parameters:

GET /anime/?genre=Action
GET /anime/?studio=Madhouse
GET /anime/?status=Completed

Multiple filters can be combined:

GET /anime/?genre=Action&studio=MAPPA&status=Completed

πŸ“ Example Anime Object

{
  "title": "Monster",
  "genre": "Thriller",
  "episodes": 74,
  "rating": 9.2,
  "studio": "Madhouse",
  "release_year": 2004,
  "status": "Completed"
}

βš™οΈ Installation

1. Clone the repository

git clone https://github.com/Pranavkr323/Anime-Dex.git
cd Anime-Dex

2. Create a virtual environment

python -m venv venv

3. Activate the environment

Windows

venv\Scripts\activate

macOS / Linux

source venv/bin/activate

4. Install dependencies

pip install -r requirements.txt

5. Configure environment variables

Create a .env file with the required application configuration.

Do not commit secrets such as JWT signing keys to the repository.

6. Run the server

uvicorn app.main:app --reload

The API will be available at:

http://127.0.0.1:8000

πŸ“– API Documentation

FastAPI automatically provides interactive documentation.


πŸ“Έ API Preview

Swagger UI

Interactive API documentation generated automatically by FastAPI.

AnimeDex Swagger UI AnimeDex Swagger UI

JWT + API Key Authorization

Protected endpoints require JWT authentication along with a valid API key where applicable.

JWT and API Key Verification


Swagger UI

http://127.0.0.1:8000/docs

ReDoc

http://127.0.0.1:8000/redoc

Swagger UI can be used to explore endpoints, inspect schemas, authenticate with JWT, provide API keys, and test protected operations.


🎯 What I Learned

Backend Development

  • REST API design
  • FastAPI application structure
  • APIRouter
  • Dependency injection
  • Middleware
  • Request/response handling
  • HTTP status codes
  • Exception handling

Database

  • SQLAlchemy 2.0
  • ORM-based database operations
  • SQLite
  • Database sessions
  • CRUD abstraction
  • Model relationships

Authentication & Security

  • Password hashing
  • JWT authentication
  • Bearer authentication
  • Token validation
  • API-key generation
  • API-key hashing
  • API-key verification
  • Ownership-based authorization
  • Authentication vs authorization

API Design

  • Query parameter filtering
  • PUT vs PATCH
  • Pydantic schemas
  • Validation
  • Separation of concerns
  • Modular backend architecture

🚧 Future Improvements β€” V5

Potential areas for the next version:

  • Automated testing with Pytest
  • Database migrations with Alembic
  • PostgreSQL support
  • Pagination
  • Async SQLAlchemy
  • Dockerization
  • CI/CD
  • API rate limiting
  • Logging & monitoring
  • Production deployment
  • Anime watchlists & favorites

πŸ“„ License

This project is licensed under the MIT License.


πŸ‘¨β€πŸ’» Author

Pranav Kumar

Backend Developer β€’ Python β€’ FastAPI β€’ SQLAlchemy

If you found AnimeDex useful or interesting, consider giving the repository a ⭐.

About

A FastAPI REST API for managing anime data, featuring SQLAlchemy, authentication, authorization, API keys, middleware, validation, and modular backend architecture.

Topics

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages