Getting Started

Getting Started

To deploy to your Cloudflare Workers account from within the ./link-shortener directory, run the following command:

wrangler deploy

Configuration Options

Set through environment variables, there are several configuration options to make Link Shortener work for your use case.

  • API_KEY - The API key used to authenticate requests to the public/external API routes.
  • MAX_TTL - The maximum time to live for a link, in seconds. Defaults to 10 years. Minimum is 60 seconds (exclusive) for any link which is a platform limitation.
  • ROOT_REDIRECT - The URL to redirect to when the root path is requested. Defaults to https://mackenly.com. If left blank, it will redirect to the /dashboard page.
  • SLUG_LENGTH - The length of the slug to generate for each link. Defaults to 6. If being used at scale, you might should consider increasing this, but the good thing is that you can change it at any time and it will still work for existing links. Collision Pobability Calculator (opens in a new tab)

Example configuration:

vars = { API_KEY = "super-secure-password", MAX_TTL = "315360000", ROOT_REDIRECT = "https://mackenly.com", SLUG_LENGTH = "6" }

Bindings

The database for this tool is Cloudflare KV which is included in Workers free and paid plan. You can create a KV store with the Wrangler CLI (opens in a new tab) or via the Dashboard (opens in a new tab).

In your wrangler.toml file, you can add the following to bind your KV store to the LINK_SHORTENER namespace:

kv_namespaces = [
    { binding = "LINK_SHORTENER", id = "yourkey", preview_id = "yourkey" }
]

Custom Domain

You'll need to edit the route(s) in the wrangler.toml file to match your custom domain (opens in a new tab).

Alternatively, you can remove the routes section from your wrangler.toml and remove the line that says workers_dev = false. This will allow you to use your project.example.workers.dev URL.