Deploy Your URL Shortener to Fly.io in Minutes
Running your own URL shortener is great, but you need somewhere to host it. Fly.io is an excellent choice - it’s fast, affordable, and their free tier is generous enough to run a production URL shortener at no cost.
In this guide, we’ll walk through deploying the URL Shortener API to Fly.io with persistent storage, custom domains, and monitoring capabilities.
Why Fly.io?
Fly.io stands out for several reasons:
Global Edge Network
Your app runs close to your users with data centers worldwide. This means faster redirects and better user experience.
Generous Free Tier
The free tier includes 3 VMs with 256MB RAM each, 3GB of persistent storage, and 160GB of outbound data transfer. More than enough for most URL shortener deployments.
Simple Deployment
Deploy with a single command. No complex configuration, no YAML nightmares, just straightforward deployment.
Built for Docker
The URL Shortener API comes as a ready-to-use Docker image. Fly.io’s Docker-first approach makes deployment seamless.
Prerequisites
Before we begin, you’ll need:
- A Fly.io account (free signup at fly.io)
- Flyctl CLI installed on your machine
- A valid URL Shortener API license key
Step 1: Install and Authenticate Flyctl
First, install the Fly.io CLI:
$ # macOS/Linux curl -L https://fly.io/install.sh | sh$ # Windows (PowerShell) pwsh -Command “iwr https://fly.io/install.ps1 -useb | iex”
$ # Authenticate fly auth login
The authentication command will open your browser to complete the login process.
Step 2: Create Your App
Choose a unique name for your app or let Fly.io generate one:
$ fly apps create your-app-name
$ # Or let Fly.io generate a name fly apps create
Your app will be accessible at https://your-app-name.fly.dev.
Step 3: Configure Your Deployment
Download the fly.toml configuration file from the url-shortener-api repository. This file defines your app’s configuration.
Key settings to customize:
app = "your-app-name"
primary_region = "iad"
Available regions:
iad- Virginia, USA (East Coast)lax- California, USA (West Coast)fra- Frankfurt, Germanysyd- Sydney, Australiasin- Singapore
Choose the region closest to your users for the best performance.
Step 4: Set Your License Key
Store your license key securely as a secret:
$ fly secrets set LICENSE_KEY=your-actual-license-key
Secrets are encrypted and never exposed in logs or configuration files.
Step 5: Create Persistent Storage
Your URL data needs to persist across deployments. Create a volume:
$ fly volumes create url_shortener_data \
--region iad \
--size 1
Important notes about volumes:
- The volume name must match the
sourceinfly.toml(default:url_shortener_data) - The region must match your app’s
primary_region - Size is in GB (1 GB minimum, sufficient for thousands of URLs)
- You can increase size later but cannot decrease it
Step 6: Deploy!
Now for the moment of truth:
$ fly deploy
Fly.io will pull the Docker image, configure your app, mount the persistent volume at /data, and start your service. The whole process takes about 30 seconds.
Step 7: Get Your API Key
Your URL shortener generates a random API key on first startup. Retrieve it from the logs:
$ fly logs
Look for output showing the generated API key. Save this securely - you’ll need it for all API requests.
Step 8: Test Your Deployment
Verify everything is working:
$ # Check status
fly status
$ # Create a short URL
curl -X POST https://your-app-name.fly.dev/shorten \
-H "X-API-Key: YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{"url": "https://example.com"}'
$ # Test the redirect
curl -L https://your-app-name.fly.dev/abc12345
If you see a successful redirect, congratulations! Your URL shortener is live.
Advanced Configuration
Enable Click Tracking
Track analytics for your shortened URLs:
$ fly secrets set CLICK_TRACKING_PATH=/data/clicks.jsonl
Note: Review GDPR and privacy considerations before enabling tracking in production.
Add a Custom Domain
Brand your short URLs with your own domain:
$ fly certs add yourdomain.com
Follow the instructions to add the required DNS records (A and AAAA) to your domain registrar. Fly.io will automatically provision SSL certificates.
Scale Your Resources
Need more power? Scale vertically or horizontally:
$ # Increase memory fly scale memory 512$ # Add more instances fly scale count 2
$ # Upgrade to dedicated CPU fly scale vm dedicated-cpu-1x
The default configuration (256MB, shared CPU) handles thousands of redirects per day comfortably.
Backup and Monitoring
Create Backups
Back up your URL data regularly:
$ fly ssh console -C "tar -czf /tmp/backup.tar.gz /data"
fly ssh sftp get /tmp/backup.tar.gz ./backup.tar.gz
Consider setting up a cron job to automate this process.
Monitor Logs
Keep an eye on your app with real-time logs:
$ # Follow live logs fly logs -f
$ # View recent logs fly logs —recent
Troubleshooting
App Not Starting?
Check these common issues:
- Verify your license key:
fly secrets list - Check the logs:
fly logs - Confirm volume is mounted:
fly volumes list
Volume Not Mounting?
Ensure the volume name in your fly.toml matches the volume you created, and that both are in the same region.
Need More Storage?
Expand your volume (you can increase but not decrease):
$ fly volumes list
fly volumes extend <volume-id> --size 5
Cost Optimization
The default configuration fits comfortably within Fly.io’s free tier:
- 3 shared-cpu-1x VMs (256MB RAM each)
- 3GB persistent storage
- 160GB outbound data transfer
To minimize costs:
- Use the
auto_stop_machines = truesetting (enabled by default in the providedfly.toml) - Start with minimal resources and scale as needed
- Use shared CPUs instead of dedicated ones
- Monitor your usage via
fly dashboard
For most use cases, the free tier is more than sufficient for production workloads.
Conclusion
Deploying your URL shortener to Fly.io gives you a globally distributed, fast, and reliable service with minimal effort. The combination of Docker-based deployment, persistent storage, and automatic SSL makes Fly.io an excellent choice for hosting your URL shortener.
The free tier is generous enough for serious production use, and you can scale up seamlessly as your needs grow. Plus, with custom domains and click tracking, you have all the features of commercial URL shorteners while maintaining complete control over your data.
Ready to deploy? Follow this guide and you’ll have your URL shortener running in production in less than 10 minutes!
For more information, check out the URL Shortener API documentation and Fly.io documentation.