A beginner-friendly URL shortening service built with Flask and SQLite.
- Provides a web endpoint to shorten long URLs into short codes.
- Redirects short codes back to the original URL.
- Stores links in a local SQLite database.
app.py— the Flask application and API routes.database.py— database setup and helper functions.requirements.txt— Python dependencies for the project.
- Install dependencies:
pip install -r requirements.txt- Run the application:
python app.py- Open the app in your browser or use an API tool like Postman.
GET /— confirms the server is running.POST /shorten— accepts JSON with aurlfield and returns a short code.GET /<code>— redirects the browser to the original URL.GET /links— returns all saved short links.
Request:
curl -X POST http://127.0.0.1:5000/shorten \
-H "Content-Type: application/json" \
-d '{"url": "https://example.com"}'Response:
{ "code": "a1B2c3" }Then open:
http://127.0.0.1:5000/a1B2c3requirements.txthas the libraries need.app.pystarts the web server and handles requests.database.pykeeps the data safe inlinks.db.