Rates API
Open Source

Deployment

Deploy your own New Zealand interest rates API to Cloudflare Workers: prepare D1, set the Worker routes, build, and set GitHub Actions secrets.

Rates API deploys three Cloudflare Workers on www.ratesapi.nz:

WorkerAppServes
ratesapi-nzapps/apiThe API routes: /api/v1/*, /openapi, and /openapi/*
ratesapi-nz-docsapps/docsThe Fumadocs site at /docs and /docs/*
ratesapi-nz-webapps/webThe landing page, and all other paths

Cloudflare D1 keeps the newest datasets and the historical snapshots for the API Worker.

Prerequisites

  1. A Cloudflare account
  2. The project, prepared for local development
  3. Bun
  4. The Wrangler CLI for Cloudflare Workers and D1

Cloudflare D1

Do these steps before you deploy the API Worker for the first time.

  1. Log in to Cloudflare:

    wrangler login
  2. Go to the API app:

    cd apps/api
  3. Add the D1 database to your Cloudflare account:

    wrangler d1 create ratesapi-data
  4. Copy the database ID from the output. Write it in the two d1_databases blocks in apps/api/wrangler.toml.

  5. Add the tables to the remote database:

    wrangler d1 execute ratesapi-data --remote --file=schema.sql

CAUTION: Without --remote, Wrangler changes only the local database on your computer.

Worker Configuration

The landing page Worker is the origin for www.ratesapi.nz and the apex domain ratesapi.nz. The configuration is in apps/web/wrangler.toml:

[[routes]]
pattern = "www.ratesapi.nz"
custom_domain = true

[[routes]]
pattern = "ratesapi.nz"
custom_domain = true

The API Worker and the docs Worker use route patterns. Cloudflare runs a route Worker before the Worker of a Custom Domain. Thus these Workers get their paths before the landing page Worker.

The configuration of the docs Worker is in apps/docs/wrangler.toml. Next.js serves the docs with basePath: "/docs":

[[routes]]
pattern = "www.ratesapi.nz/docs*"
zone_name = "ratesapi.nz"

The * at the end of the pattern matches /docs, /docs with a query string, and all paths under /docs/. A pattern without * does not match a URL that has a query string.

The docs Worker serves the prerendered pages from Workers static assets. It does not render the pages again for each request. The configuration of this cache is in apps/docs/open-next.config.ts.

The configuration of the API Worker is in apps/api/wrangler.toml. The API Worker controls the API paths on www.ratesapi.nz and on the apex domain:

[[routes]]
pattern = "www.ratesapi.nz/api/v1/*"
zone_name = "ratesapi.nz"

[[routes]]
pattern = "ratesapi.nz/api/v1/*"
zone_name = "ratesapi.nz"

[[routes]]
pattern = "ratesapi.nz/openapi"
zone_name = "ratesapi.nz"

[[routes]]
pattern = "ratesapi.nz/openapi/*"
zone_name = "ratesapi.nz"

The file has the same /openapi routes for www.ratesapi.nz.

Apex Redirect

apps/web/src/server.ts sends a 301 redirect from each apex request to the same path on www.ratesapi.nz. The API routes on the apex domain do not get this redirect, because the API Worker answers them first. Thus the API clients that use https://ratesapi.nz continue to operate.

The landing page Worker also sends the old docs paths (/api-reference/*, /open-source/*, and /llms.txt) to the same paths under /docs.

Build

Build each app in the same way as the CI build job:

bun run build

The API build is a Wrangler dry run. The docs build makes the OpenNext Worker bundle. The web build uses the Cloudflare Vite plugin to make the landing page Worker bundle.

Deploy

Deploy the three Workers:

bun run deploy

To deploy one Worker only, use the name of the app:

bun run --filter api deploy
bun run --filter docs deploy
bun run --filter web deploy

GitHub Actions

WorkflowStartsSecrets
Deploy to CloudflareOn each push to main, or manuallyCLOUDFLARE_API_TOKEN
Hourly Scraping and Database UpdatesEach hour, on each push to main, or manuallyCLOUDFLARE_API_TOKEN and CLOUDFLARE_ACCOUNT_ID

Each workflow runs bun run check before it deploys or collects data.

After Deployment

  • Make sure that https://www.ratesapi.nz/ shows the landing page.
  • Make sure that https://www.ratesapi.nz/docs shows the docs app.
  • Make sure that https://ratesapi.nz/ gives a 301 redirect to https://www.ratesapi.nz/.
  • Make sure that https://www.ratesapi.nz/api/v1/health and https://ratesapi.nz/api/v1/health give a response from the API Worker.
  • Make sure that https://www.ratesapi.nz/openapi shows the Scalar API client.
  • Make sure that https://www.ratesapi.nz/openapi/json gives the OpenAPI document.
  • Make sure that the repository has the secrets that the workflows use.

Production Rules

  • Keep the route control clear: the landing page Worker controls the Custom Domains, the docs Worker controls /docs, and the API Worker controls the API paths.
  • When you change Worker bindings or route patterns, examine the CI build job.
  • Keep the OpenAPI descriptions and schemas aligned with the routes.
  • Monitor scraper failures separately from Worker uptime.

Last updated on

On this page