This is a worker script that uses Cloudflare Workers KV to store links and redirect to them.
It allows you to create a short link service, fully on Cloudflare workers to change long links into short ones. At Jammed we use it to create short links for our users in booking SMS reminders and emails. Because the worker is fully on Cloudflare, it's fast and reliable and we don't have to worry about using a third party service that might be taken offline, down or slow.
It's also free!
- Create a Cloudflare account
- Setup wrangler CLI
- Create a secrtet key for your worker, to only allow POST requests from you
wrangler secret put CREATE_SECRET_KEY
- Set up two production KV namespaces, and thwo development KV namespaces:
Add all the binding IDs to your wrangler.toml file in the
wrangler kv:namespace create "URLS" wrangler kv:namespace create "URLS" --preview wrangler kv:namespace create "URLS_ADDED" wrangler kv:namespace create "URLS_ADDED" --preview
kv_namespacesarray. - Add your Cloudflare zone ID to your wrangler.toml file, along with your chosen domain
- Then publish your worker:
wrangler publish
- Cloudflare should add the worker to your domain, and bind the DNS records to it for you
- POST to your worker with the
CREATE_SECRET_KEYheader and aurlparameter to create a new short link - GET the short link to be redirected to the original URL
Send a POST request to the endpoint with your secret key as bearer auth in the header. The short code is returned as text in the response body.
$ curl https://short.io
-H "Accept: application/text"
-H "Authorization: Bearer {CREATE_SECRET_KEY}"
-d "https://example.com"
d93i4qSend a GET request to the endpoint with the short code as the path.
$ curl -v https://short.io/d93i4q
...
< HTTP/2 302
< location: https://example.com
< server: cloudflare
...Sending a POST request with the same URL will return the same short code. This reverse mapping is stored in the URLS_ADDED KV namespace.
$ curl https://short.io
-H "Accept: application/text"
-H "Authorization: Bearer {CREATE_SECRET_KEY}"
-d "https://example.com"
d93i4q- Simple redirect service and POST endpoint to create new short links
- Simple html page for visitors to index of the service
- robots.txt
- Add Jest tests
- Add a way to delete links
- Add Cloudflare Analytics metadata to links, like expiry date, number of clicks, etc
- Add a way to create vanity links
MIT
Jammed.app online booking sofware for studios - 2022
Contributions are welcome! Please open an issue or PR.