Shortener

Privacy-first URL shortener with AI-generated slugs, edge-cached redirects, and click analytics. Runs entirely on Cloudflare Workers.

Active links0
Version2.0.0
D1 connected
Analytics on
AI slug on

How it works

POST /api/url creates a short link backed by Drizzle on D1. Redirects through GET /:shortCode hit a 1-hour KV cache before falling through to the database. Every redirect emits an event to Cloudflare Analytics Engine.

Quick start

Admin endpoints under /api/* require an ES256 JWT in Authorization: Bearer <token>. Create a link:

curl -X POST https://shortener.cdlab.workers.dev/api/url \
  -H "Authorization: Bearer $JWT" \
  -H "Content-Type: application/json" \
  -d '{
    "records": [
      { "url": "https://example.com/very/long/path" }
    ]
  }'

Response:

{
  "code": 0,
  "message": "ok",
  "data": {
    "successes": [
      {
        "success": true,
        "hash": "<sha256-hex>",
        "shortCode": "ab12CDef",
        "shortUrl": "https://shortener.cdlab.workers.dev/ab12CDef",
        "url": "https://example.com/very/long/path",
        "expiresAt": 1730000000000
      }
    ],
    "failures": []
  }
}

Then anyone can follow the short link:

curl -I https://shortener.cdlab.workers.dev/ab12CDef
# HTTP/2 302
# location: https://example.com/very/long/path

API endpoints

GET/:shortCode

Public 302 redirect. Social-media crawlers are sent to /:shortCode/og for preview rendering.

GET/health

JSON service health check (DB + Analytics readiness).

POST/api/url

Batch create up to 100 links. Each record may include an optional custom hash, userId, expiresAt (epoch ms), or attribute blob.

PUT/api/url

Batch update up to 100 links by hash; clears the redirect cache.

DELETE/api/url

Batch soft-delete up to 100 links; clears both redirect and OG caches.

GET/api/ai/slug?url=...

Generate a memorable slug via Workers AI (cached 7d in KV). Also: POST /api/ai/batch-slug, GET /api/ai/suggestions.

GET/api/analytics/overview

Aggregate clicks/visitors across all links. Sibling endpoints: /timeseries, /top-countries, /top-referrers, /devices, /browsers, /operating-systems, /link/:hash, /real-time.

Create-link parameters

urlTarget URL (required)--
hashCustom short code; alphanumeric + -_auto
expiresAtExpiration epoch (ms)+1h
userIdOwner identifierempty
attributeArbitrary blob payloadnull