Skip to content

About

Define your web API in Rust, generate typed TypeScript, Python and Rust clients. Serve it with Axum, Cloudflare Workers or any HTTP server.

Topics

Resources

Contributing

Stars

26 stars

Watchers

3 watching

Forks

Repository files navigation

reflectapi

Define your web API in Rust, generate typed TypeScript, Python and Rust clients, typed streams included. Serve it with axum, Cloudflare Workers or any HTTP server.

Built and used in production at Partly.

Website · Quick start · API reference · Talk: Reflect your Rust API into the world

Example

A route is a Rust function:

use reflectapi::{Builder, Empty, Input, Output};
use serde::{Deserialize, Serialize};

#[derive(Deserialize, Input)]
struct CreateUser { name: String, email: String }

#[derive(Serialize, Output)]
struct User { id: u32, name: String, email: String }

async fn create_user(_: (), req: CreateUser, _: Empty) -> User {
    User { id: 1, name: req.name, email: req.email }
}

let (schema, routers) = Builder::new()
    .route(create_user, |r| r.name("users.create"))
    .build()?;

reflectapi codegen turns the schema into clients, and each client gets the same function, typed:

import { client } from "./generated";

const api = client("https://api.example.com");
const result = await api.users.create({ name: "Ada", email: "ada@example.com" }, {});
if (result.is_ok()) {
  console.log(result.unwrap_ok().id);
}

What you get

  • Clients as precise as your Rust types: generics, enums with data, absent-or-null fields and serde's attributes reach the clients as they are.
  • Typed errors: a route's error enum chooses each response's status, and every client matches on it; a missed variant is a compile error in TypeScript and Rust.
  • Typed streams: a route can stream typed events over Server-Sent Events, read as an async iterator or a Stream in each client.
  • TypeScript, Python and Rust clients, plus an OpenAPI document for every other language and tool.
  • Deprecations that reach the editor, and open enums for clients built before a variant was added.
  • Serve it anywhere: axum in one call, Cloudflare Workers, or any HTTP server through a framework-free dispatcher.

Get started

cargo add reflectapi --features builder,axum
cargo binstall reflectapi-cli   # the codegen CLI

Then follow the Quick start. The reflectapi-demo crate is a complete example.

Alternatives

Other ways to connect a Rust server to its clients, and when each fits.

  • utoipa: you keep your web framework's handlers (axum, actix-web, Rocket and others) and want an OpenAPI document generated from annotations on them.
  • aide: you build with axum and want the OpenAPI document derived from your routes and extractors.
  • OpenAPI client generators, such as openapi-generator, openapi-typescript, orval and progenitor: an OpenAPI document is the contract, or you need clients in languages reflectapi doesn't generate. reflectapi can write that document too; see Supported Outputs.
  • ts-rs, specta and typeshare: you only need your Rust types in TypeScript or other languages, and write the calls yourself.
  • tonic with Protocol Buffers: the contract is a .proto file, and clients and servers speak gRPC.

reflectapi fits when you own the server and its clients, routes can be named calls rather than REST resources, and the Rust types and their serde attributes should be the only definition of the API. Why reflectapi? explains why it doesn't build on OpenAPI.

License

Licensed under either of Apache License, Version 2.0 or MIT license, at your option.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in reflectapi by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Contributing

See CONTRIBUTING.md for building, testing, the docs and releases.

About

Define your web API in Rust, generate typed TypeScript, Python and Rust clients. Serve it with Axum, Cloudflare Workers or any HTTP server.

Topics

Resources

Contributing

Stars

26 stars

Watchers

3 watching

Forks

Releases

Used by

Contributors

Languages