The API client
How the frontend consumes the backend REST API through the generated chowbea-axios client and the Result pattern.
The school website reads two different backends, and a third store for editorial content. CMS content comes from Sanity. Enquiries and the public chat are fetch calls to the schools API: POST /api/v1/enquiry, POST /api/v1/public-ai/chat, and GET /api/v1/public-ai/config, with header x-api-key (submitEnquiry and createNaalyaChat). A generated Axios client also lives in app/services/api/. On the audited commit its committed spec only contains seven operations, including submitWebsiteForm and unipileWebhook. The running form and chat code does not call those operations. Treat openapi.json as stale until you regenerate it from a spec you trust.
Do not send an admission form through the generated client
AdmissionForm calls sendEnquiry and then createFormSubmission. The enquiry goes to {VITE_API_URL}/api/v1/enquiry. The Sanity copy goes to this site's POST /api/form-submission, which uses the server-side SANITY_WRITE_TOKEN. createWebsiteForm is not that path. The codegen endpoint in api.config.toml is https://tunnel.chowbea.com/docs/swagger/json. That is the watch URL, not proof of the production API host. See Environments and HTTP surface.
Here's the core idea: you never hand-write the types or methods for that API. The backend publishes a live OpenAPI (Swagger) spec, and a CLI called chowbea-axios reads it and generates a fully type-safe axios client into app/services/api/. New endpoint on the backend? Regenerate, and your editor immediately knows its path, its params, and the exact shape of what comes back. The types are a mirror of the backend, not a guess you maintain by hand.
Each client has one job. app/lib/sanity-client.ts reads CMS content with GROQ. app/services/api/ is a generated REST client. Public enquiries and chat do not go through that client: they use fetch in app/lib/naalya-chat/. To change a blog post, use Sanity (Querying with GROQ). To change the admission form's submit path, read useSubmitEnquiry and app/routes/api/form-submission.ts.
The mental model: spec in, typed client out
The whole workflow is one loop. The backend serves a spec at https://tunnel.chowbea.com/docs/swagger/json. The CLI fetches it, hashes the body against a local cache, and, only if the hash changed, regenerates the client. That hashing is the quiet hero: it means the watcher can poll every ten seconds without thrashing your files. Nothing changes on disk until the contract actually changes.
The generated calls are Result-based: every method returns { data, error } and never throws. No try/catch around every fetch, no surprise exceptions bubbling out of a loader. You check error, or you use data, and TypeScript narrows the type for you on each branch. (The full Result ergonomics, error codes, normalization, type guards, live in the generated api.error.ts and its type-guard helpers.)
Configuration lives in api.config.toml
One file at the repo root tells the CLI where the spec is, where to write the client, and how the axios instance should behave. It's short on purpose.
api_endpoint = "https://tunnel.chowbea.com/docs/swagger/json"
poll_interval_ms = 10000
[output]
folder = "app/services/api"
[instance]
base_url_env = "VITE_API_URL"
token_key = "auth-token"
with_credentials = true
timeout = 30000Two lines deserve a callout. poll_interval_ms = 10000 is the watcher's heartbeat: ten seconds. And base_url_env = "VITE_API_URL" is the bridge between this config and your .env: it tells the generated axios instance to read its baseURL from VITE_API_URL at runtime.
The client cannot run without VITE_API_URL. The generated api.instance.ts sets baseURL: import.meta.env.VITE_API_URL. If that variable is missing, every Axios request goes to undefined. Chat and enquiries fail in a different way: the widget renders nothing when the URL or the API key is missing. Set VITE_API_URL to the API you mean (http://localhost:8000 for a local server). Vite inlines VITE_* values at build time.
Generated files vs. files you own
This is the single most important thing to internalize before you touch app/services/api/. The folder is half robot, half yours, and the line between them is non-negotiable.
Everything under _generated/ is overwritten on every run: the headers literally say "DO NOT EDIT MANUALLY." The sibling wrapper files were generated once and then handed to you; their headers say "You can safely modify this file: it will NOT be overwritten." Custom logic, an interceptor tweak, a helper, a default header, goes in the wrappers. Never in _generated/.
| File | Owner | Edit it? |
|---|---|---|
_generated/api.types.ts | openapi-typescript | No: regenerated from the spec |
_generated/api.operations.ts | chowbea-axios | No: exposes api.op.<operationId>() |
api.client.ts | you (generated once) | Yes: the typed api object you import |
api.instance.ts | you (generated once) | Yes: axios instance + auth interceptor |
api.error.ts / api.helpers.ts | you (generated once) | Yes: error shaping + helpers |
If you edited _generated/, you've already lost it
The next spec change, or the next pnpm dev:all poll, silently overwrites anything under app/services/api/_generated/. There is no warning. Put your code in the sibling api.client.ts / api.instance.ts / api.error.ts / api.helpers.ts files, which the generator leaves alone.
Calling the API
You import the api object from api.client.ts when you are calling an operation that exists on the current spec. api.op.<operationId>() follows that spec. api.get and api.post take a path. On the audited commit the pages that visitors use do not call api.op.submitWebsiteForm. The snippet below shows the shape of a generated call. It is not the admission form.
const response = await api.op.getWebsiteForm({ id: "form_example" });
return response.data;getWebsiteForm is an operation id in the committed openapi.json. Confirm it still exists before you rely on it. The Result methods return { data, error } and do not throw. Check error before you read data.
app/queries/base.query.ts is a plain JavaScript singleton class that holds the shared api and logger. The Sanity pages use "singleton" for a different concept: documents with a fixed _id. For those, see Single documents and singletons.
The api:* scripts
The CLI is a global install (chowbea-axios, not a repo dependency), and the root package.json wraps it in api:* scripts so you never type raw CLI flags. You'll reach for api:status and api:watch daily; the rest are situational.
| Script | What it does |
|---|---|
pnpm api:status | Show config, cache state, endpoint counts, and which files are present. Run this first when something looks off. |
pnpm api:fetch | One-shot: pull the live spec and regenerate. |
pnpm api:generate | Regenerate from the cached spec: no network call. |
pnpm api:watch | Poll the endpoint every poll_interval_ms and regenerate on change. |
pnpm api:diff | Preview the delta between current and incoming spec before regenerating. |
pnpm api:validate | Validate the spec (--strict treats warnings as errors). |
The CLI is not in package.json, node_modules, or the lockfile. A fresh pnpm install does not provide it, so every api:* script fails with command not found until you install it globally. This command installs it. It was not run for this page.
pnpm add -g chowbea-axiosWhen do I actually regenerate?
Almost never, and that's by design: the generated files are committed. A fresh clone type-checks and boots immediately; you do not run codegen just to start working. You regenerate in exactly one situation: the backend spec changed, a new endpoint, a renamed field, a tweaked response shape.
When that happens, you have two paths. For a one-off, run pnpm api:diff to see what moved, then pnpm api:fetch to apply it. But the smoother path during active feature work is to let the watcher do it for you.
pnpm dev:all runs the API watcher and Vite side by side via concurrently, as two named processes:
pnpm dev:allapi=pnpm api:watch, pollinghttps://tunnel.chowbea.com/docs/swagger/jsonevery 10s.Vite=pnpm dev, the dev server onhttp://localhost:5173.
The moment the backend ships a spec change, the watcher rewrites app/services/api/_generated/*, Vite hot-reloads, and your types are current: no manual step, no stale client. If you're building a feature that touches a brand-new backend endpoint, dev:all is the command you want running.
pnpm dev starts only Vite, so it does not regenerate the client: the client stays as it is on disk. That is fine when the API contract does not change. When you expect backend changes, use pnpm dev:all so the watcher runs alongside Vite.
Where to go next
Getting started
Install the CLI, set VITE_API_URL, and run the dev servers end to end.
The stack
Where axios, the generated client, and every other dependency fit.
Troubleshooting and gotchas
The global-CLI requirement and other API-client traps.
Forms
The react-hook-form pattern. Submit goes to the enquiry API and a server route, not submitWebsiteForm.
The website chat assistant
What the chat widget calls, the API key and rate limits, and the event stream it reads.