A simple and efficient FastAPI application for user management with full CRUD (Create, Read, Update, Delete) operations. This project demonstrates best practices for building RESTful APIs with FastAPI, SQLAlchemy, and Docker.
- ✅ Full CRUD operations for user management
- ✅ RESTful API design with proper HTTP status codes
- ✅ Data validation using Pydantic models
- ✅ SQLAlchemy ORM with SQLite database
- ✅ Docker containerization
- ✅ API documentation with Swagger UI
- ✅ Search functionality for users
- ✅ Pagination support
- ✅ Error handling and validation
- ✅ Health check endpoint
``` fastapi-user-crud/ ├── main.py # FastAPI application and API endpoints ├── models.py # SQLAlchemy database models ├── schemas.py # Pydantic models for request/response ├── crud.py # Database operations ├── database.py # Database configuration ├── requirements.txt # Python dependencies ├── Dockerfile # Docker configuration ├── docker-compose.yml # Docker Compose configuration ├── .dockerignore # Docker ignore file ├── .env.example # Environment variables example └── README.md # Project documentation ```
GET /- Welcome messageGET /health- Health check
POST /users/- Create a new userGET /users/- Get all users (with pagination and search)GET /users/{user_id}- Get a specific user by IDPUT /users/{user_id}- Update a specific userDELETE /users/{user_id}- Delete a specific userGET /users/stats/count- Get total count of users
-
Clone the repository ```bash git clone cd fastapi-user-crud ```
-
Build and run with Docker Compose ```bash docker-compose up --build ```
-
Access the API
- API: http://localhost:8000
- Interactive API docs: http://localhost:8000/docs
- Alternative docs: http://localhost:8000/redoc
-
Install dependencies ```bash pip install -r requirements.txt ```
-
Run the application ```bash uvicorn main:app --reload --host 0.0.0.0 --port 8000 ```
```bash
curl -X POST "http://localhost:8000/users/"
-H "Content-Type: application/json"
-d '{
"email": "[email protected]",
"first_name": "John",
"last_name": "Doe",
"password": "securepassword123",
"is_active": true
}'
```
```bash curl -X GET "http://localhost:8000/users/" ```
```bash curl -X GET "http://localhost:8000/users/?skip=0&limit=10&search=john" ```
```bash curl -X GET "http://localhost:8000/users/1" ```
```bash
curl -X PUT "http://localhost:8000/users/1"
-H "Content-Type: application/json"
-d '{
"first_name": "Jane",
"email": "[email protected]"
}'
```
```bash curl -X DELETE "http://localhost:8000/users/1" ```
```python class User(Base): id: int (Primary Key) email: str (Unique, Required) first_name: str (Required) last_name: str (Required) hashed_password: str (Required) is_active: bool (Default: True) created_at: datetime (Auto-generated) updated_at: datetime (Auto-updated) ```
Copy .env.example to .env and configure:
```env DATABASE_URL=sqlite:///./users.db API_HOST=0.0.0.0 API_PORT=8000 ```
```bash docker build -t fastapi-user-crud . ```
```bash docker run -p 8000:8000 fastapi-user-crud ```
```bash
docker-compose up -d
docker-compose logs -f
docker-compose down ```
- Update models in
models.py - Create/update Pydantic schemas in
schemas.py - Add database operations in
crud.py - Create API endpoints in
main.py
For production use, consider using Alembic for database migrations: ```bash pip install alembic alembic init alembic ```
-
Security
- Use proper password hashing (bcrypt, scrypt)
- Implement JWT authentication
- Add rate limiting
- Use HTTPS
-
Database
- Switch to PostgreSQL for production
- Implement connection pooling
- Add database migrations
-
Monitoring
- Add logging
- Implement metrics collection
- Set up health checks
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests
- Submit a pull request
This project is licensed under the MIT License.