A Flask backend project for shortening long URLs, redirecting visitors through short codes, and tracking how many times each short URL is used.
This project was built for the CodeAlpha Backend Development Internship.
Ahmed Wael Salah Mohammed AlSheikh
- Create short URLs from valid
httporhttpslinks. - Redirect short codes to the original URLs.
- Track
click_countevery time a short URL is visited. - Use the optional browser frontend to shorten URLs from a simple page.
- Return clean JSON error responses for invalid input and missing short codes.
- Persist data with SQLite.
- Include pytest coverage for the API.
- Python
- Flask
- Flask-SQLAlchemy
- SQLite
- python-dotenv
- pytest
CodeAlpha_URLShortener/
├── app/
│ ├── __init__.py
│ ├── extensions.py
│ ├── models.py
│ ├── routes.py
│ └── templates/
│ └── index.html
├── tests/
│ ├── conftest.py
│ └── test_routes.py
├── run.py
├── requirements.txt
├── pytest.ini
├── .gitignore
├── AGENTS.md
└── README.md
Create a fresh virtual environment:
python -m venv .venvActivate the virtual environment on Linux or macOS:
source .venv/bin/activateActivate the virtual environment on Windows PowerShell:
.venv\Scripts\Activate.ps1Install dependencies:
.venv/bin/python -m pip install -r requirements.txtThis repository already uses SQLite by default. The database file is created at:
instance/urlshortener.sqlite
You can override the database with a .env file:
DATABASE_URL=sqlite:///instance/urlshortener.sqlite
python run.py is intended for local development only. Debug mode is off by default.
To enable Flask debug mode locally, set FLASK_DEBUG=true.
.venv/bin/python run.pyOptional debug mode:
FLASK_DEBUG=true .venv/bin/python run.pyThe local API will be available at:
http://127.0.0.1:5000
The optional browser frontend is available at:
http://127.0.0.1:5000/
Open that page, enter a long http or https URL, and click Shorten to see the generated short URL.
After installing dependencies, run the test suite with:
.venv/bin/python -m pytestIf your virtual environment is missing test dependencies, reinstall them from the pinned requirements file:
.venv/bin/python -m pip install -r requirements.txtGET /api/healthExample request:
curl http://127.0.0.1:5000/api/healthExpected response:
{
"status": "ok"
}POST /api/shortenRequest body:
{
"url": "https://example.com"
}Example request:
curl -X POST http://127.0.0.1:5000/api/shorten \
-H "Content-Type: application/json" \
-d '{"url": "https://example.com"}'Expected response:
{
"original_url": "https://example.com",
"short_code": "abc123",
"short_url": "http://127.0.0.1:5000/abc123"
}Example request:
curl -X POST http://127.0.0.1:5000/api/shorten \
-H "Content-Type: application/json" \
-d '{}'Expected response:
{
"error": "A valid http or https URL is required."
}Example request:
curl -X POST http://127.0.0.1:5000/api/shorten \
-H "Content-Type: application/json" \
-d '{"url": "not-a-url"}'Expected response:
{
"error": "A valid http or https URL is required."
}GET /<short_code>Example request:
curl -i http://127.0.0.1:5000/abc123Expected response:
HTTP/1.1 302 FOUND
Location: https://example.com
GET /api/stats/<short_code>Example request:
curl http://127.0.0.1:5000/api/stats/abc123Expected response:
{
"original_url": "https://example.com",
"short_code": "abc123",
"click_count": 1,
"created_at": "2026-06-29T10:00:00.000000"
}Example request:
curl http://127.0.0.1:5000/api/stats/invalidcodeExpected response:
{
"error": "short_code not found."
}Before submitting to CodeAlpha:
- Run
.venv/bin/python -m pytestand confirm all tests pass. - Start the development app with
.venv/bin/python run.py. - Manually test the API with the curl examples above.
- Commit the final project files.
- Push the repository to GitHub and submit the GitHub repository link.