A FastAPI-based URL Shortener service that allows users to convert long URLs into shortened links, and redirect users to the original URLs using the short codes.
- Shorten long URLs with a unique short code
- Redirect users from short code to the original URL
- PostgreSQL-backed persistent storage
- Fast and lightweight, built with FastAPI
Description: Accepts a long URL and returns a shortened version.
Request Body:
{
"long_url": "https://example.com"
}
Response:
{
"short_url": "http://localhost:8000/abc123"
}
Description: Redirects the user to the original long URL associated with the short code.
This project uses PostgreSQL as its database. Ensure the following environment variables are set:
Variable | Description |
---|---|
DB_HOST |
Hostname of the PostgreSQL server |
DB_PORT |
Port number (usually 5432 ) |
DB_USER |
Username for the database |
DB_PASS |
Password for the database |
DB_NAME |
Database name |
The database should have a table called urls
with the following schema:
CREATE TABLE urls (
id SERIAL PRIMARY KEY,
long_url TEXT NOT NULL,
short_code TEXT UNIQUE NOT NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
git clone https://github.com/your-username/urlshort.git
cd urlshort
pip install -r requirements.txt
You can use a .env
file or export variables directly in your terminal:
export DB_HOST=localhost
export DB_PORT=5432
export DB_USER=your_user
export DB_PASS=your_password
export DB_NAME=your_db
uvicorn app.main:app --reload
The API will be available at: http://localhost:8000
urlshort/
├── app/ # Application module
│ ├── database.py # DB connection setup
│ ├── main.py # FastAPI routes & logic
│ └── models.py # SQLAlchemy models/schema
│
├── output_IMG/ # API flow screenshots
│ ├── DB.png
│ ├── GetRequest.png
│ └── PostRequest.png
│
├── .env # Environment variables
├── .gitignore # Git ignored files
├── README.md # Project documentation
└── requirements.txt # Python dependencies