A simple URL shortener built with Flask and SQLite. Paste in a long link, get back a short one that redirects to the original when visited. Built this as part of my CodeAlpha internship.
- Shortens any long URL into a random 6-character code
- Lets you set your own custom short code instead of a random one, if you want
- Redirects
yoursite.com/abc123straight to the original long URL - Tracks how many times each short link has been clicked
- If you shorten the same URL twice, it just gives you back the same short code instead of creating a duplicate
- Has a small web page to test it out — no need to use Postman or curl
- Backend: Python (Flask)
- Database: SQLite
- Frontend: Plain HTML, CSS and JavaScript (no frameworks used)
- Clone the repo
git clone https://github.com/sachin8808/CodeAlpha_URL_Shortener.git
cd CodeAlpha_URL_Shortener
- Install the dependencies
pip install -r requirements.txt
- Run the app
python app.py
- Open your browser and go to
http://127.0.0.1:5000
That's it, no extra config or environment variables needed. The SQLite database file gets created automatically the first time you run it.
| Method | Route | What it does |
|---|---|---|
| POST | /api/shorten |
Send { "url": "..." } (and optionally "custom_code") to get a short link back |
| GET | /<short_code> |
Redirects to the original long URL |
| GET | /api/urls |
Lists all shortened URLs |
| GET | /api/urls/<short_code> |
Get info about one specific short link |
| DELETE | /api/urls/<short_code> |
Delete a short link |
POST /api/shorten
Content-Type: application/json
{
"url": "https://www.example.com/some/really/long/path?x=1"
}
Response:
{
"short_code": "aZ3kT9",
"short_url": "http://127.0.0.1:5000/aZ3kT9",
"original_url": "https://www.example.com/some/really/long/path?x=1"
}- User accounts, so people can manage their own links
- Expiry dates for links
- QR code generation for each short URL
- Better analytics (right now it only counts total clicks, nothing about when or from where)
- Deploying it somewhere so it's not just localhost
This was mainly a learning project to get comfortable with building a small backend from scratch — routing, a database, some basic validation, and a minimal frontend to tie it together. Feedback welcome if you spot something that could be done better.