Skip to content

Generate a Rust SDK for your API

Octri generates a typed Rust crate for your API with serde models and typed errors, publishes it to crates.io under your crate name, and regenerates it whenever your API changes.

Start from scratch

Or upload a file: OpenAPI 3.0 or 3.1, Swagger 2.0, Postman or AsyncAPI.

Install from crates.io
cargo add acme-api

The Rust API client your users get

What your users write once the Rust package is installed.

Two call styles come out of the same generator. class-namespaced is the default: one client object with operations grouped by resource, the shape most popular SDKs use and the one your API reference already shows. functions is the alternative: no client object, one configure call, and each operation imported on its own. It is one setting per language, and nothing else about the client changes with it.

Every call returns a Result, so ? works the way it does in the rest of your users’ code, and a failure they forget to handle is a compiler warning. Optional parameters are Option values, in the order your API declares them.

Published with your crates.io token, and docs.rs builds the rustdoc for every release on its own, so the reference page your users read updates with the crate.

Rust
use acme_api::{Acme, ClientAuthConfig, ClientConfig};

let acme = Acme::new(ClientConfig {
    base_url: Some("https://api.acme.com/v1".to_string()),
    auth: Some(ClientAuthConfig {
        bearer: Some(std::env::var("ACME_TOKEN")?),
        ..Default::default()
    }),
    ..Default::default()
});

// The closure sees each item. Return false to stop paging early.
acme.users.list_paginated(Some(100), None, None, |user| {
    println!("{}", user.id);
    true
})?;

let invoice = acme.invoices.create("cus_123".to_string(), 4900, None)?;

The Rust SDK at a glance

What a developer checks before adding the Rust package to a project.

Runs on
Stable Rust, edition 2021
Transport
ureq, blocking, so no async runtime is required
Runtime dependencies
ureq, serde, serde_json and base64
Models
Structs deriving Serialize, Deserialize, Clone and Debug
Pagination
Closure that returns false to stop
Ships to
crates.io, with rustdoc on docs.rs

When a call fails

SdkError is an enum, so a match names validation, HTTP, network and timeout failures as separate arms, and kind() maps the status codes callers branch on most to variants.

Typed errors
use acme_api::{SdkError, SdkHttpErrorKind};

match acme.invoices.create("cus_123".to_string(), 4900, None) {
    Ok(invoice) => println!("created {}", invoice.id),
    Err(SdkError::Http(err)) if matches!(err.kind(), SdkHttpErrorKind::RateLimit) => {
        // 429 after the built-in retries. Back off at the caller.
    }
    Err(SdkError::Http(err)) => {
        eprintln!("status {} request {:?}", err.status_code, err.request_id);
    }
    Err(other) => return Err(other.into()),
}

Configure your Rust SDK

Rust has no engine setting. The operations block on ureq, and an optional async feature is there for the request layer.

  • Client style

    class-namespaced (default), class, namespaced, functions

    A client struct with a public field per resource, so acme.users.list() reads like the clients Rust users already know. Functions are module-level, under acme_api::methods.

  • Argument style

    positional (default), object

    Required parameters by value and optional ones as Option, in the order your API declares them. Object moves them into a params struct, which is kinder to callers once an operation has more than three or four.

  • Doc comments

    full (default), minimal

    Full becomes rustdoc, which is the page your users read on docs.rs. Rust developers expect that page to be complete, so keep it on full.

Method naming and namespacing work the same in every language: operations are grouped by your API’s tags or by path, and the namespace word is dropped from the method name unless you ask to keep the operationId whole.

Publishing to crates.io: names are permanent and unscoped

Crate names are global and first come, first served, with no organisation scopes to fall back on. A name cannot be renamed or reused once published, and a version can be yanked but never replaced. Pick the crate name before the first release, and check it is free on crates.io before you announce it anywhere.

crates.io crate name
acme-api
  • Not possible: Rename the crate
  • Not possible: Replace a published version
  • Still possible: Yank a version

Retries, pagination and typed errors, built in

The parts nobody wants to hand-write, and the parts a hand-written client usually skips.

  • Retries with exponential backoff and jitter, on the statuses that mean "try again", with a per-attempt timeout.
  • Idempotency keys on the methods that need them, so a retried write does not become two.
  • Auth wired in from your API’s security schemes, read from wherever your users keep secrets.
  • Typed errors split by cause: a failed constraint, an HTTP status, a network failure and a timeout are four different things.
  • Pagination that follows whichever style your API uses, cursor, offset, page number or next URL, without prefetching the collection.
  • Server-sent events as a native stream, for the operations that stream.
  • Error reporting to Octri’s monitoring, pre-wired and switched off until a consumer opts in.
  1. Add your API

    Upload an OpenAPI or Swagger file, paste a URL, connect the repository it lives in, or add endpoints in the editor.

  2. Read the audit

    Octri scores your API out of 10 on what a generator can do with it and names every missing schema and clashing operationId. 8 of the 14 rules come with a button that writes the fix.

  3. Shape the client

    Client style, argument style, namespacing, auth, retries and pagination, set once for the project and overridden per language or per endpoint.

  4. Generate and read it

    The package is generated, compiled and handed to you as source you can read before anyone installs it.

  5. Publish, then keep publishing

    Release to the registry under your own account. Every later change to your API regenerates the client, and the version number follows the version your API declares.

Rust SDK generator questions

Progenitor generates a Rust client from OpenAPI 3.0 through a macro, a build script or a static crate you publish yourself, and its examples use reqwest. OpenAPI Generator’s Rust client is marked beta and offers reqwest and hyper. Octri generates a blocking crate on ureq with async behind a feature, publishes it to crates.io under your name, and regenerates it when your API changes.