Quickstart: Your First Spin Application

https://leanpub.com/building-serverless-apps-with-wasm-and-spin

Setting Up Your Development Machine

https://leanpub.com/building-serverless-apps-with-wasm-and-spin

Install Prerequisites

https://leanpub.com/building-serverless-apps-with-wasm-and-spin

Install the Spin CLI

https://leanpub.com/building-serverless-apps-with-wasm-and-spin

Install Spin with mise

https://leanpub.com/building-serverless-apps-with-wasm-and-spin

Install Spin with Homebrew

https://leanpub.com/building-serverless-apps-with-wasm-and-spin

Verify Your Spin Installation

https://leanpub.com/building-serverless-apps-with-wasm-and-spin

Install Rust

https://leanpub.com/building-serverless-apps-with-wasm-and-spin

Update the Rust Toolchain

https://leanpub.com/building-serverless-apps-with-wasm-and-spin

Install the Rust Target

https://leanpub.com/building-serverless-apps-with-wasm-and-spin

Install Spin Templates

https://leanpub.com/building-serverless-apps-with-wasm-and-spin

Run a Smoke Test

https://leanpub.com/building-serverless-apps-with-wasm-and-spin

Scaffolding Spin Applications

https://leanpub.com/building-serverless-apps-with-wasm-and-spin

Running spin new

https://leanpub.com/building-serverless-apps-with-wasm-and-spin

Generated Project Structure

https://leanpub.com/building-serverless-apps-with-wasm-and-spin

spin.toml: The Application Manifest

https://leanpub.com/building-serverless-apps-with-wasm-and-spin

Cargo.toml: Library Crate Configuration

https://leanpub.com/building-serverless-apps-with-wasm-and-spin

.gitignore: Version Control Defaults

https://leanpub.com/building-serverless-apps-with-wasm-and-spin

The Spin Workflow: Code, Build, Run

Time to write some Rust and proactively run through the Spin Workflow. We’ll replace the generated src/lib.rs with a clean Spin 4.0-style HTTP service, build it, run it, and hit it with curl. Small scope, complete result.

To do so, we’ll use the hello-rust application we scaffolded in the previous section.

Code

Open src/lib.rs and replace its contents entirely:

use spin_sdk::http::{IntoResponse, Request, StatusCode};
use spin_sdk::http_service;

#[http_service]
async fn handle_hello_rust(req: Request) 
-> anyhow::Result<impl IntoResponse> {
    println!("Handling {:?} request to {:?}", req.method(), req.uri());
    if req.method() == "POST" {
        return Ok((
            StatusCode::METHOD_NOT_ALLOWED,
            "Sorry - No POST allowed!".to_string(),
        ).into_response());
    }
    Ok((StatusCode::OK, "Hello, from Rust!".to_string()).into_response())
}

Let’s read it top to bottom, because every line earns its place.

Request and StatusCode are re-exports of the types from the standard http Rust crate. If you’ve used hyper, axum, or reqwest before, they’ll feel familiar — Spin didn’t invent bespoke HTTP types; it leaned on the ecosystem’s.

The #[http_service] macro is the 4.0 way to declare an HTTP handler. It wraps your async fn and exports it as the component’s entry point, so the Spin host can call it when the HTTP trigger fires. You write plain application code; the macro handles the component-model glue. That the function is async is the headline change in Spin 4.0. The host APIs (outbound HTTP, key-value, variables, databases) are all async now, so your handler is async too, and you .await them naturally. Our handle_hello_rust handler doesn’t await anything yet, but the shape is ready for when it does.

Returning anyhow::Result<impl IntoResponse> means you can use the question mark operator (?) throughout and let errors bubble up as a clean 500 status instead of writing match arms for every fallible call. It’s the idiomatic error story you’ll see in every handler in this book.

Build

No matter which language you use to build Spin apps, you’ll always turn source code into WebAssembly components using the language-agnostic spin build command:

# Compile source down to WebAssembly
spin build
# Building hello-rust with `cargo build --target wasm32-wasip2 --release`
# ...
# Finished `release` profile [optimized] target(s) in 12.15s
# Finished building all Spin components

Because our application is written in Rust, spin build runs cargo build --target wasm32-wasip2 --release under the hood and emits a WebAssembly component at target/wasm32-wasip2/release/hello_rust.wasm. Since we already compiled this Spin app in the previous section, you may notice that subsequent builds are much faster. This is simply because dependencies haven’t changed and don’t need to be recompiled.

Run

# Launch the application
spin up

# Logging component stdio to ".spin/logs/"
# Serving http://127.0.0.1:3000
# Available Routes:
#  hello-rust: http://127.0.0.1:3000 (wildcard)

Spin reads the application manifest (spin.toml), loads your component, wires it to the HTTP trigger, and prints a local address, by default http://127.0.0.1:3000. Leave it running and, in another terminal, send it a request:

# Send a GET request to the Spin App
curl -iX GET localhost:3000/foo

# HTTP/1.1 200 OK
# transfer-encoding: chunked
# date: Mon, 31 Aug 2026 10:57:30 GMT

# Hello, from Rust!

There it is — a request served by Rust compiled to a sandboxed WebAssembly component, cold-starting in microseconds. Back in the spin up terminal, you’ll also see the Handling GET request to http://localhost:3000/ line from our println! invocation, which is the crudest possible logging and completely fine for now (we get serious about observability in Part III).

An icon of a gem

You can save a few keystrokes and simply run spin b, which is a convenient alias for spin build. You can also combine compiling and running with a single spin b --up or spin up --build.

With that, you’ve made it through the fundamental Spin Workflow, summarized in the illustration below:

The Spin Workflow
Figure 3.1. The Spin Workflow

That’s your first success — your development machine is configured correctly, you’ve created your first application in Rust, walked through the Spin Workflow, and received an actual response by invoking the application with curl. No magic left unexplained.

Next, we’ll slow down a bit and dive into some essential Spin idioms.