# OpenAPI to Fetch Client

Generate a self-contained typed TypeScript fetch client from an OpenAPI or Swagger paths spec, with functions or a class and configurable error style.

## Run it

- **CLI:** `gizza tool openapi-to-fetch-client "openapi: 3.1.0
servers:
  - url: https://api.example.com
paths:
  /pets/{petId}:
    get:
      operationId: getPet
      parameters:
        - name: petId
          in: path
          required: true
          schema: { type: string }
      responses:
        '200':
          description: OK"`
- **Web:** https://gizza.ai/tools/openapi-to-fetch-client/
- **Agents:** machine-readable descriptor (parameters JSON Schema) at https://gizza.ai/tools/openapi-to-fetch-client/tool.json

## Inputs

- `spec` — OpenAPI or Swagger spec _(field)_
- `input_format` — Input format _(field)_
- `style` — Output style _(field)_
- `client_name` — Class name _(field)_
- `naming` — Function naming _(field)_
- `param_style` — Parameter style _(field)_
- `error_handling` — Error handling _(field)_
- `base_url` — Base URL override _(field)_
- `types_module` — Types module _(field)_
- `tags` — Tag filter _(field)_
- `jsdoc` — Include JSDoc _(field)_
- `indent` — Indent spaces _(field)_

## Output

- Generated TypeScript client (text)

## Query parameters

Open the tool pre-filled and auto-run via URL:

- `spec` — OpenAPI or Swagger spec
- `input_format` — Input format
- `style` — Output style
- `client_name` — Class name
- `naming` — Function naming
- `param_style` — Parameter style
- `error_handling` — Error handling
- `base_url` — Base URL override
- `types_module` — Types module
- `tags` — Tag filter
- `jsdoc` — Include JSDoc
- `indent` — Indent spaces

Example: `https://gizza.ai/tools/openapi-to-fetch-client/?spec=openapi%3A%203.1.0%0Aservers%3A%0A%20%20-%20url%3A%20https%3A%2F%2Fapi.example.com%0Apaths%3A%0A%20%20%2Fpets%2F%7BpetId%7D%3A%0A%20%20%20%20get%3A%0A%20%20%20%20%20%20operationId%3A%20getPet%0A%20%20%20%20%20%20parameters%3A%0A%20%20%20%20%20%20%20%20-%20name%3A%20petId%0A%20%20%20%20%20%20%20%20%20%20in%3A%20path%0A%20%20%20%20%20%20%20%20%20%20required%3A%20true%0A%20%20%20%20%20%20%20%20%20%20schema%3A%20%7B%20type%3A%20string%20%7D%0A%20%20%20%20%20%20responses%3A%0A%20%20%20%20%20%20%20%20%27200%27%3A%0A%20%20%20%20%20%20%20%20%20%20description%3A%20OK&input_format=auto&style=functions&client_name=ApiClient&naming=operation_id&param_style=object&error_handling=throw&base_url=https%3A%2F%2Fapi.example.com%2Fv1&types_module=.%2Ftypes&tags=pets%2C%20admin&jsdoc=true&indent=2`

---

## What this tool does

Paste an OpenAPI 3.x or Swagger 2.0 document and this tool generates a single TypeScript file: a dependency-free client that calls your API with `fetch`. It focuses on the operations layer (`paths`): function names, path parameters, query parameters, header parameters, request bodies, response types, and the small runtime helpers needed to call `fetch` safely.

Use it when you already have, or plan to generate, schema types from `components.schemas` separately. Local `$ref` schema names become imported TypeScript types from your `types_module`; blank `types_module` emits placeholder aliases so the output still compiles while you wire real types in later.

It can generate:

- exported async functions or one client class;
- operation names from `operationId` or method + path;
- single request-object calls or positional path/body arguments;
- `ApiError` throwing or `{ data, error, response }` result unions;
- base URLs from the spec or from an override;
- typed path, query, header, body, and response references;
- JSDoc from summaries/descriptions and `@deprecated` markers.

## Worked example

Use a spec like this:

