Deployment
Learn how to deploy Rates API to Cloudflare Workers for production use.
Deploying to Cloudflare Workers
Rates API is designed to be deployed to Cloudflare Workers, a serverless platform that offers global distribution, high performance, and excellent scalability. The API also uses Cloudflare D1 for data storage. This guide will walk you through the process of deploying your own instance of Rates API.
Prerequisites
Before deploying, ensure you have:
- A Cloudflare account
- The project set up for local development (see Local Development)
- Bun installed on your machine
- Wrangler CLI for Cloudflare Workers and D1 management
Deployment Steps
1. Create a Cloudflare Worker
First, you need to create a new Worker in your Cloudflare dashboard:
- Log in to the Cloudflare dashboard
- Select your account
- Go to Workers & Pages > Create application
- Select Create Worker > Deploy
This will create a new Worker where you can deploy your code.
2. Set Up Cloudflare D1 Database
You’ll need to create a D1 database in your Cloudflare account:
- In the Cloudflare dashboard, go to Workers & Pages > D1
- Click Create database
- Give your database a name (e.g.,
ratesapi-data
) - Select a location close to your target audience
After creating the database, you’ll need to apply the migrations:
3. Configure Wrangler
The project is already configured to use Wrangler, Cloudflare’s deployment tool for Workers. The configuration is in the wrangler.toml
file.
You’ll need to update some settings in this file to match your Cloudflare account:
- Make sure the
name
field matches what you want to call your worker - Update the D1 database binding to point to your production database
- Adjust any other settings as needed for your specific deployment
Example wrangler.toml
configuration with D1:
4. Deploy the Worker
To deploy the worker, use the command:
This will build the project and deploy it to your Cloudflare Worker with the D1 database binding.
Post-Deployment Setup
Data Updates
The API requires regular updates to the financial rate data. You have several options to handle this:
- Manual Updates: Run the scraper scripts manually and re-deploy
- Scheduled Workflows: Set up GitHub Actions or another CI/CD service to run the scrapers and deploy on a schedule
- Cloudflare Cron Triggers: Configure a cron trigger to run data updates directly on the worker
Monitoring
The repository includes a monitoring system that uses GitHub Actions to check the health of your API endpoints every 15 minutes. To set this up:
- Make sure the repository has the required permissions for GitHub Actions
- The workflow will:
- Check all API endpoints
- Create an issue when endpoints are down
- Label issues as “incident” and “high-priority”
- Include detailed information about failed endpoints
Custom Domain
To use a custom domain for your API:
- In the Cloudflare dashboard, go to Workers & Pages
- Select your worker
- Go to Triggers > Custom Domains
- Add your domain and follow the DNS configuration instructions
Production Considerations
Caching
The API is configured with a 5-second cache control by default. You may want to adjust this based on your specific needs:
- Increase cache time for better performance
- Decrease cache time for more up-to-date data
Rate Limiting
Consider implementing rate limiting if you expect high traffic or want to prevent abuse.
Documentation
The production API redirects documentation requests to https://docs.ratesapi.nz
. Update this in the code to point to your own documentation site if needed.