Skip to content

Navigation Menu

Sign in
Sign up

Repository files navigation


Vision

RustAPI redefines API development for the AI era.

We combine Rust's performance and safety with FastAPI's ergonomics. Write type-safe, production-ready APIs without fighting trait bounds. MCP servers, LLM integrations, or classic REST APIs β€” one framework for all.


Philosophy

"API surface is ours, engines can change."

RustAPI follows a Facade Architecture β€” a stable, ergonomic public API that shields you from internal complexity and breaking changes.

Core Principles

Principle What It Means
🎯 5-Line APIs A working REST endpoint in 5 lines. No ceremony.
πŸ›‘οΈ Stable Surface Your code depends on rustapi-rs. Internal crates (hyper, tokio, validator) are implementation details.
πŸ”„ Engines Change We can swap hyper for h3, upgrade tokio, or replace validator β€” your code stays the same.
🎁 Batteries Included JWT, CORS, Rate Limiting, OpenAPI β€” all built-in, all optional via feature flags.
πŸ€– LLM-First TOON format, token counting headers, MCP-ready. Built for the AI era.

Why This Matters

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Your Application β”‚
β”‚ use rustapi_rs::prelude::* β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ rustapi-rs (Facade) β”‚
β”‚ Stable API ── Never Breaks β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ rustapi-core β”‚ rustapi-toon β”‚ rustapi-extrasβ”‚ ... β”‚
β”‚ (hyper) β”‚ (serde) β”‚ (jwt) β”‚ β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Foundation: tokio, serde, hyper β”‚
β”‚ ↑ Can be upgraded/swapped internally β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Internal upgrades don't break your code. When hyper 2.0 releases, we update rustapi-core. Your RustApi::new() keeps working.

πŸ“š Read more: docs/PHILOSOPHY.md | docs/ARCHITECTURE.md

use rustapi_rs::prelude::*;
#[rustapi_rs::get("/hello/{name}")]
async fn hello(Path(name): Path<String>) -> Json<Message> {
 Json(Message { greeting: format!("Hello, {name}!") })
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
 RustApi::auto().run("0.0.0.0:8080").await
}

5 lines of code. Auto-generated OpenAPI docs. Production-ready.


Quick Start

[dependencies]
rustapi-rs = "0.1.4"
use rustapi_rs::prelude::*;
#[derive(Serialize, Schema)]
struct User { id: u64, name: String }
#[rustapi_rs::get("/users/{id}")]
async fn get_user(Path(id): Path<u64>) -> Json<User> {
 Json(User { id, name: "Tunahan".into() })
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
 // Zero config: all `#[rustapi_rs::get/post/..]` routes are auto-registered.
 // Swagger UI is enabled at /docs by default (when built with the `swagger-ui` feature).
 RustApi::auto().run("127.0.0.1:8080").await
}

http://localhost:8080/docs β†’ Swagger UI ready.


Features

Feature Description
Type-Safe Extractors Json<T>, Query<T>, Path<T> β€” compile-time guarantees
Zero-Config Routing Macro-decorated routes auto-register at startup (RustApi::auto())
Auto OpenAPI Your code = your docs. /docs endpoint out of the box
Validation #[validate(email)] β†’ automatic 422 responses
JWT Auth One-line auth with AuthUser<T> extractor
CORS & Rate Limit Production-ready middleware
TOON Format 50-58% token savings for LLMs

Optional Features

rustapi-rs = { version = "0.1.4", features = ["jwt", "cors", "toon"] }
  • jwt β€” JWT authentication
  • cors β€” CORS middleware
  • rate-limit β€” IP-based rate limiting
  • toon β€” LLM-optimized responses
  • full β€” Everything included

Examples

All examples in this repository are written in the Phase 6 "zero-config" style.

cargo run -p hello-world
cargo run -p crud-api
cargo run -p auth-api
cargo run -p sqlx-crud
cargo run -p toon-api
cargo run -p proof-of-concept

πŸ€– LLM-Optimized: TOON Format

RustAPI is built for AI-powered APIs.

TOON (Token-Oriented Object Notation) uses 50-58% fewer tokens than JSON. Ideal for MCP servers, AI agents, and LLM integrations.

use rustapi_rs::toon::{Toon, LlmResponse, AcceptHeader};
// Direct TOON response
#[rustapi::get("/ai/users")]
async fn ai_users() -> Toon<UsersResponse> {
 Toon(get_users())
}
// Content negotiation: JSON or TOON based on Accept header
#[rustapi::get("/users")]
async fn users(accept: AcceptHeader) -> LlmResponse<UsersResponse> {
 LlmResponse::new(get_users(), accept.preferred)
}
// Headers: X-Token-Count-JSON, X-Token-Count-TOON, X-Token-Savings

Why TOON?

  • Compatible with Claude, GPT-4, Gemini β€” all major LLMs
  • Cut your token costs in half
  • Optimized for MCP (Model Context Protocol) servers

