---
title: "Code Generation"
description: "Code Generation in this architecture is a specialized subsystem designed to bridge the gap between static API documentation and actionable, implementation-ready code. Its primary purpose is to tran..."
last_updated: "2026-07-02T09:46:39.438062+00:00"
canonical_url: "https://www.doc0.dev/docs/e8872a72-1909-479c-b585-dc74c6e26726/technical/api-documentation/code-generation"
---

<details>
<summary>Relevant source files</summary>

The following files were used as context for generating this wiki page:

- [packages/openapi/src/ui/operation/index.tsx](https://github.com/blade47/fumadocs/blob/main/packages/openapi/src/ui/operation/index.tsx)
- [packages/openapi/src/requests/generators/curl.ts](https://github.com/blade47/fumadocs/blob/main/packages/openapi/src/requests/generators/curl.ts)
- [packages/openapi/src/ui/operation/usage-tabs.tsx](https://github.com/blade47/fumadocs/blob/main/packages/openapi/src/ui/operation/usage-tabs.tsx)
- [packages/openapi/src/requests/generators/python.ts](https://github.com/blade47/fumadocs/blob/main/packages/openapi/src/requests/generators/python.ts)
- [packages/api-docs/src/schema/sample.ts](https://github.com/blade47/fumadocs/blob/main/packages/api-docs/src/schema/sample.ts)
- [packages/openapi/src/requests/generators/go.ts](https://github.com/blade47/fumadocs/blob/main/packages/openapi/src/requests/generators/go.ts)
- [packages/openapi/src/requests/generators/java.ts](https://github.com/blade47/fumadocs/blob/main/packages/openapi/src/requests/generators/java.ts)
- [packages/openapi/src/requests/generators/csharp.ts](https://github.com/blade47/fumadocs/blob/main/packages/openapi/src/requests/generators/csharp.ts)
- [packages/openapi/src/requests/media/adapter.ts](https://github.com/blade47/fumadocs/blob/main/packages/openapi/src/requests/media/adapter.ts)
- [packages/openapi/src/requests/generators/javascript.ts](https://github.com/blade47/fumadocs/blob/main/packages/openapi/src/requests/generators/javascript.ts)
- [packages/openapi/src/utils/pages/preset-auto.ts](https://github.com/blade47/fumadocs/blob/main/packages/openapi/src/utils/pages/preset-auto.ts)
- [packages/openapi/src/requests/generators/all.ts](https://github.com/blade47/fumadocs/blob/main/packages/openapi/src/requests/generators/all.ts)
- [packages/api-docs/src/codegen.ts](https://github.com/blade47/fumadocs/blob/main/packages/api-docs/src/codegen.ts)
- [packages/asyncapi/src/ui/operation/message-examples.tsx](https://github.com/blade47/fumadocs/blob/main/packages/asyncapi/src/ui/operation/message-examples.tsx)
- [packages/core/src/source/llms.ts](https://github.com/blade47/fumadocs/blob/main/packages/core/src/source/llms.ts)
- [packages/openapi/src/utils/get-example-requests.ts](https://github.com/blade47/fumadocs/blob/main/packages/openapi/src/utils/get-example-requests.ts)
- [packages/openapi/src/ui/operation/response-tabs.tsx](https://github.com/blade47/fumadocs/blob/main/packages/openapi/src/ui/operation/response-tabs.tsx)
- [packages/openapi/src/ui/operation/request-tabs.tsx](https://github.com/blade47/fumadocs/blob/main/packages/openapi/src/ui/operation/request-tabs.tsx)
- [packages/openapi/src/requests/generators/index.ts](https://github.com/blade47/fumadocs/blob/main/packages/openapi/src/requests/generators/index.ts)
- [packages/openapi/src/server/index.tsx](https://github.com/blade47/fumadocs/blob/main/packages/openapi/src/server/index.tsx)
- [packages/python/src/convert.ts](https://github.com/blade47/fumadocs/blob/main/packages/python/src/convert.ts)
- [packages/mdx/src/utils/codegen.ts](https://github.com/blade47/fumadocs/blob/main/packages/mdx/src/utils/codegen.ts)
- [packages/openapi/src/requests/media/encode.ts](https://github.com/blade47/fumadocs/blob/main/packages/openapi/src/requests/media/encode.ts)
- [packages/openapi/src/utils/pages/to-text.ts](https://github.com/blade47/fumadocs/blob/main/packages/openapi/src/utils/pages/to-text.ts)
- [packages/openapi/src/utils/pages/to-static-data.ts](https://github.com/blade47/fumadocs/blob/main/packages/openapi/src/utils/pages/to-static-data.ts)
- [packages/asyncapi/src/utils/pages/to-text.ts](https://github.com/blade47/fumadocs/blob/main/packages/asyncapi/src/utils/pages/to-text.ts)
- [packages/openapi/src/utils/schema.ts](https://github.com/blade47/fumadocs/blob/main/packages/openapi/src/utils/schema.ts)
- [packages/openapi/src/requests/string-utils.ts](https://github.com/blade47/fumadocs/blob/main/packages/openapi/src/requests/string-utils.ts)
- [packages/python/fumapy/__init__.py](https://github.com/blade47/fumadocs/blob/main/packages/python/fumapy/__init__.py)
- [packages/openapi/src/scalar/client.tsx](https://github.com/blade47/fumadocs/blob/main/packages/openapi/src/scalar/client.tsx)
</details>

Code Generation in this architecture is a specialized subsystem designed to bridge the gap between static API documentation and actionable, implementation-ready code. Its primary purpose is to transform formal API specifications—specifically OpenAPI—into functional code snippets for various programming languages (e.g., Python, Go, Java, cURL, JavaScript), allowing users to immediately understand how to interact with the documented endpoints.

This subsystem solves the common problem of manual documentation maintenance by dynamically generating code samples from the same source of truth used to render the documentation pages. It acts as an abstraction layer between the API definition and the final UI presentation, enabling extensibility via generator registries while handling the complexities of media type serialization, request parameter encoding, and language-specific syntax formatting.

The architecture centers on a pluggable registry pattern, where `CodeUsageGenerator` implementations are registered and then invoked within the UI layer. This decoupled design allows users to support custom programming languages or override default behavior for specific API endpoints without modifying the core codebase.

## The Generator Registry

The `CodeUsageGeneratorRegistry` is the primary orchestrator for code generation. It maintains a collection of generators keyed by language identifiers, providing a standardized interface for registration, retrieval, and removal. 

The mechanism utilizes a factory function `createCodeUsageGeneratorRegistry` that encapsulates the map-based storage. A critical aspect of this registry is that it supports inheritance, allowing new registries to be initialized with existing generator sets, which facilitates modular configurations.

```typescript
// Initializing a registry with inherited generators
const registry = createCodeUsageGeneratorRegistry(ctx.codeUsages);

// Adding an inline generator for a custom language
registry.addInline(gen);
```

Sources: [packages/api-docs/src/codegen.ts:37-72](https://github.com/blade47/fumadocs/blob/main/packages/api-docs/src/codegen.ts#L37-L72), [packages/openapi/src/ui/operation/usage-tabs.tsx:74-90](https://github.com/blade47/fumadocs/blob/main/packages/openapi/src/ui/operation/usage-tabs.tsx#L74-L90)

## Request Data Flow

The flow of code generation starts when an operation is rendered in the UI. The process involves sampling raw request data, encoding it via media type adapters, and finally passing the resulting structured request object to a specific code generator.

```mermaid
flowchart TD
    A["Operation UI"] --> B["getExampleRequests"]
    B --> C["encodeRequestData"]
    C --> D["Media Adapters (JSON, XML, etc.)"]
    D --> E["UsageTabs"]
    E --> F["Selected Code Generator"]
    F --> G["Formatted Code Output"]
```

Sources: [packages/openapi/src/ui/operation/index.tsx:77-80](https://github.com/blade47/fumadocs/blob/main/packages/openapi/src/ui/operation/index.tsx#L77-L80), [packages/openapi/src/utils/get-example-requests.ts:23-76](https://github.com/blade47/fumadocs/blob/main/packages/openapi/src/utils/get-example-requests.ts#L23-L76)

## Media Type Adapters

Media adapters handle the transformation of request bodies into language-specific formats and actual byte arrays for transport. Each adapter implements two primary methods: `encode` (for runtime usage, like a playground) and `generateExample` (for static code generation).

> [!NOTE]
> The `generateExample` function receives a `MediaContext` object. This context allows the generator to "inject" language-specific requirements—such as imports—back into the main generator's scope (e.g., `addImport` in Go/Java).

Sources: [packages/openapi/src/requests/media/adapter.ts:40-58](https://github.com/blade47/fumadocs/blob/main/packages/openapi/src/requests/media/adapter.ts#L40-L58), [packages/openapi/src/requests/media/adapter.ts:180-182](https://github.com/blade47/fumadocs/blob/main/packages/openapi/src/requests/media/adapter.ts#L180-L182)

## Request Parameter Serialization

Parameters (path, query, header, cookie) must be serialized according to their schema and "serialization style". The `encodeRequestData` function performs this logic:

1. It iterates over defined parameter types.
2. It attempts to find a specific media encoder (if defined via content).
3. If no media encoder exists, it defaults to standard serializers (`serializePathParameter`, `serializeQueryParameter`, etc.).

The `serializeSimple` function acts as the foundational mechanism for basic key-value pairs, while specialized functions handle exploded styles (where array elements or object properties are expanded).

Sources: [packages/openapi/src/requests/media/encode.ts:17-70](https://github.com/blade47/fumadocs/blob/main/packages/openapi/src/requests/media/encode.ts#L17-L70), [packages/openapi/src/requests/media/encode.ts:83-95](https://github.com/blade47/fumadocs/blob/main/packages/openapi/src/requests/media/encode.ts#L83-L95)

## Generator Implementation Strategies

Generators utilize shared string utilities to maintain consistency. The `string-utils.ts` file provides escaping helpers like `doubleQuote`, `singleQuote`, and `tripleDoubleQuote` (for Python multiline strings), which are essential for security and syntax correctness when building code strings.

| Language | Primary Serialization Mechanism |
| :--- | :--- |
| **cURL** | `-F` (multipart) or `-d` (data) flags |
| **Python** | `requests.request` with dictionary objects |
| **Go** | `http.NewRequest` with `strings.Reader` or `bytes.Buffer` |
| **Java** | `java.net.http.HttpRequest` with `BodyPublishers` |
| **C#** | `HttpClient` using `MultipartFormDataContent` or `StringContent` |

Sources: [packages/openapi/src/requests/string-utils.ts:45-73](https://github.com/blade47/fumadocs/blob/main/packages/openapi/src/requests/string-utils.ts#L45-L73), [packages/openapi/src/requests/generators/python.ts:8-58](https://github.com/blade47/fumadocs/blob/main/packages/openapi/src/requests/generators/python.ts#L8-L58)

## Lifecycle of a Code Snippet

The actual render of a code block in the UI involves a listener pattern to ensure real-time updates when an example request changes (e.g., if a user selects a different example from a dropdown).

1. `UsageTab` uses `useOperationContext` to `addListener`.
2. The `useEffect` hook subscribes to state updates.
3. Upon triggering `setExample` or `setExampleData`, the listener updates the internal `data` state.
4. `useMemo` triggers a re-generation of the code snippet via the registered `codegen.generate` function.

```mermaid
sequenceDiagram
    participant UI as Operation UI
    participant Ctx as OperationContext
    participant Tab as UsageTab
    participant Gen as Generator

    UI->>Ctx: setExample(newId)
    Ctx-->>Tab: callback(encodedData)
    Tab->>Tab: setData(encodedData)
    Tab->>Gen: generate(data, {mediaAdapters})
    Gen-->>Tab: string (formatted code)
```

Sources: [packages/openapi/src/ui/operation/usage-tabs.tsx:137-185](https://github.com/blade47/fumadocs/blob/main/packages/openapi/src/ui/operation/usage-tabs.tsx#L137-L185), [packages/openapi/src/ui/operation/context.tsx](https://github.com/blade47/fumadocs/blob/main/packages/openapi/src/ui/operation/context.tsx)

## Related

- [OpenAPI Generation](https://www.doc0.dev/docs/e8872a72-1909-479c-b585-dc74c6e26726/technical/api-documentation/openapi-generation)


## Sitemap

See the full [sitemap](https://www.doc0.dev/docs/e8872a72-1909-479c-b585-dc74c6e26726/llms.txt) for all pages in this wiki.
