How much code does it take to build a REST API with concurrent request handling, in-memory storage, and JSON responses? In Wyn: 93 lines. 71KB binary. Zero dependencies.
The code
wyn
var items = HashMap.new()
var next_id = 1
fn handle(method: string, path: string, body: string, fd: int) {
if method == "GET" && path == "/api/items" {
var keys = items.keys()
var parts = []
for k in keys { parts.push(items.get(k)) }
Http.respond(fd, 200, "application/json", "[${parts.join(",")}]")
return
}
if method == "POST" && path == "/api/items" {
var id = next_id
next_id += 1
var json = "{\"id\":${id},\"name\":\"${body.trim()}\"}"
items.set("${id}", json)
Http.respond(fd, 201, "application/json", json)
return
}
Http.respond(fd, 404, "application/json", "{\"error\":\"not found\"}")
}
fn main() -> int {
var server = Http.serve(8080)
println("API on http://localhost:8080")
while 1 == 1 {
var req = Http.accept(server)
var method = req.split_at("|", 0)
var path = req.split_at("|", 1)
var body = req.split_at("|", 2)
var fd = req.split_at("|", 3).to_int()
spawn handle(method, path, body, fd)
}
return 0
}(Simplified from the full 93-line version which includes GET by ID, DELETE, and health check.)
What makes this possible
Http.serve/Http.accept— built into the stdlib, no framework neededspawn handle(...)— each request runs in a green threadHashMap— built-in key-value store"${expr}"— string interpolation builds JSON inline- No imports — everything is available out of the box
Try it
bash
wyn new myapi --api
cd myapi
wyn runThe --api template generates a full CRUD API with SQLite persistence.
Comparison
| Wyn | Go | Node.js | |
|---|---|---|---|
| Lines | 93 | ~120 | ~80 |
| Binary | 71KB | 6.2MB | N/A (50MB runtime) |
| Dependencies | 0 | 1 (net/http) | 1+ (express) |
| Concurrency | spawn (green threads) | goroutines | async/await |