cloudflare-worker-url-shortner is a serverless URL shortening service built using Cloudflare Workers. It provides a simple and efficient way to shorten long URLs and manage redirects.
Important
This project was developed using Vue out of consideration for its flexibility, reactivity, and robust ecosystem. If you prefer a more lightweight version, please refer to the lite branch.
- 😀 Simple and clean style
- 🗄️ Uses Cloudflare KV for persistent storage
- 🔗 Custom short URLs support
- 📡 Simple API for creating short links
- 🌐 A Cloudflare account
- 🗄️ Install Wrangler and login
npm install -g wrangler wrangler login
- Create KV namespace

- Remember KV namespace ID

- Clone this repo and
git clone https://github.com/coderxi1/cloudflare-worker-url-shortner
- Install dependencies
pnpm install
- Change
PASSWORDand and KV namespace 'sIDinwrangler.jsonc - Deploy to cloudflare
pnpm run deploy
| Key | Default Value | Description |
|---|---|---|
PASSWORD |
<YOUR_PASSWORD> |
The password for authentication. |
PASSWORD_SECRET_KEY |
<YOUR_SECRET_KEY> |
The secret key for jwt. |
PASSWORD_REQUIRE |
true |
If set to true, password must be entered to use all features. |
HOST_WHITELIST |
["example.com", "coderxi.com"] |
A list of allowed hostnames. Only requests from these domains are accepted unless a valid password is provided. |
KEY_MIN_LENGTH |
6 |
The minimum length required for the key. |
KEY_REMOVE |
false |
If set to true, the URL record will be deleted when expired. If false, the URL will only be marked as expired |
Request:
POST /
Content-Type: application/json
{
"url": "https://coderxi.com/posts/url-shortener-with-cloudflare-worker",
"password": "<YOUR_PASSWORD>",
"key": "doc",
"expireDay": 1
}Response:
{
"key": "doc"
}
{ "vars": { "PASSWORD": "<YOUR_PASSWORD>", //custom your password * "PASSWORD_SECRET_KEY": "<YOUR_SECRET_KEY>", //custom your secret key * }, "kv_namespaces": [ { "binding": "URLS", "id": "<YOUR_KV_NAMESPACE_ID>" //change to your KV namespace ID * } ] }