A secure RESTful API built with FastAPI that allows users to register, log in, and manage their personal notes. Designed for backend developers who want a modern, portfolio-ready project demonstrating authentication, CRUD operations, and database integration.
- User Authentication
- Register new users
- Password hashing with
Passlib - JWT token-based login
- Notes Management
- Create, read, update, delete notes
- Each user can only manage their own notes
- Database
- SQLAlchemy ORM
- SQLite (easy local setup, can switch to PostgreSQL)
- FastAPI & Pydantic
- Input validation
- Automatic OpenAPI docs
- JWT Authentication
- Users log in with username/password
- Receive a JWT token to access protected routes (notes)
- Token-based authorization ensures users can only access their own notes
- Python 3.12
- FastAPI
- Uvicorn
- SQLAlchemy
- Passlib
- Python-JOSE
- Pydantic
- SQLite (database)
fastapi-user-notes-api/
βββ app/
β βββ main.py # Application entry point
β βββ models.py # Database models
β βββ schemas.py # Pydantic schemas
β βββ database.py # SQLAlchemy database config
β βββ auth.py # JWT authentication utils (future)
β βββ routes/
β βββ init.py
β βββ users.py # User routes
β βββ notes.py # Notes routes
βββ requirements.txt
βββ README.md
- Clone the repository:
git clone https://github.com/your-username/fastapi-user-notes-api.git
cd fastapi-user-notes-api- Create and activate a virtual environment:
python -m venv venv
source venv/bin/activate # Windows: venv\Scripts\activate- Install dependencies:
pip install -r requirements.txt- Run the FastAPI server:
uvicorn app.main:app --reload- Open the API docs in your browser:
http://127.0.0.1:8000/docs
| Method | Endpoint | Description | Request Body Example | Response Example |
|---|---|---|---|---|
| POST | /users/ |
Register a new user | json { "username": "johndoe", "email": "john@example.com", "password": "secret123" } |
json { "id": 1, "username": "johndoe", "email": "john@example.com" } |
| POST | /users/login |
Login and get JWT token | x-www-form-urlencoded: username=johndoe, password=secret123 |
json { "access_token": "<jwt-token>", "token_type": "bearer" } |
| POST | /notes/ |
Create a new note (protected) | json { "title": "My Note", "content": "Some text" } |
json { "id": 1, "title": "My Note", "content": "Some text", "owner_id": 1 } |
| GET | /notes/ |
Get all notes (protected) | N/A | json [ { "id": 1, "title": "My Note", "content": "Some text", "owner_id": 1 } ] |
All /notes endpoints require a JWT token (use the βAuthorizeβ button in Swagger UI to paste the token).
For production, you should set a secure SECRET_KEY:
export SECRET_KEY="your-super-secret-key"- JWT Authentication & Authorization
- Implement login endpoint with JWT token generation
- Protect
/notes/routes so users can only access their own notes
- Database Upgrade
- Replace SQLite with PostgreSQL or MySQL for production-ready deployments
- Add database migrations using Alembic
- Enhanced CRUD Features
- Allow updating and deleting notes with proper permission checks
- Implement pagination and search/filtering for notes
- Testing & CI/CD
- Add unit and integration tests using
pytest - Set up GitHub Actions or other CI/CD pipelines for automated testing and deployment
- Add unit and integration tests using
- Dockerization & Deployment
- Create Dockerfile and docker-compose setup
- Deploy the app to cloud platforms like Render, Heroku, or AWS
- API Documentation Enhancements
- Improve OpenAPI docs with more examples and detailed descriptions
- Add API versioning for future compatibility
- Optional Features
- Allow users to categorize notes or add tags
- Implement user profile management (avatars, bio, etc.)