Skip to main content

Serve an HTTP API

Server-side syntax is part of the language. Routes are declarations, not middleware registrations.

Define a server

server Api port=8080:
    get "/health":
        respond {status: "ok"}

    get "/users":
        json(db.query("SELECT * FROM users"))

    post "/echo":
        let body = request.body
        respond body

get, post, put, delete, and patch are all available. Handlers are ordinary Lux — the last expression is the response.

Run it

lux run server.lux          # interpreter, fast iteration
lux build --release         # native binary via Cranelift for deployment

The deployed artifact is compiled machine code — no JIT warmup on the hot path, no garbage-collector pauses (memory is reference-counted).

Energy in the response

A response can carry an X-Energy-Joules header reporting the energy attributed to the handler. Attribution at handler granularity uses Lux’s calibrated cost model rather than a per-handler hardware read — see read an energy receipt for exactly what is measured versus modeled.

Why no framework

The HTTP server, router, and JSON handling ship in the standard library — 1,723 pure-Rust modules compiled ahead of time. There is no node_modules, no supply-chain surface, and no version resolution between your routes and your runtime.

Try it: the HTTP server example parses and analyzes in the playground, no install needed.