URL shortener

Create a url shortener API

4 hours
medium
32 submissions

Project Overview

Create a url shortener API

This project is a basic URL shortening API built using the technology of your choice. It enables users to generate short URLs for long links, track the number of clicks on each short URL, and provides redirection to the original URL.

Key Features

  • URL Shortening: Generates a short URL identifier for any valid long URL.
  • Redirection: Redirects users from the short URL to the original long URL.
  • Click Tracking: Counts the number of times each short URL is accessed.
  • URL and Click Retrieval: Allows fetching the original URL and click count via an API.

Usage

  1. Create a Short URL: Send a long URL to generate a short version.
  2. Redirect: Access the short URL to be redirected to the original URL.
  3. Click Count: Retrieve click statistics for each short URL.

This project provides a foundational URL shortener service with the essential features and flexibility for potential expansion.

Your submissions

No submissions yet, start by making your first submission

Detailed Project Description

This project is a basic URL shortening API built using the technology of your choice. It enables users to generate short URLs for long links, track the number of clicks on each short URL, and provides redirection to the original URL.

Features

  • Shorten a URL: Takes a long URL and generates a unique, shorter URL.
  • Redirect to Original URL: Redirects users to the original URL when they access the short link, and increments a click count.
  • Track Clicks: Keeps track of the number of times a shortened URL is accessed.
  • Retrieve Original URL and Click Count: Provides endpoints to retrieve the original URL and the total click count for a given shortened URL.

Environment Variables

  • BASE_URL: The base URL for generating shortened links (e.g., http://localhost or a production URL).
  • PORT: Port on which the server will listen (defaults to 3033 if not specified).

API Endpoints

1. Shorten a URL

  • Endpoint: POST /shorten
  • Description: Shortens a given long URL and returns the shortened version.
  • Request Body:
    • url (string, required): The long URL to be shortened.
  • Response:
    • success:
      • Response status: 201 Created
      • Response body : {shortUrl: "example.com/shortened"}
    • failure:
      • Response status: 400 Bad Request
      • Returned if the URL is invalid.

2. Redirect to Original URL

  • Endpoint: GET /:shortId
  • Description: Redirects the user to the original URL associated with the given short ID and increments the click count.
  • Path Parameter:
    • shortId (string, required): The unique ID associated with the shortened URL.
  • Response:
    • 302 Found: Redirects to the original URL.
    • 404 Not Found: Returned if the short ID does not exist.

3. Retrieve Original URL and Click Count

  • Endpoint: GET /:shortId/stats
  • Description: Retrieves the original long URL and the click count for a given short ID.
  • Path Parameter:
    • shortId (string, required): The unique ID associated with the shortened URL.
  • Response:
    • success:
      • Response status: 200 OK
      • Response body : { longUrl: "www.example.com", clickCount: 1 }
    • failure:
      • Response status: 404 Not Found
      • Returned if the short ID does not exist.

Future Improvements

  • Database Integration: Replace in-memory storage with a database (e.g., MongoDB, PostgreSQL) for persistence.
  • Enhanced Analytics: Track additional metrics such as timestamps, referrer URLs, and unique clicks.
  • Custom URL Aliases: Allow users to specify custom aliases for their short URLs.
  • User Authentication: Enable user accounts to manage URLs and view personalized click analytics.

The server will run on the specified port or default to 3033.

Example .env File

BASE_URL=http://localhost:3033 PORT=3033

Notes

  • This project is currently configured with in-memory storage, meaning data will be lost when the server restarts.
  • For production deployment, integrate with a database and secure the server with HTTPS.

Project Completion Criteria

  • The system should allow users to shorten a valid long URL and return a shortened URL identifier.
  • The shortened URL generated should be unique and consistently structured according to the base URL.
  • The system should accurately redirect each shortened URL to the correct original URL with proper HTTP redirection (302 status).
  • The system should correctly track the number of clicks (or accesses) on each shortened URL.
  • The system should support creating and handling multiple shortened URLs, ensuring that each shortened URL accurately redirects to its respective original URL.
  • Each shortened URL should independently track click counts without interference from other URLs.
  • The system should validate URLs before attempting to shorten them and reject invalid URLs with appropriate feedback (e.g., a 400 error for invalid URL format).
  • The system should return a 404 error when an attempt is made to redirect a non-existent or invalid shortened URL.
  • Attempts to retrieve click counts or original URLs for non-existent short URLs should also return a 404 error, with an appropriate error message.