Sitelet https://github.com/cloudflare/python-workers-examples/tree/main/url-shortener
Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

README.md

URL Shortener Example

Deploy to Cloudflare

A Python Worker that creates short URLs backed by Workers KV.

Configure Workers KV

Create a namespace, then replace the placeholder ID in wrangler.jsonc with the ID from the command output:

uv run pywrangler kv namespace create LINKS

Run locally

First ensure that uv is installed. Then run:

uv run pywrangler dev
curl -X POST http://localhost:8787/shorten \
  -H 'content-type: application/json' \
  -d '{"url":"https://example.com/path"}'

The response includes a code, short_url, and the original url. Request the returned short URL to receive a 302 redirect. Deploy with uv run pywrangler deploy.

API

Endpoint Description
POST /shorten Store an absolute HTTP(S) URL and return a short URL.
GET /<code> Redirect to the stored URL with status 302.