High-performance, multi-threaded HTTP API framework for Zig - build blazing-fast APIs with compile-time safety.
π Documentation | API Reference | Quick Start | Contributing
Note: This Project is in active development. Currently, Breaking changes may occur in every commit. Use at your own risk.
- π High Performance - Zero runtime reflection, compile-time route validation
- β‘ Multi-Threaded - Configurable thread pools for concurrent request handling
- π Automatic OpenAPI - Auto-generated OpenAPI 3.1 specification
- π¨ Swagger UI & ReDoc - Built-in interactive API documentation (Swagger UI 5.31.0, ReDoc 2.5.2)
- π Type Safety - Full compile-time type checking for routes and handlers
- π Concurrency - Thread-safe request handling with atomic counters
- π― GraphQL Support - Built-in GraphQL Playground with GraphiQL 3.8.3
- π¦ Zero Dependencies - Pure Zig implementation
- π Cross-Platform - Linux, Windows, macOS
|
|
|
|
Add api.zig to your build.zig.zon:
zig fetch --save https://github.com/muhammad-fiaz/api.zig/archive/refs/heads/main.tar.gzThen in your build.zig:
const api = b.dependency("api", .{
.target = target,
.optimize = optimize,
});
exe.root_module.addImport("api", api.module("api"));const std = @import("std");
const api = @import("api");
fn hello() api.Response {
return api.Response.jsonRaw("{\"message\":\"Hello, World!\"}");
}
fn getUser(ctx: *api.Context) api.Response {
const id = ctx.param("id") orelse "0";
_ = id;
return api.Response.jsonRaw("{\"id\":1,\"name\":\"John Doe\"}");
}
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
var app = api.App.init(allocator, .{
.title = "My API",
.version = "1.0.0",
});
defer app.deinit();
try app.get("/", hello);
try app.get("/users/{id}", getUser);
// Run with 4 worker threads
try app.run(.{ .port = 8000, .num_threads = 4 });
}Run your server:
zig build runThen visit:
- http://localhost:8000/ β Your API
- http://localhost:8000/docs β Swagger UI
- http://localhost:8000/redoc β ReDoc
api.zig supports configurable thread pools for maximum performance:
// Single-threaded mode (default)
try app.run(.{ .port = 8000 });
// Multi-threaded with 4 workers
try app.run(.{ .port = 8000, .num_threads = 4 });
// Auto-detect CPU count
try app.run(.{ .port = 8000, .num_threads = null });try app.get("/resource", handler);
try app.post("/resource", handler);
try app.put("/resource/{id}", handler);
try app.delete("/resource/{id}", handler);
try app.patch("/resource/{id}", handler);fn getUser(ctx: *api.Context) api.Response {
const user_id = ctx.param("id") orelse "0";
// Use user_id...
}
try app.get("/users/{id}", getUser);// JSON response
api.Response.jsonRaw("{\"key\":\"value\"}");
// Text response
api.Response.text("Hello, World!");
// HTML response
api.Response.html("<h1>Hello</h1>");
// Error response
api.Response.err(.not_found, "{\"error\":\"Not found\"}");
// Redirect
api.Response.redirect("/new-location");api.Response.text("Created")
.setStatus(.created)
.setHeader("X-Custom", "value")
.withCors("*");Run the test suite:
zig build testContributions are welcome! Please read our Contributing Guide for details.
This project is licensed under the MIT License - see the LICENSE file for details.
If you find this project helpful, please consider:
- β Starring the repository
- π Reporting bugs
- π‘ Suggesting new features
- π Submitting pull requests



