Skip to content
darshanr27Public

Repository files navigation

Notes API

A RESTful API for managing notes with user authentication, built with FastAPI and PostgreSQL.


Live API

Base URL: https://notes-api-7zrq.onrender.com Swagger UI: https://notes-api-7zrq.onrender.com/docs

How to Test

  1. Signup — POST /auth/signup with email and password
  2. Login — POST /auth/login, copy the access_token from the response
  3. Authorize — Click the 🔒 Authorize button in Swagger UI, paste the token
  4. Test Notes routes — All /notes endpoints are now accessible

Tech Stack

  • FastAPI — API framework
  • SQLAlchemy — ORM for database operations
  • Alembic — Database migrations
  • PostgreSQL (Supabase) — Database
  • Pydantic — Request/response validation
  • JWT (python-jose) — Authentication
  • Passlib + bcrypt — Password hashing

Folder Structure

notes-api/
├── main.py                  # App entry point
├── database.py              # Engine, SessionLocal, Base, get_db
├── .env                     # Environment variables (not committed)
├── alembic/                 # Migration files
├── models/
│   ├── note.py              # SQLAlchemy Note model
│   └── user.py              # SQLAlchemy User model
├── schemas/
│   ├── note.py              # Pydantic NoteSchema
│   └── user.py              # Pydantic UserCreate, UserResponse, LoginSchema
├── routers/
│   ├── notes.py             # Note route handlers
│   └── auth.py              # Auth route handlers
└── services/
    ├── note_service.py      # Note DB logic
    └── auth_service.py      # Password hashing, JWT creation

Local Setup

1. Clone the repo

git clone https://github.com/darshanr27/notes-api
cd notes-api

2. Create and activate virtual environment

python -m venv venv

# Mac/Linux
source venv/bin/activate

# Windows
venv\Scripts\activate

3. Install dependencies

pip install fastapi sqlalchemy alembic pydantic python-jose passlib bcrypt==4.0.1 passlib==1.7.4 python-dotenv psycopg2-binary uvicorn

4. Set up environment variables

Create a .env file in the project root:

DATABASE_URL=postgresql://postgres:yourpassword@db.xxxx.supabase.co:5432/postgres
SECRET_KEY=your_random_secret_key
ALGORITHM=HS256
ACCESS_TOKEN_EXPIRY_MINUTES=30

Generate a secure SECRET_KEY:

openssl rand -hex 32

5. Run database migrations

alembic upgrade head

6. Start the server

fastapi dev main.py

API runs at http://localhost:8000 Swagger UI at http://localhost:8000/docs


API Endpoints

Auth

Method Route Description
POST /auth/signup Register a new user
POST /auth/login Login and receive JWT token

Notes

Method Route Description
POST /notes Create a new note
GET /notes Get all notes (optional ?title= filter)
GET /notes/{id} Get a single note by id
PUT /notes/{id} Update a note
DELETE /notes/{id} Delete a note

Example Requests

Signup

POST /auth/signup
{
  "email": "user@example.com",
  "password": "yourpassword"
}

Login

POST /auth/login
{
  "email": "user@example.com",
  "password": "yourpassword"
}

Response:

{
  "access_token": "eyJ...",
  "token_type": "bearer"
}

Create Note

POST /notes
{
  "id": 1,
  "title": "My first note",
  "content": "Some content here",
  "created_at": "2024-01-15T10:00:00"
}

Database Migrations

# Create a new migration after model changes
alembic revision --autogenerate -m "description"

# Apply migrations
alembic upgrade head

# Rollback one step
alembic downgrade -1

# Check current version
alembic current

Contributors

Languages