Skip to content

Quick Start

Quick Start

Build your first Rusta API in 5 minutes.

The fastest way to get a working Rusta project is with the scaffolder:

Terminal window
cargo install cargo-rusta
cargo rusta new my-api
cd my-api
cargo run

The API will be available at http://localhost:3000. See the CLI guide for template options.

Option B: Build from Scratch

1. Create a New Project

Terminal window
cargo new my-api --bin
cd my-api

2. Add Dependencies

Terminal window
# Add to Cargo.toml
cargo add rusta tokio --features full

3. Create Your First Controller

Create src/controllers/user_controller.rs:

use rusta::prelude::*;
use std::sync::Arc;
#[injectable]
pub struct UserService {
// Add your dependencies here
}
#[controller("/users")]
impl UserController {
#[get("/")]
pub async fn list(&self) -> Response {
Http::json(json!([{ "id": 1, "name": "Alice" }]))
}
#[get("/:id")]
pub async fn get(&self, Path(id): Path<String>) -> Response {
Http::json(json!({ "id": id, "name": "User" }))
}
#[post("/")]
pub async fn create(&self, Json(body): Json<serde_json::Value>) -> Response {
Http::created(body)
}
}

4. Bootstrap the Application

Update src/main.rs:

mod controllers;
use rusta::{App, Container};
use controllers::UserController;
#[tokio::main]
async fn main() {
let mut container = Container::new();
container.register(UserController::construct(&container));
App::new()
.container(container)
.run("0.0.0.0:3000", |res| match res {
Ok(addr) => println!("Listening on {}", addr),
Err(msg) => eprintln!("Startup error: {}", msg),
})
.await;
}

Create src/controllers/mod.rs:

pub mod user_controller;

5. Run and Test

Terminal window
cargo run
# Test endpoints
curl http://localhost:3000/users
curl http://localhost:3000/users/123
curl -X POST http://localhost:3000/users -H "Content-Type: application/json" -d '{"name":"Bob"}'

Next Steps