Sink: Run Your Own Serverless Link Shortener on Cloudflare
Sink is a powerful, open-source link shortener that you can deploy entirely on Cloudflare’s serverless platform.
This means no traditional servers to manage, scaling is automatic, and costs are typically low.
A Simple / Speedy / Secure Link Shortener with Analytics, 100% run on Cloudflare.
This post will guide you through setting up and running your own instance of Sink.
What is Sink and Why Cloudflare?
Sink offers a simple, speedy, and secure way to shorten URLs, complete with analytics.
What makes Sink particularly interesting is its serverless architecture.
It leverages Cloudflare’s suite of services:
- Cloudflare Pages: Hosts the frontend application (built with Nuxt.js). Pages automatically builds and deploys your site whenever you push changes to your Git repository.
- Cloudflare Workers KV: Acts as the database, storing your shortened links and associated data. KV is a fast, globally distributed key-value store.
- Cloudflare Workers Analytics Engine: Powers the analytics dashboard, providing insights into link usage.
- Cloudflare Workers (Optional): Can be used for custom logic or integrations, although the core functionality doesn’t require Workers directly.
By using these Cloudflare services, Sink eliminates the need for you to manage servers. Everything runs on Cloudflare’s infrastructure, providing scalability, reliability, and cost-effectiveness.
Is Sink Self-Hosted?
Yes, in a way.
While you don’t manage traditional servers, you are deploying and controlling your own instance of Sink.
The “hosting” happens on Cloudflare’s platform, but you are responsible for the setup, configuration, and any code modifications.
It’s a “serverless” self-hosted solution.
Setting Up Sink on Cloudflare
Here’s a step-by-step guide to get Sink running:
-
Fork the Repository: Go to the Sink GitHub repository and fork it to your own GitHub account.
-
Create a Cloudflare Pages Project:
- Log in to your Cloudflare account.
- Navigate to “Pages” and create a new project.
- Connect your forked GitHub repository.
- Choose the
Nuxt.js
preset.
-
Configure Environment Variables: These variables are essential for Sink to function correctly. In your Cloudflare Pages project settings:
NUXT_SITE_TOKEN
: A secret token (at least 8 characters long) to access your Sink dashboard. Keep this secure!NUXT_CF_ACCOUNT_ID
: Your Cloudflare account ID. You can find this in your Cloudflare dashboard. Go to “Overview” and it’s on the right.NUXT_CF_API_TOKEN
: Create a Cloudflare API token with the following permissions:Account.Account Analytics
(required)- Potentially others if using Workers or other Cloudflare services.
-
Set Up Bindings: Bindings connect your Pages project to the necessary Cloudflare services. In your Cloudflare Pages project settings, go to “Bindings” -> “Add”:
- KV Namespace:
- Create a new KV namespace in “Storage & Databases” -> “KV”.
- Bind the variable name
KV
to this namespace.
- Workers AI (Optional): If you want to use the AI slug generation feature:
- Bind the variable name
AI
to your Workers AI Catalog.
- Bind the variable name
- Analytics Engine:
- Enable the free version of Analytics Engine in “Workers & Pages” -> “Account details”.
- Bind the variable name
ANALYTICS
to thesink
dataset.
- KV Namespace:
-
Deploy and Redeploy: Save your Pages project settings and trigger a deployment. After the initial deployment, you might need to cancel it and redeploy to apply the bindings correctly.
-
Access Your Sink Dashboard: Once deployed, you can access your Sink dashboard at the URL provided by Cloudflare Pages (it will look something like
your-project-name.pages.dev
). Use theNUXT_SITE_TOKEN
you set earlier to log in.
Updating Sink
To update Sink, simply pull the latest changes from the original repository into your forked repository and then push those changes to your GitHub repo connected to Cloudflare Pages.
Cloudflare Pages will automatically rebuild and redeploy your site.
Key Concepts Revisited
- Serverless: No servers for you to manage. Cloudflare handles the infrastructure.
- Cloudflare Ecosystem: Sink relies heavily on Cloudflare’s services (Pages, KV, Analytics Engine).
- Self-Hosted (in a sense): You control the deployment and configuration, but the hosting happens on Cloudflare.
Enjoy your new serverless link shortener!