Skip to content

A simple URL shortener service built on top of Cloudflare Workers with analytics and custom URL support

License

Notifications You must be signed in to change notification settings

belphegor-s/url-shortener

Repository files navigation

URL Shortener API with Analytics

This is a simple URL shortener API with analytics and custom URL support. It provides endpoints to create short URLs, track usage, and retrieve analytics data.

Setup & Configuration

Prerequisites

1. Clone the repository

git clone https://github.com/belphegor-s/url-shortner
cd url-shortner

2. Install Dependencies

Install required dependencies using npm:

npm install

3. Configure Environment Variables

Replace D1 SQL database_id in ./wrangler.jsonc:

"d1_databases": [
    {
        "binding": "DB",
        "database_name": "url_shortener_db",
        "database_id": "<your_d1_database_id>"
    }
]

4. Deploy to Cloudflare Workers

Use the wrangler CLI to deploy the application to Cloudflare Workers:

  1. Install Wrangler if you haven't already:
npm install -g wrangler
  1. Authenticate with Cloudflare:
wrangler login
  1. Deploy the worker:
wrangler deploy

This will deploy the application to Cloudflare Workers, and your API will be accessible at the provided URL.

5. API Endpoints

  • GET /create: Create a short URL from an original URL using query parameters.

    Query Parameters:

    ?url=https://example.com&custom_id=mycustomid # `custom_id` is optional

    Response:

    {
    	"short_url": "https://<your-worker-url>/<short_id>",
    	"existing": true // Indicates if the URL already existed in the database
    }
  • GET /analytics: Fetch analytics for all short URLs.

    Query Parameters:

    • page (optional): The page number for pagination.
    • limit (optional): The number of results per page (default: 50, max: 500).
    • sort (optional): Sort order (asc or desc, default: desc).
  • DELETE /analytics: Delete analytics and short URLs for the provided ids (short_id[]).

    JSON Body:

    {
    	"ids": ["abc123", "xyz456"]
    }
  • GET /analytics/:id: Fetch analytics for a specific short URL ID.

  • GET /:id: Redirect to the original URL for a given short URL ID. This also logs analytics such as IP address, user-agent, country code, and referrer.

6. Swagger UI Documentation

Access the API documentation Here or locally at:

/  # root route

This will provide an interactive UI to explore and test the API endpoints.

7. License

This project is licensed under the MIT License - see the LICENSE file for details.

About

A simple URL shortener service built on top of Cloudflare Workers with analytics and custom URL support

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published