A simple URL shortening service like bit.ly or tinyurl.com, built using Python and Flask.
- Shorten long URLs to a short 6-character alphanumeric code.
- Redirect using the short code.
- Track and view analytics (click count, original URL, creation timestamp).
- In-memory storage (no database).
- REST API with JSON responses.
- Python 3.8+
pip(Python package manager)- 3 hours of focused development time (for challenge)
- Clone the repository
git clone https://github.com/Nikhilks2002/URL-Shortner.git cd url-shortener
``
-
Install dependencies
pip install -r requirements.txt
In a new terminal window:
$env:FLASK_APP = "app.main"
python -m flask runexport FLASK_APP=app.main
python -m flask runYou should see:
Running on http://127.0.0.1:5000
In a separate terminal while the Flask server is running:
pytestAlternatively, if you have a custom test file (e.g., test_api.py):
python test_api.py✅ Make sure the Flask server is running before you run tests — otherwise requests will fail with connection errors.
curl -X POST http://localhost:5000/api/shorten \
-H "Content-Type: application/json" \
-d '{"url": "https://www.example.com/very/long/url"}'Response:
{
"short_code": "abc123",
"short_url": "http://localhost:5000/abc123"
}curl -L http://localhost:5000/abc123curl http://localhost:5000/api/stats/abc123Response:
{
"url": "https://www.example.com/very/long/url",
"clicks": 5,
"created_at": "2024-01-01T10:00:00"
}-
At least 5 tests cover:
- Shorten logic
- Redirection
- Analytics
- Error cases (e.g., invalid URL, missing short code)
- Concurrent behavior