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:

  1. A Cloudflare account
  2. The project set up for local development (see Local Development)
  3. Bun installed on your machine
  4. 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:

  1. Log in to the Cloudflare dashboard
  2. Select your account
  3. Go to Workers & Pages > Create application
  4. 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:

  1. In the Cloudflare dashboard, go to Workers & Pages > D1
  2. Click Create database
  3. Give your database a name (e.g., ratesapi-data)
  4. Select a location close to your target audience

After creating the database, you’ll need to apply the migrations:

# Authenticate with Cloudflare (if not already done)
wrangler login

# Apply database schema to the production database
wrangler d1 execute ratesapi-data --file=./schema.sql

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:

name = "rates-api"
main = "src/index.ts"
compatibility_date = "2023-05-15"

# D1 Database binding
[[d1_databases]]
binding = "RATESAPI_DB"
database_name = "ratesapi-data"
database_id = "your-database-id-from-cloudflare"

4. Deploy the Worker

To deploy the worker, use the command:

bun run deploy

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:

  1. Manual Updates: Run the scraper scripts manually and re-deploy
  2. Scheduled Workflows: Set up GitHub Actions or another CI/CD service to run the scrapers and deploy on a schedule
  3. 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:

  1. Make sure the repository has the required permissions for GitHub Actions
  2. 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:

  1. In the Cloudflare dashboard, go to Workers & Pages
  2. Select your worker
  3. Go to Triggers > Custom Domains
  4. 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.