This is a User Management API built with FastAPI, SQLAlchemy, and PostgreSQL. The API allows you to create, read, and delete users, following best practices such as data validation with Pydantic, database session management, and dependency injection.
✅ CRUD Operations – Create, Read, and Delete users
✅ Database Integration – PostgreSQL with SQLAlchemy ORM
✅ Pydantic Validation – Ensures proper data validation
✅ Dependency Injection – Efficient database session management
✅ Exception Handling – Returns meaningful error messages
✅ Modular Structure – Organized codebase for maintainability
git clone https://github.com/gideon877/fastapi-user-management.git
cd fastapi-user-managementpython -m venv venv
source venv/bin/activate # On Windows use `venv\Scripts\activate`pip install fastapi uvicorn sqlalchemy databases pydantic psycopg psycopg2-binary python-dotenvCreate a .env file and add your PostgreSQL database URL:
DATABASE_URL=postgresql://username:password@localhost/dbnameuvicorn main:app --reload📂 fastapi-user-management
┣ 📜 main.py # FastAPI app with API endpoints
┣ 📜 model.py # SQLAlchemy database models
┣ 📜 schema.py # Pydantic schemas for validation
┣ 📜 services.py # Business logic and database operations
┣ 📜 database.py # Database connection and session management
┣ 📜 .env # Environment variables
┗ 📜 README.md # API Documentation
POST /users/
Creates a new user.
Request Body (JSON)
{
"first_name": "John",
"last_name": "Doe",
"age": 25,
"gender": "Male",
"race": "Caucasian"
}Response
{
"id": 1,
"first_name": "John",
"last_name": "Doe",
"age": 25,
"gender": "Male",
"race": "Caucasian"
}GET /users/
Fetches a paginated list of users.
Query Parameters
skip(default:0) → Number of records to skiplimit(default:10) → Number of records to return
Response
[
{
"id": 1,
"first_name": "John",
"last_name": "Doe",
"age": 25,
"gender": "Male",
"race": "Caucasian"
},
{
"id": 2,
"first_name": "Jane",
"last_name": "Smith",
"age": 30,
"gender": "Female",
"race": "Asian"
}
]GET /users/{user_id}
Fetches details of a single user.
Example Request:
GET /users/1
Response:
{
"id": 1,
"first_name": "John",
"last_name": "Doe",
"age": 25,
"gender": "Male",
"race": "Caucasian"
}Error Response:
{
"detail": "User not found"
}DELETE /users/{user_id}
Deletes a user by ID.
Example Request:
DELETE /users/1
Response:
{
"message": "User deleted successfully"
}Error Response:
{
"detail": "User not found"
}✅ Add filters and statistics (e.g., most common names, average age)
✅ Implement user update functionality
✅ Add authentication and authorization
Feel free to open issues and submit pull requests. Let's build something great together!
This project is licensed under the MIT License.