API Reference

Chi

Lightweight idiomatic Go HTTP router with middleware composition, sub-routers, and full net/http compatibility.

Official Documentation ↗
CompatibilityThis documentation covers version v5.5.0. Check the official docs for the latest API surface.

Installation

gogo get github.com/go-chi/chi/v5

Methods

Chi is a small router for Go that stays inside the standard library’s HTTP model. Routers implement http.Handler, middleware follows the func(http.Handler) http.Handler shape, and sub-routers compose without a separate framework runtime.

Overview

A chi router maps method and path patterns to handlers. Named parameters are read with chi.URLParam, and route groups inherit middleware from their parent. Because everything is net/http compatible, handlers can be tested with httptest and existing middleware keeps working. The package also ships a middleware collection for logging, recovery, timeouts, and request IDs.

Installation

Run go get github.com/go-chi/chi/v5 inside a Go module. The router has no external dependency and integrates with the standard net/http server. Middleware lives in the github.com/go-chi/chi/v5/middleware package. Create the router once, register middleware before routes, and pass it to http.ListenAndServe or a configured http.Server.

Core API walkthrough

chi.NewRouter returns a *chi.Mux that satisfies http.Handler. r.Route creates a nested router that shares a path prefix. chi.URLParam extracts named parameters inside a handler. r.With applies middleware to a single route or subtree, which is how authentication is limited to protected endpoints. r.Mount attaches any http.Handler, including static file servers or a separate API router.

Pitfalls

URLParam returns an empty string outside its route context, and values are strings, so parse and validate them. Middleware registered after routes does not apply to those routes. Path parameters do not match slashes, so use a wildcard for multi-segment paths and guard against path traversal when serving files.

Verification

Write httptest cases that exercise each route, including a missing parameter and an unauthorized request. Check that middleware order produces the expected log fields, and curl a nested route to confirm the prefix and parameter are parsed as intended.

FAQ

How is Chi different from gorilla/mux?
Chi focuses on composable middleware, sub-routers, and zero allocation routing while staying a plain http.Handler. gorilla/mux offers more matching features and is maintained again, but both cover standard REST routing.
Why is chi.URLParam returning an empty string?
The request did not pass through the route that declares the parameter, the key spelling differs from the pattern, or a sub-router was mounted with a mismatched prefix. URL parameters exist only within their route context.
Does Chi support trailing slashes and method fallbacks?
Yes. middleware.RedirectSlashes or StripSlashes normalizes paths, and r.MethodNotAllowed and r.NotFound customize fallback responses for unmatched methods and routes.