DEV Community

Alex Spinov
Alex Spinov

Posted on

Spin Has a Free API: Build WebAssembly Microservices in 30 Seconds

Docker containers start in seconds. WebAssembly modules start in milliseconds. Spin makes building them as easy as Express.

What Is Spin?

Spin by Fermyon is a framework for building WebAssembly-based microservices. Write in Rust, Go, Python, JavaScript, or C# — compile to Wasm — deploy anywhere.

spin new -t http-rust my-app
cd my-app
spin build
spin up
# HTTP server running on http://localhost:3000
Enter fullscreen mode Exit fullscreen mode

The Code

// Rust
use spin_sdk::http::{IntoResponse, Request, Response};
use spin_sdk::http_component;

#[http_component]
fn handle_request(req: Request) -> anyhow::Result<impl IntoResponse> {
    Ok(Response::builder()
        .status(200)
        .header("content-type", "application/json")
        .body(r#"{"message": "Hello from WebAssembly!"}"#)
        .build())
}
Enter fullscreen mode Exit fullscreen mode
// JavaScript/TypeScript
import { HandleRequest, HttpRequest, HttpResponse } from "@fermyon/spin-sdk"

export const handleRequest: HandleRequest = async function(request: HttpRequest): Promise<HttpResponse> {
  return {
    status: 200,
    headers: { "content-type": "application/json" },
    body: JSON.stringify({ message: "Hello from Wasm!" })
  }
}
Enter fullscreen mode Exit fullscreen mode
# Python
from spin_sdk.http import IncomingHandler, Request, Response

class IncomingHandler(IncomingHandler):
    def handle_request(self, request: Request) -> Response:
        return Response(200, {"content-type": "application/json"}, b'{"message": "Hello!"}')
Enter fullscreen mode Exit fullscreen mode

Why WebAssembly Microservices

1. Cold start: <1ms — vs Docker 500ms-5s, Lambda 100ms-1s

2. Memory: 1-10MB — vs Docker container 50-500MB

3. Security — Wasm modules are sandboxed. No file system access, no network access unless explicitly granted.

4. Language agnostic — Rust, Go, Python, JS, C#, C++ → all compile to the same Wasm format

5. Portable — Wasm runs anywhere. No "works on my machine."

Key-Value Store

use spin_sdk::key_value::Store;

let store = Store::open_default()?;
store.set("user:123", b"Alice")?;
let name = store.get("user:123")?;
Enter fullscreen mode Exit fullscreen mode

Deploy to Fermyon Cloud

spin cloud deploy
# Deployed in seconds. Global edge. Free tier.
Enter fullscreen mode Exit fullscreen mode

Building microservices? Check out my developer tools or email spinov001@gmail.com.

Top comments (0)