# Zinc > Zinc is a Go web framework on net/http, with an Express-style API, typed handlers and an OpenAPI 3.1 spec generated from your Go types. The core has no dependencies. - Import path: `github.com/0mjs/zinc`. It needs Go 1.25 or newer. - A handler is `func(c *zinc.Context) error`: return an error, or write a response with `c.JSON`, `c.String` and the like. - Path parameters are written `{id}`, and a rest-of-path match `{path...}`, as in net/http. Gin and Echo's `:id` and `*path` syntax is rejected at startup. - `zinc.Typed(func(c *zinc.Context, in In) (Out, error))` binds and validates `In` from the request and documents both types in the spec. - Struct tags choose where a field comes from: `path`, `query`, `header`, `cookie`, `form`, `json`, `xml`; `validate`, `default`, `enum` and `pattern` add rules. - Every app serves `/openapi.json` and `/docs`. An `App` is an `http.Handler`. - Middleware lives in `github.com/0mjs/zinc/middleware/`, one package each, such as `cors`, `logger` and `limiter`. The whole documentation as one file: https://zinc.carbonsoft.sh/llms-full.txt ## Start here - [Quickstart](https://zinc.carbonsoft.sh/guide/quickstart/): Go from an empty folder to a running Zinc API in about five minutes. - [Installation](https://zinc.carbonsoft.sh/guide/installation/): Add Zinc to a Go module, import its packages, and upgrade between versions. - [Your First Route](https://zinc.carbonsoft.sh/guide/first-route/): Build one small endpoint end to end, reading params, query values, JSON input, and returning errors. - [Coming from Gin or Echo](https://zinc.carbonsoft.sh/guide/coming-from-gin-or-echo/): The Zinc version of what you already write in Gin or Echo, side by side, and the few differences worth knowing. - [Coming from Huma or Fuego](https://zinc.carbonsoft.sh/guide/coming-from-huma-or-fuego/): Huma, Fuego and Zinc all build an OpenAPI spec from Go types. How the same API looks in each, where Zinc differs, and what the others do that Zinc doesn't. ## Essentials - [Routing](https://zinc.carbonsoft.sh/guide/routing/): Send each request to the right handler, read values from the path, and control what happens when nothing matches. - [Request Data](https://zinc.carbonsoft.sh/guide/request/): Read path parameters, query strings, headers, forms, uploaded files, raw bodies, and cancellation from a request. - [Binding](https://zinc.carbonsoft.sh/guide/binding/): Read path parameters, query strings, headers and request bodies into a typed Go struct in one call, then validate it. - [Responses and Rendering](https://zinc.carbonsoft.sh/guide/responses-and-rendering/): Send JSON, text, HTML, templates, files, downloads, streams and server-sent events, with the right status and headers. - [Errors](https://zinc.carbonsoft.sh/guide/errors/): Return errors from handlers, let domain errors choose their status, and shape every error response in one place. - [Groups and Middleware](https://zinc.carbonsoft.sh/guide/groups-and-middleware/): Write middleware, choose which requests it runs for, and see the order Zinc runs it in. - [Testing](https://zinc.carbonsoft.sh/guide/testing/): Test routes, middleware, and error handling with the standard net/http/httptest package. ## Going further - [Typed Handlers](https://zinc.carbonsoft.sh/guide/typed-handlers/): Write a handler that takes a struct and returns a value, and let Zinc bind, validate and send the JSON. - [OpenAPI](https://zinc.carbonsoft.sh/guide/openapi/): Zinc describes your API as an OpenAPI 3.1 spec from the types you already write, and serves it with a docs page. No comments to maintain, and the rules it states are the rules it enforces. - [Context](https://zinc.carbonsoft.sh/guide/context/): Use the request's Context to read input, write the response, pass values from middleware to handlers, and run background work safely. - [Templates](https://zinc.carbonsoft.sh/guide/templates/): Send HTML pages from Zinc handlers with html/template, text/template, Templ or another engine. - [Static Files](https://zinc.carbonsoft.sh/guide/static-files/): Serve a folder of assets, a single file, or files embedded in your binary. - [Cookies](https://zinc.carbonsoft.sh/guide/cookies/): Set, read, and clear cookies, and give every cookie a safe SameSite default. - [Client IP and Proxies](https://zinc.carbonsoft.sh/guide/ip-address/): Read the client's IP address correctly, both directly and behind load balancers and reverse proxies you trust. - [Configuration](https://zinc.carbonsoft.sh/guide/configuration/): Every Zinc setting, its default, and how to change it. - [Customization](https://zinc.carbonsoft.sh/guide/customization/): Swap Zinc's error format, validator, template engine or JSON library, and add body formats such as YAML. - [Zinc and net/http](https://zinc.carbonsoft.sh/guide/http-interoperability/): Run Zinc on your own http.Server, serve routes with standard handlers, wrap the app in standard middleware, and mount existing muxes. ## Middleware - [Middleware](https://zinc.carbonsoft.sh/middleware/overview/): Pick the built-in middleware your app needs, and attach it to the app, a group or a single route. - [Request ID](https://zinc.carbonsoft.sh/middleware/requestid/): Give every request an ID, or keep the one a client or proxy sent, and return it in X-Request-ID. - [Request Logger](https://zinc.carbonsoft.sh/middleware/logger/): Write one structured log line per request with slog, or send the values to your own logger. - [Recover](https://zinc.carbonsoft.sh/middleware/recover/): Turn a panic in a handler into a 500 response instead of a dropped connection. - [Secure Headers](https://zinc.carbonsoft.sh/middleware/secure/): Turn on the browser's protections against content sniffing, clickjacking and referrer leaks with a set of response headers. - [CORS](https://zinc.carbonsoft.sh/middleware/cors/): Let browser code on other origins call your API, with preflight answered for you. - [Body Limit](https://zinc.carbonsoft.sh/middleware/bodylimit/): Reject request bodies over a size you choose with 413, before your handler reads them. - [Context Timeout](https://zinc.carbonsoft.sh/middleware/timeout/): Give each request a deadline, so slow database and HTTP calls stop and the client gets a 503. - [Rate Limiter](https://zinc.carbonsoft.sh/middleware/limiter/): Refuse requests over a set rate with 429, per client IP, per API key or across the app, and cap concurrent requests. - [Health Check](https://zinc.carbonsoft.sh/middleware/healthcheck/): Answer load balancer and uptime probes with 204, or 503 when a check fails. - [API Docs](https://zinc.carbonsoft.sh/middleware/apidocs/): Serve a browsable reference page for your API's OpenAPI spec, with Scalar, Swagger UI, Stoplight Elements or ReDoc. - [Basic Auth](https://zinc.carbonsoft.sh/middleware/basicauth/): Protect routes with a username and password, using the browser's built-in login prompt. - [Key Auth](https://zinc.carbonsoft.sh/middleware/keyauth/): Require an API key on requests, read from a header, query value or cookie. - [JWT](https://zinc.carbonsoft.sh/middleware/jwtauth/): Accept only requests that carry a valid signed token, and read its claims in your handlers. - [Session](https://zinc.carbonsoft.sh/middleware/session/): Remember small values, such as a signed-in user's ID, between requests in a signed cookie. - [CSRF](https://zinc.carbonsoft.sh/middleware/csrf/): Stop other websites from sending forms and requests that ride on your users' cookies. - [Casbin Auth](https://zinc.carbonsoft.sh/middleware/casbin/): Decide whether a signed-in user may make a request, using a Casbin policy. - [Content Type](https://zinc.carbonsoft.sh/middleware/contenttype/): Reject request bodies in media types or encodings an endpoint doesn't accept. - [Headers](https://zinc.carbonsoft.sh/middleware/headers/): Add fixed headers to every response, or run different middleware when a request carries a header. - [No Cache](https://zinc.carbonsoft.sh/middleware/nocache/): Stop browsers and proxies from storing or reusing responses. - [Compress](https://zinc.carbonsoft.sh/middleware/compress/): Gzip responses for clients that accept it. - [Decompress](https://zinc.carbonsoft.sh/middleware/decompress/): Accept gzip-compressed request bodies and read them as plain ones. - [Proxy](https://zinc.carbonsoft.sh/middleware/proxy/): Forward requests to another server and send its response back to the client. - [Prometheus](https://zinc.carbonsoft.sh/middleware/prometheus/): Count requests and their durations per route, and serve them for Prometheus to scrape, with no dependencies. - [OpenTelemetry](https://zinc.carbonsoft.sh/middleware/open-telemetry/): Trace every request with OpenTelemetry's net/http instrumentation, added through app.UseHTTP. - [pprof](https://zinc.carbonsoft.sh/middleware/pprof/): Serve Go's runtime profiler behind authentication, to see where a running service spends CPU and memory. - [Body Dump](https://zinc.carbonsoft.sh/middleware/bodydump/): Hand a copy of each request and response body to your own function, for debugging, auditing or tests. - [Rewrite](https://zinc.carbonsoft.sh/middleware/rewrite/): Route a request as if it had asked for a different path, without telling the client. - [Redirect](https://zinc.carbonsoft.sh/middleware/redirect/): Send clients from old paths to new ones with a redirect status. - [Method Override](https://zinc.carbonsoft.sh/middleware/methodoverride/): Let HTML forms reach PUT, PATCH and DELETE routes by sending a POST. - [Trailing Slash](https://zinc.carbonsoft.sh/middleware/trailingslash/): Remove or add the trailing slash on request paths, or redirect clients to one form. ## Cookbook - [Cookbook](https://zinc.carbonsoft.sh/cookbook/): Complete Zinc programs for APIs, existing net/http services, operations, files, and realtime work. - [A Production Service](https://zinc.carbonsoft.sh/cookbook/production/): One complete program with what a deployed API needs: limits, timeouts, logs with request IDs, validation, a protected spec, and graceful shutdown. - [CRUD API](https://zinc.carbonsoft.sh/cookbook/crud/): Build an in-memory JSON API that creates, lists, reads, updates and deletes widgets, with 404 and 422 answers you can see. - [Typed CRUD API](https://zinc.carbonsoft.sh/cookbook/typed-crud/): Build the widgets API with typed handlers, where each function's signature says what it reads and what it returns. - [SQLite CRUD API](https://zinc.carbonsoft.sh/cookbook/sqlite-crud-api/): Build a to-do API that keeps its data in a SQLite file, with JSON in and out and 404 and 422 answers. - [Validate Input](https://zinc.carbonsoft.sh/cookbook/validation/): Check request bodies with rules written on the struct, and send a 422 that names every failing field. Then swap in go-playground/validator for more rules. - [Generate an API Client](https://zinc.carbonsoft.sh/cookbook/openapi-client/): Serve your API's OpenAPI spec from Zinc and generate a typed Go client for it with oapi-codegen. - [Verify a Signed Webhook](https://zinc.carbonsoft.sh/cookbook/signed-webhook/): Accept webhook deliveries only when their HMAC signature matches, and get 401 for anything forged or changed. - [Health and Readiness Checks](https://zinc.carbonsoft.sh/cookbook/health-readiness/): Give your orchestrator two endpoints, one that says the process is alive and one that says it can serve traffic. - [Structured Request Logs with slog](https://zinc.carbonsoft.sh/cookbook/structured-logging/): Write one JSON log line per request through Go's log/slog, with the request ID and the headers you choose. - [Graceful Shutdown](https://zinc.carbonsoft.sh/cookbook/graceful-shutdown/): Stop taking new connections on SIGTERM and let the requests already running finish before the process exits. - [Docker and Environment Config](https://zinc.carbonsoft.sh/cookbook/docker-config/): Read settings from environment variables with defaults, build a small container image, and shut down cleanly when the container stops. - [Automatic TLS](https://zinc.carbonsoft.sh/cookbook/auto-tls/): Serve Zinc over HTTPS with Let's Encrypt certificates that are fetched and renewed for you. - [HTTP/2 Server](https://zinc.carbonsoft.sh/cookbook/http2/): Serve Zinc over HTTP/2, with TLS from a certificate file or unencrypted (h2c) behind a proxy. - [Reverse Proxy](https://zinc.carbonsoft.sh/cookbook/reverse-proxy/): Forward everything under /api to another service, with the prefix removed and timeouts on the upstream connection. - [Request Timeouts](https://zinc.carbonsoft.sh/cookbook/timeout/): Give every request a 3-second budget and answer 503 when a handler runs over it. - [Rate Limiting](https://zinc.carbonsoft.sh/cookbook/rate-limiting/): Limit each client to a few requests per second and answer the rest with 429 Too Many Requests and a Retry-After header. - [Add Zinc to an Existing net/http Service](https://zinc.carbonsoft.sh/cookbook/existing-net-http-service/): Put Zinc in front of a working net/http service and add new routes without rewriting the old handlers or middleware. - [Embed Resources](https://zinc.carbonsoft.sh/cookbook/embed-resources/): Compile HTML, CSS and images into your Zinc binary, so one file is all you deploy. - [File Upload](https://zinc.carbonsoft.sh/cookbook/file-upload/): Accept a file from a multipart form, save it under a name you choose, and reject bad or oversized uploads. - [File Download](https://zinc.carbonsoft.sh/cookbook/file-download/): Send a file as a named download or show it in the browser, without letting the URL pick a path on disk. - [Single-Page App](https://zinc.carbonsoft.sh/cookbook/spa-fallback/): Serve a built React, Vue or Svelte app from Zinc, answer its client-side routes with index.html, and keep API 404s as JSON. - [CORS for a Browser Front End](https://zinc.carbonsoft.sh/cookbook/cors-browser-frontend/): Let a web page on one origin call your Zinc API on another, with preflight requests answered for you. - [Server-Rendered UI with Templ UI](https://zinc.carbonsoft.sh/cookbook/templ-ui/): Render Templ UI components from Zinc handlers, style them with Tailwind CSS, and handle a form post. - [Templated HTML + JS Page](https://zinc.carbonsoft.sh/cookbook/templated-html-js-page/): Render a page on the server with html/template, then add behavior in the browser with a small script. - [Server-Sent Events](https://zinc.carbonsoft.sh/cookbook/sse/): Push a stream of events from the server to the browser over one long-lived HTTP response. - [Streaming Response](https://zinc.carbonsoft.sh/cookbook/streaming-response/): Send a response in pieces as you produce them, so the client sees each line without waiting for the end. - [WebSocket](https://zinc.carbonsoft.sh/cookbook/websocket/): Upgrade a Zinc route to a WebSocket and send messages both ways over one connection. - [JWT](https://zinc.carbonsoft.sh/cookbook/jwt/): Protect a group of routes with signed bearer tokens, read the token's claims in a handler, and get 401 without a valid token. - [Session Login](https://zinc.carbonsoft.sh/cookbook/session-login/): Log users in with a form, keep them logged in with a signed session cookie, protect a page, and log them out. - [Custom Middleware](https://zinc.carbonsoft.sh/cookbook/middleware/): Write your own middleware that runs code before and after each handler and logs the status and size of every response. ## API Reference - [Application](https://zinc.carbonsoft.sh/api/app/): Reference for zinc.App, covering construction, routing, middleware, files, error routes, introspection, and server lifecycle. - [Config type](https://zinc.carbonsoft.sh/api/config/): Reference for zinc.Config and the default constants. - [Context](https://zinc.carbonsoft.sh/api/context/): Reference for zinc.Context, grouped by task, with every request, response, and state method. - [Binding](https://zinc.carbonsoft.sh/api/binding/): Reference for c.Bind(), struct tags, BindError, Validator, and body decoders. - [Errors](https://zinc.carbonsoft.sh/api/errors/): Reference for zinc.HTTPError, the error constructors, StatusCoder, ValidationError, and the error handlers. - [Groups](https://zinc.carbonsoft.sh/api/group/): Reference for zinc.Group, a set of routes that share a path prefix and middleware. - [Response Writer](https://zinc.carbonsoft.sh/api/response-writer/): Reference for zinc.WrapResponseWriter, which lets middleware observe the status and size of a response. ## Project - [FAQ](https://zinc.carbonsoft.sh/extra/faq/): Answers to common questions about Zinc, including how it compares, stability, performance, and compatibility. - [Benchmarks](https://zinc.carbonsoft.sh/extra/benchmarks/): How Zinc performs against Gin and Echo, and against the bare routers BunRouter and Chi, where it wins and loses, and how to run the suite yourself. - [Compatibility](https://zinc.carbonsoft.sh/extra/compatibility/): What Zinc promises between versions before 1.0, what counts as a breaking change, which Go versions are supported, and how to report a security problem. - [Upgrading to 0.7](https://zinc.carbonsoft.sh/extra/migration-0.7/): What changed in Zinc 0.7 and its patch releases. Zinc checks validate tags itself, the OpenAPI spec claims only the rules something enforces, 0.7.1 tidies the spec's names, and 0.7.2 and 0.7.3 fix bugs in binding, routing, statuses and middleware. - [Upgrading to 0.6](https://zinc.carbonsoft.sh/extra/migration-0.6/): What changed in Zinc 0.6. Every app now serves an OpenAPI spec at /openapi.json, which a private API must turn off or protect; one misuse of redirect or rewrite now fails at startup. - [Upgrading to 0.5](https://zinc.carbonsoft.sh/extra/migration-0.5/): What changed in Zinc 0.5, 0.5.1 and 0.5.2. Moving from 0.4 needs no code changes. - [Migrating to Zinc 0.4](https://zinc.carbonsoft.sh/extra/migration-0.4/): Upgrade a Zinc 0.3 application to 0.4, which reshapes the public API around shorter handlers and safer defaults. - [Migrating to Zinc 0.3](https://zinc.carbonsoft.sh/extra/migration-0.3/): Upgrade a Zinc 0.2 application to 0.3, a hardening release whose breaking changes are mostly behavioural. - [Migrating to Zinc 0.2](https://zinc.carbonsoft.sh/extra/migration-0.2/): Update route patterns, registration, native HTTP integration, and background work for Zinc 0.2.