Architecture

RustAPI follows a Facade Architecture β€” a stable public API that shields you from internal changes.

System Overview

graph TB
 subgraph Client["🌐 Client Layer"]
 HTTP[HTTP Request]
 LLM[LLM/AI Agent]
 MCP[MCP Client]
 end
 subgraph Public["πŸ“¦ rustapi-rs (Public Facade)"]
 direction TB
 Prelude[prelude::*]
 Macros["#[rustapi::get/post]<br>#[rustapi::main]"]
 Types[Json, Query, Path, Form]
 end
 subgraph Core["βš™οΈ rustapi-core (Engine)"]
 direction TB
 Router[Radix Router<br>matchit]
 Extract[Extractors<br>FromRequest trait]
 MW[Middleware Stack<br>Tower-like layers]
 Resp[Response Builder<br>IntoResponse trait]
 end
 subgraph Extensions["πŸ”Œ Extension Crates"]
 direction LR
 OpenAPI["rustapi-openapi<br>Swagger/Docs"]
 Validate["rustapi-validate<br>Request Validation"]
 Toon["rustapi-toon<br>LLM Optimization"]
 Extras["rustapi-extras<br>JWT/CORS/RateLimit"]
 end
 subgraph Foundation["πŸ—οΈ Foundation Layer"]
 direction LR
 Tokio[tokio<br>Async Runtime]
 Hyper[hyper 1.0<br>HTTP Protocol]
 Serde[serde<br>Serialization]
 end
 HTTP --> Public
 LLM --> Public
 MCP --> Public
 Public --> Core
 Core --> Extensions
 Extensions --> Foundation
 Core --> Foundation
Loading

Request Flow

sequenceDiagram
 participant C as Client
 participant R as Router
 participant M as Middleware
 participant E as Extractors
 participant H as Handler
 participant S as Serializer
 C->>R: HTTP Request
 R->>R: Match route (radix tree)
 R->>M: Pass to middleware stack
 
 loop Each Middleware
 M->>M: Process (JWT, CORS, RateLimit)
 end
 
 M->>E: Extract parameters
 E->>E: Json<T>, Path<T>, Query<T>
 E->>E: Validate with #[validate]
 
 alt Validation Failed
 E-->>C: 422 Unprocessable Entity
 else Validation OK
 E->>H: Call async handler
 H->>S: Return response type
 
 alt TOON Enabled
 S->>S: Check Accept header
 S->>S: Serialize as TOON/JSON
 S->>S: Add token count headers
 else Standard
 S->>S: Serialize as JSON
 end
 
 S-->>C: HTTP Response
 end
Loading

Crate Dependency Graph

graph BT
 subgraph User["Your Application"]
 App[main.rs]
 end
 subgraph Facade["Single Import"]
 RS[rustapi-rs]
 end
 subgraph Internal["Internal Crates"]
 Core[rustapi-core]
 Macros[rustapi-macros]
 OpenAPI[rustapi-openapi]
 Validate[rustapi-validate]
 Toon[rustapi-toon]
 Extras[rustapi-extras]
 end
 subgraph External["External Dependencies"]
 Tokio[tokio]
 Hyper[hyper]
 Serde[serde]
 Utoipa[utoipa]
 Validator[validator]
 end
 App --> RS
 RS --> Core
 RS --> Macros
 RS --> OpenAPI
 RS --> Validate
 RS -.->|optional| Toon
 RS -.->|optional| Extras
 
 Core --> Tokio
 Core --> Hyper
 Core --> Serde
 OpenAPI --> Utoipa
 Validate --> Validator
 Toon --> Serde
 style RS fill:#e1f5fe
 style App fill:#c8e6c9
Loading

Design Principles

Principle Implementation
Single Entry Point use rustapi_rs::prelude::* imports everything you need
Zero Boilerplate Macros generate routing, OpenAPI specs, and validation
Compile-Time Safety Generic extractors catch type errors at compile time
Opt-in Complexity Features like JWT, TOON are behind feature flags
Engine Abstraction Internal hyper/tokio upgrades don't break your code

Crate Responsibilities

Crate Role
rustapi-rs Public facade β€” single use for everything
rustapi-core HTTP engine, routing, extractors, response handling
rustapi-macros Procedural macros: #[rustapi::get], #[rustapi::main]
rustapi-openapi Swagger UI generation, OpenAPI 3.0 spec
rustapi-validate Request body/query validation via #[validate]
rustapi-toon TOON format serializer, content negotiation, LLM headers
rustapi-extras JWT auth, CORS, rate limiting middleware

Roadmap

  • Core framework (routing, extractors, server)
  • OpenAPI & Validation
  • JWT, CORS, Rate Limiting
  • TOON format & LLM optimization
  • Coming soon...

License

MIT or Apache-2.0, at your option.

About

Ergonomic Web and API Framework for Rust

Resources

Code of conduct

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /