An open API service indexing awesome lists of open source software.

https://github.com/muhammad-fiaz/api.zig

High-performance, multi-threaded HTTP API framework for Zig
https://github.com/muhammad-fiaz/api.zig

api api-package api-zig graphql http-zig resful resful-api zig zig-api zig-api-package zig-http zig-lang zig-library zig-package zig-restful

Last synced: 1 day ago
JSON representation

High-performance, multi-threaded HTTP API framework for Zig

Awesome Lists containing this project

README

          

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`:

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

## ๐Ÿš€ Quick Start

```zig
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:

```bash
zig build run
```

Then visit:

- **http://localhost:8000/** โ€” Your API
- **http://localhost:8000/docs** โ€” Swagger UI
- **http://localhost:8000/redoc** โ€” ReDoc

## โšก Multi-Threading

api.zig supports configurable thread pools for maximum performance:

```zig
// 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

```zig
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

```zig
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

```zig
// JSON response
api.Response.jsonRaw("{\"key\":\"value\"}");

// Text response
api.Response.text("Hello, World!");

// HTML response
api.Response.html("

Hello

");

// Error response
api.Response.err(.not_found, "{\"error\":\"Not found\"}");

// Redirect
api.Response.redirect("/new-location");
```

### Builder Pattern

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

## ๐Ÿงช Testing

Run the test suite:

```bash
zig build test
```

## ๐Ÿค Contributing

Contributions are welcome! Please read our [Contributing Guide](CONTRIBUTING.md) for details.

## ๐Ÿ“„ License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

## ๐Ÿ’– Support

If you find this project helpful, please consider:

- โญ Starring the repository
- ๐Ÿ› Reporting bugs
- ๐Ÿ’ก Suggesting new features
- ๐Ÿ”€ Submitting pull requests