```json
{"openapi":"3.1.0","servers":[{"url":"https://api.example.com"}],"paths":{"/pets/{petId}":{"get":{"operationId":"getPet","parameters":[{"name":"petId","in":"path","required":true,"schema":{"type":"string"}},{"name":"include","in":"query","schema":{"type":"string"}}],"responses":{"200":{"description":"OK","content":{"application/json":{"schema":{"$ref":"#/components/schemas/Pet"}}}}}}}}}
```

With the default **functions**, **request object**, and **throw ApiError** settings, the output contains a call shaped like:

```ts
export interface GetPetRequest {
  petId: string;
  include?: string;
}

export async function getPet(params: GetPetRequest, options: RequestOptions = {}): Promise<Pet> {
  return apiFetch("GET", `/pets/${encodeURIComponent(String(params.petId))}`, { include: params.include }, {}, undefined, options);
}
```

Point `types_module` at your generated schema declarations (for example `./types`) so local refs such as `#/components/schemas/Pet` are imported as `Pet`. Use `base_url` when you want the generated file to call staging, production, or a test server regardless of the spec's `servers` section.

## Limits and edge cases

- This generates one text file, not a multi-file SDK package. It does not create `models/`, `apis/`, npm metadata, or a runtime library folder.
- It reads `paths` operations only. It does not generate schema model declarations from `components.schemas`; use a schema-types generator for that and import those types with `types_module`.
- Only local refs such as `#/components/schemas/Pet` or `#/definitions/Pet` become named TypeScript types. External or remote refs fall back to `unknown` because this tool does not fetch the network.
- `application/json` request/response bodies are preferred; otherwise the first declared content type is used. Form-data and cookie parameters are noted in comments but are not automatically assembled.
- The success type comes from the lowest 2xx response (then `default`). Other response shapes are runtime `unknown` in the thrown `ApiError` or result union.
- Auth helpers, retries, middleware, interceptors, and date revivers are intentionally left to `RequestOptions.headers`, `RequestOptions.fetch`, or your surrounding app code.

## FAQ

<details>
<summary>Does this replace a full OpenAPI SDK generator?</summary>

No. It is intentionally smaller: one dependency-free TypeScript file for operations over `fetch`. Full SDK generators can produce package metadata, model files, API class folders, auth middleware, and many naming knobs; this tool is for a quick client layer you can paste into an existing project.

</details>

<details>
<summary>Where do the TypeScript model types come from?</summary>

Local schema refs become imported names from `types_module`, which defaults to `./types`. Generate those model types separately, or leave `types_module` blank to emit placeholder `type Name = unknown` aliases while prototyping.

</details>

<details>
<summary>Can it call private or remote refs while generating?</summary>

No. The block is pure Rust/WASM and never fetches referenced documents. External refs are represented as `unknown` so the generated client remains deterministic and safe to run locally.

</details>

<details>
<summary>How should I add authentication?</summary>

Pass auth headers with `RequestOptions.headers`, either per call or in the client-class constructor. For retries, refresh tokens, logging, or tracing, pass a wrapped `fetch` implementation through `RequestOptions.fetch`.

</details>

## Related tools

- [Apply a Unified Diff to a File](https://gizza.ai/tools/apply-patch/): Paste a file and a unified diff to get the patched text in your browser, with reverse apply, fuzz matching, and per-hunk conflict reports.
- [Autocomplete Trie](https://gizza.ai/tools/autocomplete-trie/): Build a prefix trie from a pasted wordlist and get ranked autocomplete suggestions for any typed prefix. Weights, typo tolerance, trie stats, JSON. Runs locally.
- [Code Chunker](https://gizza.ai/tools/code-chunker/): Split Python, Rust, JavaScript, TypeScript, Go, Java, C/C++, C#, PHP, or Swift into line-ranged chunks that keep functions and classes intact.
- [Code Formatter](https://gizza.ai/tools/code-formatter/): Beautify and re-indent minified or messy HTML, CSS, JavaScript, or JSON. Auto-detect the language, choose spaces or tabs, and format locally in your browser.
- [Code language detector](https://gizza.ai/tools/code-language-detect/): Detect the likely programming language of a pasted code snippet with ranked alternatives, confidence and explainable signals.
