Skip to content

Nikhil18Patil/URL_SHORTNER_FastAPI

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

6 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🌍 What Is This Project?

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

πŸ’‘ What Does the API Do?

It has 2 main endpoints:

πŸ”Ή POST /shorten

  • 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).


πŸ”Ή GET /{short_code}

  • Input: A short code from the URL (like bM9)
  • Output: Redirects to the original long URL

🧠 How Does It Ensure Uniqueness?

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).

This ensures:

  • πŸ”’ 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.


πŸ“ Project Structure

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

πŸ” Fallback to SQLite

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)

πŸ› οΈ How to Run the Project

  1. Clone the repo or create files as shown

  2. Create a virtual environment (optional but recommended)

python -m venv venv
source venv/bin/activate  # or `venv\Scripts\activate` on Windows
  1. Install dependencies
pip install -r requirements.txt

If requirements.txt doesn't exist yet, use:

pip install fastapi uvicorn sqlalchemy psycopg2-binary
  1. Run the app
uvicorn app.main:app --reload

πŸ§ͺ Test the API

πŸ”Έ POST /shorten

POST http://localhost:8000/shorten
Content-Type: application/json

{
  "original_url": "https://openai.com"
}

Response:

{
  "short_url": "http://localhost:8000/bM9"
}

πŸ”Έ GET /bM9

Open in browser:

http://localhost:8000/bM9

It redirects you to:

https://openai.com

Releases

No releases published

Packages

No packages published

Languages