Documentation

Generate a typed Rust client from OpenAPI

openapi-to-rust turns a contract into readable Serde models and async Reqwest methods. Generate the entire API or keep only the operations your application calls.

Last reviewed July 15, 2026

On This Page
  1. Generate a client
  2. Select operations
  3. Runtime behavior
  4. Generated types
  5. Typed errors
  6. SSE streaming
  7. Important limits

Generate the async Reqwest client

The HTTP client is the default output. After installing the CLI, point it at an OpenAPI 3.0 or 3.1 YAML or JSON document:

Terminalshell
openapi-to-rust generate openapi.yaml

The generator emits types.rs, client.rs, module exports, and aREQUIRED_DEPS.toml fragment. The dependency fragment is computed from the generated shapes, so a spec using UUIDs or dates includes the appropriate crates while a simpler document does not.

openapi-to-rust.tomltoml
[generator]
spec_path = "openapi.json"
output_dir = "src/generated"
module_name = "api"

[features]
enable_async_client = true

[client]
operations = [
  "createResponse",
  "GET /v1/models",
  "tag:Files",
]
prune_models = true

[http_client]
base_url = "https://api.example.com"
timeout_seconds = 30

[http_client.retry]
max_retries = 3

[http_client.auth]
type = "Bearer"
header_name = "Authorization"

Select only the operations you use

Without a [client] section, the generator keeps every operation for backward compatibility. The operations list accepts three selector forms:

  • An exact operationId, such as createResponse.
  • An exact method and path, such as GET /v1/models.
  • Every operation under a tag, such as tag:Files.

Set prune_models = true to remove schemas that are unreachable from the selected operations. When client and server selection coexist, pruning retains the union of schemas needed by both surfaces.

Configure runtime behavior

Generated methods are asynchronous and use Reqwest. TOML configuration controls the base URL, timeout, default headers, retry policy, tracing middleware, and authentication. Bearer, API-key, and custom header auth can be applied at runtime rather than compiled into generated source.

Keep secrets out of code generation

Configuration describes how auth is attached; credentials belong in your application’s environment or secret manager. Never place tokens in an OpenAPI document, generator config, or generated files.

Path-template values are percent-encoded and parameter serialization follows the analyzed OpenAPI style where implemented. Use overlays when a published document is almost correct but omits a media type or operation detail; overlays are deep-merged before analysis, avoiding a permanent fork of the upstream spec.

Get Rust types for difficult schema shapes

Generated models derive Serde serialization and deserialization. OpenAPI formats map to Rust types such aschrono::DateTime<Utc>, uuid::Uuid, url::Url, andbytes::Bytes, with per-format opt-outs in TOML.

Composition is shape-aware: discriminated object unions become tagged enums, while unions that mix scalar and object branches become untagged enums. Typed additionalProperties values become maps rather than falling back toserde_json::Value when the document provides a value schema.

Match typed operation errors

Each operation gets an error enum for documented response bodies. Transport failures remain separate, so your application can distinguish a failed network request from a valid 4xx or 5xx API response.

Typed operation errorrust
match client.create_response(request).await {
    Ok(body) => consume(body),
    Err(ApiOpError::Api(error)) => match error.typed {
        Some(CreateResponseApiError::Status4xx(body)) => {
            eprintln!("API rejected the request: {body:?}");
        }
        _ => eprintln!("raw API body: {}", error.body),
    },
    Err(ApiOpError::Transport(error)) => {
        eprintln!("request failed: {error}");
    }
}

Generate SSE streaming clients

Server-Sent Events can be configured as a first-class client surface with event parsing, event-flow modeling, and reconnection. This is useful for APIs such as streamed model responses where the OpenAPI document declares or is overlaid withtext/event-stream behavior.

Streaming code is opt-in because not every text/event-stream response shares the same event framing or lifecycle. Define the operation, stream parameter, event union, and reconnection behavior in the generator configuration.

Know what the generator does not do

  • It does not accept Swagger/OpenAPI 2.0 documents; convert them to OpenAPI 3 first.
  • Schema constraints are emitted as documentation, not runtime validation.
  • Modeled security schemes do not imply runtime credential support for every scheme; mutual TLS is one example.
  • Experimental OpenAPI 3.2 parsing does not mean every 3.2 behavior affects generated code.

See the compatibility page for the current corpus, CI evidence, and feature boundaries.