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:
| Worker | App | Serves |
|---|---|---|
ratesapi-nz | apps/api | The API routes: /api/v1/*, /openapi, and /openapi/* |
ratesapi-nz-docs | apps/docs | The Fumadocs site at /docs and /docs/* |
ratesapi-nz-web | apps/web | The landing page, and all other paths |
Cloudflare D1 keeps the newest datasets and the historical snapshots for the API Worker.
Prerequisites
- A Cloudflare account
- The project, prepared for local development
- Bun
- The Wrangler CLI for Cloudflare Workers and D1
Cloudflare D1
Do these steps before you deploy the API Worker for the first time.
-
Log in to Cloudflare:
wrangler login -
Go to the API app:
cd apps/api -
Add the D1 database to your Cloudflare account:
wrangler d1 create ratesapi-data -
Copy the database ID from the output. Write it in the two
d1_databasesblocks inapps/api/wrangler.toml. -
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
| Workflow | Starts | Secrets |
|---|---|---|
| Deploy to Cloudflare | On each push to main, or manually | CLOUDFLARE_API_TOKEN |
| Hourly Scraping and Database Updates | Each hour, on each push to main, or manually | CLOUDFLARE_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/docsshows the docs app. - Make sure that
https://ratesapi.nz/gives a301redirect tohttps://www.ratesapi.nz/. - Make sure that
https://www.ratesapi.nz/api/v1/healthandhttps://ratesapi.nz/api/v1/healthgive a response from the API Worker. - Make sure that
https://www.ratesapi.nz/openapishows the Scalar API client. - Make sure that
https://www.ratesapi.nz/openapi/jsongives 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
Local Development
Clone Rates API, install it with Bun, start the API, docs, and landing page, and load real New Zealand rate data into a local D1 database.
Monitoring
How GitHub Actions check Rates API uptime every 15 minutes, monitor the hourly collection of New Zealand rates, and open issues for failures.