Getting Started with Sword
You can create a project with the sword CLI, start from an empty crate, or run the examples in the GitHub repository. Each example is designed to show a specific part of the framework and help you understand how it works.
Create a project with the CLI
The CLI creates the project, the configuration, the shared module and, where the application type uses it, the base error system. Install it from the repository:
cargo install --git https://github.com/sword-web/sword sword-cliThen create a web project:
sword new hello-sword
cd hello-sword
cargo runThe command asks for the application type and the layout. For another type or a module layout, pass the options:
sword new hello-sword --kind grpc
sword new hello-sword --module usersThe generated project depends on the Sword version the CLI ships with. See Sword CLI for the full command set.
Create a project by hand
Sword is published on crates.io, so you can start from an empty crate without cloning the repository. The framework needs a configuration file: Application::builder() looks for config/config.toml and fails if it is missing.
cargo new hello-sword
cd hello-sword
cargo add sword --features webCreate config/config.toml:
[application]
name = "Hello Sword"
[web]
port = 3000Replace src/main.rs with:
use sword::prelude::*;
use sword::web::*;
#[controller(kind = Controller::Web, path = "/hello")]
struct HelloController;
impl HelloController {
#[get("/")]
async fn hello(&self) -> WebResult {
Ok(JsonResponse::Ok().message("Hello from Sword"))
}
}
struct AppModule;
impl Module for AppModule {
fn register_controllers(controllers: &ControllerRegistry) {
controllers.register::<HelloController>();
}
}
#[sword::main]
async fn main() -> Result<(), Error> {
let app = Application::builder()?
.with_module::<AppModule>()
.build()?;
app.run().await
}Run it and query the endpoint:
cargo run
curl http://localhost:3000/hello{ "code": 200, "success": true, "message": "Hello from Sword" }Run the web example
The web example is the most complete one. It needs nothing but a Rust toolchain: the data lives in an in-memory SQLite database that is created on startup.
git clone https://github.com/sword-web/sword.git
cd sword/examples/web
cargo runThe API is served under the /api prefix:
curl http://localhost:8081/api/usersThe same example ships a small browser client at http://localhost:8081/api/static/, which exercises the CRUD endpoints and the SSE stream without extra setup.
Examples
You can find the examples in the repository on GitHub.
Web Application Example
This is the most direct example for understanding the base flow of a REST API built with Sword on top of axum.
Socket.IO Application Example
This example focuses on Socket.IO integration with Sword, using the socketioxide crate.
gRPC Application Example
This example focuses on gRPC integration with Sword, using the tonic crate.
Events Application Example
This example publishes and consumes events through two transports at once: in-memory for events that stay in the process, and NATS for events that leave it. It needs a NATS broker to run.
Jobs Application Example
This example shows the three kinds of schedule: a cron expression, a fixed interval, and a one-shot task that runs after a delay.

