published fresh until

OpenAPI: how do I stop my agent hand-writing fetch calls to my API?

Summary When generating a typed API client from FastAPI, tag each route in the OpenAPI spec, generate the client (for example with Hey API's openapi-ts), and regenerate on every backend change so the build can fail on drift. Never edit generated code by hand. Not tried by WhatWorks.

Not for choosing a generator; third-party API docs for agents; MCP from OpenAPI

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

The problem

Your backend publishes an OpenAPI spec, but the frontend agent writes fetch calls and response types by hand. Fields get renamed in the backend and nothing fails until runtime, or the agent invents a field that does not exist.

The pattern two sources agree on

Generate the client from the spec, treat it as machine-owned, and make regeneration the routine.

  • The FastAPI docs on generating clients use Hey API: npx @hey-api/openapi-ts -i ./openapi.json -o src/client (the page's first example points at http://localhost:8000/openapi.json). If you build after regenerating, a mismatch errors out and shows drift early.
  • A Redocly engineering post by Roman Marshevskyi (1 Sep 2026) argues for the same idea for agents: strict types make the compiler a fact-checker, the generated client is machine-owned and never hand-edited, the generator is human-owned, and the CLI should be version-pinned (its own tool is described as experimental). It is a vendor post about Redocly's generator; the principle is not specific to it.

Steps

1. Clean the spec first. The FastAPI page tags each route and sets generate_unique_id_function so operation ids look like items-get_items; for the client it strips the tag prefix so methods read getItems, not createItemItemsPost. Untagged routes break that function (it reads tags[0]), and renaming a backend function renames the client method. 2. Install pinned. Hey API's docs say the package is in initial development and tell you to pin an exact version (npm install @hey-api/openapi-ts -D -E). 3. Add one script, e.g. "generate-client": "curl -s http://localhost:8000/openapi.json -o openapi.json && ... openapi-ts -i ./openapi.json -o src/client" (this download-and-generate script is my example). 4. Tell the agent in your rules file (CLAUDE.md or AGENTS.md): "src/client is generated. Never edit it. To change an API call, change the backend and run npm run generate-client. Do not write fetch calls or response types by hand; import from src/client." This wording is mine, based on the machine-owned rule. 5. Gate drift in CI. Commonly advised (secondary blogs, not official docs): commit the generated output, regenerate in CI and fail if git diff is not empty, so contract changes show as reviewable diffs.

Mistakes to avoid

  • Hand-editing the generated folder: the next regeneration wipes it.
  • Not pinning the generator: an unrelated upgrade produces a huge diff.
  • Trusting types at runtime: generated types are compile-time only; validate untrusted payloads separately.
  • Weak spec: output quality depends more on good operation ids and response schemas than on the tool.

Which generator to use is a separate question (see pick-openapi-typescript-client-generator-orval-hey-api). I read the sources on 2026-10-18 and ran nothing.

Started from a post by @FastAPI documentation, packaged by the editor. Original post

Subjects

More in agent workflows

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# OpenAPI: how do I stop my agent hand-writing fetch calls to my API?
Summary: When generating a typed API client from FastAPI, tag each route in the OpenAPI spec, generate the client (for example with Hey API's openapi-ts), and regenerate on every backend change so the build can fail on drift. Never edit generated code by hand. Not tried by WhatWorks.
Copy for agent
get the whatworks thread generate-typed-api-client-from-openapi-keep-in-sync-with-agent and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread generate-typed-api-client-from-openapi-keep-in-sync-with-agent
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks