Skip to content

muhammad-fiaz/api.zig

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

7 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

logo

Documentation Zig Version GitHub stars GitHub issues GitHub pull requests GitHub last commit License CI Supported Platforms Latest Release Sponsor GitHub Sponsors Repo Visitors

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.

✨ Features

  • πŸš€ 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

UI Preview

πŸ“¦ Installation

Add api.zig to your build.zig.zon:

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

Then in your build.zig:

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

πŸš€ Quick Start

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 run

Then visit:

⚑ Multi-Threading

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 });

πŸ“š API Reference

HTTP Methods

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);

Path Parameters

fn getUser(ctx: *api.Context) api.Response {
    const user_id = ctx.param("id") orelse "0";
    // Use user_id...
}

try app.get("/users/{id}", getUser);

Response Types

// 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");

Builder Pattern

api.Response.text("Created")
    .setStatus(.created)
    .setHeader("X-Custom", "value")
    .withCors("*");

πŸ§ͺ Testing

Run the test suite:

zig build test

🀝 Contributing

Contributions are welcome! Please read our Contributing Guide for details.

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ’– Support

If you find this project helpful, please consider:

  • ⭐ Starring the repository
  • πŸ› Reporting bugs
  • πŸ’‘ Suggesting new features
  • πŸ”€ Submitting pull requests

Releases

No releases published

Packages

No packages published

Languages