A simple backend service that shortens URLs using FastAPI and SQLite.
- Shorten long URLs to compact, shareable links.
- Redirect visitors from short URLs to the original URLs.
- Track the number of times each short URL is accessed.
- Retrieve click statistics for each short URL.
- Language: Python
- Framework: FastAPI
- Database: SQLite (via SQLAlchemy ORM)
- Endpoint:
POST /shorten - Request Body:
{ "url": "<long_url>" } - Response:
{ "short_url": "<generated_short_url>" } - Description: Accepts a long URL and returns a shortened URL.
- Endpoint:
GET /{short_id} - Response: Redirects the request to the original URL.
- Description: When a user accesses a short URL, they are redirected to the original URL, and the click is counted.
- Endpoint:
GET /stats/{short_id} - Response:
{ "short_id": "<short_id>", "original_url": "<original_url>", "clicks": <number_of_clicks> } - Description: Returns the original URL and the number of times the short URL has been accessed.
- Endpoint:
GET / - Response:
{ "message": "Starting URL Shortener" } - Description: Basic health check or root endpoint.
- SQLite database file is created at
./shortener.db. - All URLs and click statistics are stored in the database.
-
Clone the repository:
git clone https://github.com/aigbee17/url-shortner.git cd url-shortner -
Install dependencies:
pip install fastapi uvicorn sqlalchemy pydantic
-
Run the server:
uvicorn app.main:app --reload
-
Access the API:
- Open http://localhost:8000 in your browser.
- Use an API client like Postman or cURL to interact with the endpoints.
app/main.py- Core FastAPI app and API endpoints.app/database.py- Database connection and model definitions.shortener.db- SQLite database (auto-generated).
No license specified.
For any issues or feature requests, please open an issue at https://github.com/aigbee17/url-shortner/issues.