The minimal RustAPI application — your first step into the framework.
📖 Cookbook: Getting Started → Quickstart
This example demonstrates the absolute minimum needed to create a working API with RustAPI. In just ~20 lines of code, you get:
- Automatic route discovery with
RustApi::auto() - Path parameter extraction
- JSON response serialization
- OpenAPI documentation at
/docs
- Rust 1.70+
- Basic Rust knowledge
| Feature | Description |
|---|---|
RustApi::auto() |
Zero-config route discovery |
#[rustapi_rs::get] |
Route macro for GET endpoints |
Path<T> |
Path parameter extractor |
Json<T> |
JSON response type |
Schema derive |
OpenAPI schema generation |
# Run the example
cargo run -p hello-world
# Server starts at http://127.0.0.1:8080| Method | Path | Description |
|---|---|---|
| GET | /hello/{name} |
Returns greeting with name |
# Basic greeting
curl http://127.0.0.1:8080/hello/World
# Response: {"greeting":"Hello, World!"}
# With different name
curl http://127.0.0.1:8080/hello/RustAPI
# Response: {"greeting":"Hello, RustAPI!"}
# View API documentation
open http://127.0.0.1:8080/docs#[derive(Serialize, utoipa::ToSchema)]
struct Message {
greeting: String,
}The Serialize derive enables JSON serialization, and ToSchema generates OpenAPI schema.
#[rustapi_rs::get("/hello/{name}")]
async fn hello(Path(name): Path<String>) -> Json<Message> {
Json(Message {
greeting: format!("Hello, {name}!"),
})
}#[rustapi_rs::get("/hello/{name}")]— Defines a GET endpoint with path parameterPath(name): Path<String>— Extracts the{name}parameter from the URLJson<Message>— Returns JSON response
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
RustApi::auto().run("0.0.0.0:8080").await
}RustApi::auto() automatically discovers all routes marked with RustAPI macros — no manual registration needed!
Unlike other frameworks that require manual route registration:
// Other frameworks:
app.route("/hello/:name", get(hello))
.route("/users", get(list_users))
.route("/users/:id", get(get_user));
// RustAPI:
RustApi::auto() // That's it!Path parameters are defined with {param} syntax and extracted with Path<T>:
#[rustapi_rs::get("/users/{id}/posts/{post_id}")]
async fn get_post(
Path((id, post_id)): Path<(u64, u64)>
) -> String {
format!("User {} Post {}", id, post_id)
}Once comfortable with this example, move to:
- LEARNING_PATH.md — Structured learning progression
- FEATURES.md — Feature flags reference
- RustAPI Cookbook — Full documentation