Serverless URL shortener based on Cloudflare Workers and Cloudflare Workers KV.
Grab your Worker URL and go to https://url-shortener.my-account.workers.dev/shorten
.
Tip
Use Cloudflare Access to protect the /shorten
route behind an Entra ID authentication.
POST /shorten
{
"url": "https://google.com"
}
{
"url": "https://google.com",
"alias": "hZk"
}
{
"status": "success",
"message": "Alias has been created.",
"alias": "hZk"
}
- How long are the shortened URLs?
- By default it's your root domain
example.com
+ a 3 character alias (example.com/aS3
). Shortening the same URL multiple times would result in the same alias. 3 characters would last for about 200k unique aliases if no hash collision occurs. After exceeding ~200k unique aliases, each new alias is a hash collision, resulting in the behavior described below.
- By default it's your root domain
- What happens in case of a hash collision?
- In case of a hash collision an additional character would be appended to your alias (
example.com/aS3q
). - Hash collisions increase the read operations on the KV namespace (+1 for each appended character), resulting in higher costs. If you are expecting many alias creations, increase
ALIAS_LENGTH
before going live.
- In case of a hash collision an additional character would be appended to your alias (
- Can I increase the length of my aliases?
- You can increase the alias length via the
ALIAS_LENGTH
variable. - Only increase
ALIAS_LENGTH
if your KV namespace is empty, otherwise duplicated aliases are being created.
- You can increase the alias length via the
- Are the aliases the same across all deployed workers?
- If you don't change the
SALT
variable, the aliases will be the same.
- If you don't change the
- Can I change the
/shorten
route?- Yes, the
/shorten
route can be overwritten in config.ts.
- Yes, the