Welcome to the URL Shortener API. This API allows you to shorten URLs, redirect to the original URLs, manage shortened URLs, and delete them.
-
Clone the repository:
git clone https://github.com/yourusername/url-shortener-api.git cd url-shortener-api
-
Create a virtual environment and activate it:
python -m venv env source env/bin/activate # On Windows use `env\Scripts\activate`
-
Install the dependencies:
pip install -r requirements.txt
-
Set up a PostgreSQL database name "url-shortener"
To run the application, use the following command:
uvicorn main:app --reload
This will start the FastAPI server on http://127.0.0.1:8000
.
- Endpoint:
/url
- Method:
POST
- Request Body:
{ "target_url": "https://example.com" }
- Response:
{ "url": "http://127.0.0.1:8000/<short_url_key>", "admin_url": "http://127.0.0.1:8000/admin/<secret_key>" }
- Description: Creates a shortened URL and returns the shortened URL and an admin URL for managing the short URL.
- Endpoint:
/{url_key}
- Method:
GET
- Response: Redirects to the original URL.
- Description: Redirects to the original URL using the shortened URL key.
- Endpoint:
/admin/{secret_key}
- Method:
GET
- Response:
{ "target_url": "https://example.com", "clicks": 10 }
- Description: Retrieves information about the shortened URL, including the original URL and the number of clicks.
- Endpoint:
/admin/{secret_key}
- Method:
DELETE
- Response:
{ "detail": "URL deleted successfully" }
- Description: Deletes the shortened URL using the admin secret key.
The database is set up using SQLAlchemy. The database URL should be configured in the config.py
file and .env
(You need to create this one). Here's a quick overview of the database setup:
-
Database Configuration:
- Configure your database URL in the
config.py
file. - Configure your database URL in the
.env
file.
- Configure your database URL in the
-
Models:
- The database model is already defined in the
models.py
file.
- The database model is already defined in the
-
CRUD Operations:
- Implement the necessary CRUD operations in the
crud.py
file.
- Implement the necessary CRUD operations in the