This is a clean, modular URL Shortener API built using:
- ✅ FastAPI for the web API
- ✅ PostgreSQL as the main database (with SQLite fallback if Postgres is unavailable)
- ✅ Base62 encoding for short, unique, and human-friendly short codes
It has 2 main endpoints:
- Input: A long URL (e.g.,
https://www.google.com) - Output: A short URL (e.g.,
http://localhost:8000/bM9)
✅ If the same URL is sent again, it returns the existing short URL (no duplication).
- Input: A short code from the URL (like
bM9) - Output: Redirects to the original long URL
Short code generation is deterministic and unique:
- When a new URL is added, it's assigned a unique auto-incrementing ID in the database.
- This ID is converted into a Base62 string (digits + A–Z + a–z).
- 🔒 Codes are always unique
- ❌ No random collisions
- 📏 Codes are shorter over time (first few are just 1–2 characters like
1,a,B,z, etc.)
✅ Even if a user sends the same URL again — it reuses the original short code.
url_shortener/
├── app/
│ ├── main.py # FastAPI app entry point
│ ├── database.py # Sets up DB connection (Postgres or fallback to SQLite)
│ ├── models.py # DB table definition using SQLAlchemy
│ ├── schemas.py # Request/Response format validation (Pydantic)
│ ├── utils.py # Encodes integer ID to Base62
│ └── routers/
│ └── url.py # Main routing logic: shortening and redirecting
└── requirements.txt # All needed packages
If DATABASE_URL is not set (e.g. in .env or environment variables), it automatically uses:
sqlite:///./url_shortener.db
This helps:
- ✅ Run the project locally without setting up Postgres
- 🔁 Still allows switching to Postgres in production (just set the
DATABASE_URL)
-
Clone the repo or create files as shown
-
Create a virtual environment (optional but recommended)
python -m venv venv
source venv/bin/activate # or `venv\Scripts\activate` on Windows- Install dependencies
pip install -r requirements.txtIf
requirements.txtdoesn't exist yet, use:
pip install fastapi uvicorn sqlalchemy psycopg2-binary- Run the app
uvicorn app.main:app --reloadPOST http://localhost:8000/shorten
Content-Type: application/json
{
"original_url": "https://openai.com"
}Response:
{
"short_url": "http://localhost:8000/bM9"
}Open in browser:
http://localhost:8000/bM9
It redirects you to:
https://openai.com