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
- Host: GitHub
- URL: https://github.com/muhammad-fiaz/api.zig
- Owner: muhammad-fiaz
- License: mit
- Created: 2025-12-15T14:22:30.000Z (4 months ago)
- Default Branch: main
- Last Pushed: 2025-12-16T13:34:36.000Z (4 months ago)
- Last Synced: 2026-04-22T15:40:31.807Z (5 days ago)
- Topics: 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
- Language: Zig
- Homepage: https://muhammad-fiaz.github.io/api.zig/
- Size: 4.56 MB
- Stars: 11
- Watchers: 0
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README

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