Skip to content

Getting Started

Welcome to api.zig, a high-performance, multi-threaded HTTP API framework for Zig.

Prerequisites

  • Zig 0.15.0 or later

Installation

Add api.zig to your project:

bash
zig fetch --save https://github.com/muhammad-fiaz/api.zig/archive/refs/heads/main.tar.gz

Then in your build.zig:

zig
const api = b.dependency("api", .{
    .target = target,
    .optimize = optimize,
});
exe.root_module.addImport("api", api.module("api"));

Your First API

Create a simple API server:

zig
const std = @import("std");
const api = @import("api");

fn hello() api.Response {
    return api.Response.jsonRaw("{\"message\":\"Hello, World!\"}");
}

pub fn main() !void {
    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    defer _ = gpa.deinit();
    const allocator = gpa.allocator();

    var app = try api.App.init(allocator, .{
        .title = "My First API",
        .version = "1.0.0",
    });
    defer app.deinit();

    try app.get("/", hello);
    try app.run(.{ .port = 8000 });
}

Run Your Server

bash
zig build run

Output:

[OK] http://127.0.0.1:8000
[INFO]   /docs   - Interactive API Documentation
[INFO]   /redoc  - API Reference

Visit:

App Init Options

OptionTypeDefaultDescription
title[]const u8"API"OpenAPI document title
version[]const u8"0.0.0"API version string
description[]const u8""API description

Server Run Options

OptionTypeDefaultDescription
portu168000Port to listen on
address[]const u8"127.0.0.1"Bind address
num_threads?u32CPU countWorker thread count
enable_access_logbooltrueEnable request logging
auto_portbooltrueAuto-find available port
max_port_attemptsu16100Max ports to try

Features

Auto Port Selection

If port 8000 is busy, the server automatically finds an available port:

zig
try app.run(.{ 
    .port = 8000,
    .auto_port = true,      // Default: true
    .max_port_attempts = 50, // Try up to 50 ports
});

Multi-Threading

Automatically scales to CPU cores:

zig
try app.run(.{ 
    .num_threads = null,  // Auto-detect CPU cores (default)
});

// Or specify explicitly
try app.run(.{ 
    .num_threads = 8,     // Use 8 worker threads
});

Cross-Platform Support

api.zig works on:

  • Windows - Full support with Winsock2
  • Linux - Native POSIX sockets with epoll
  • macOS - Native POSIX sockets with kqueue

Next Steps

Released under the MIT License.