Standard Library

🌐 std.http

HTTP server, routing, and JSON responses — powered by Axum.

The foundation of every AID application. Create servers, define routes with handlers, and return JSON or text responses — all in a few lines.

API Reference

Every function in the std.http module.

http.new(port: int) → Server

Creates a new HTTP server bound to the specified port. Does not start listening until server.start() is called.

server := http.new(port: 8080)

server.get(path: string) => fn(req) → Response

Registers a GET route handler. The handler receives a request object and must return a Response.

server.get("/users") => fn(req) -> Response {
    Response.json({ users: [] })
}

server.post(path: string) => fn(req) → Response

Registers a POST route handler. Request body is available via req.body.

server.post("/users") => fn(req) -> Response {
    Response.json({ created: req.body.name })
}

server.put(path: string) => fn(req) → Response

Registers a PUT route handler for updates.

server.delete(path: string) => fn(req) → Response

Registers a DELETE route handler for resource removal.

Response.json(data: object) → Response

Returns a JSON response with Content-Type: application/json. Accepts any AID object/map literal.

Response.text(body: string) → Response

Returns a plain text response with Content-Type: text/plain.

server.start()

Starts the HTTP server and begins listening for connections. Blocks the main thread.

Complete Example

A full HTTP API with multiple routes and response types.

module main

use std.http

fn main() {
    server := http.new(port: 8080)

    server.get("/") => fn(req) -> Response {
        Response.json({
            service: "My API",
            version: "1.0.0"
        })
    }

    server.get("/health") => fn(req) -> Response {
        Response.json({ status: "ok" })
    }

    server.post("/echo") => fn(req) -> Response {
        Response.text(req.body)
    }

    server.start()
}

Generated Rust

What the AID compiler produces — an Axum application.

Transpiled Output (simplified)
use axum::{Router, routing::{get, post}, Json, response::IntoResponse};
use serde_json::json;

// server.get("/") => handler
async fn handle_root() -> impl IntoResponse {
    Json(json!({
        "service": "My API",
        "version": "1.0.0"
    }))
}

// http.new(port: 8080) + server.start()
#[tokio::main]
async fn main() {
    let app = Router::new()
        .route("/", get(handle_root))
        .route("/health", get(handle_health))
        .route("/echo", post(handle_echo));

    let listener = tokio::net::TcpListener::bind("0.0.0.0:8080")
        .await.unwrap();
    axum::serve(listener, app).await.unwrap();
}