TheDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/go-chi/chi/llms.txt
Use this file to discover all available pages before exploring further.
Router interface is the core contract every chi router implements. It embeds both http.Handler and the Routes interface, meaning any Router value can be passed directly to http.ListenAndServe and also supports route introspection. The concrete implementation is Mux, returned by chi.NewRouter(). The Routes interface, Middlewares type, and related global helpers are all defined in chi.go and documented here alongside the Router interface.
Global Functions
These package-level helpers are the main entry points for creating routers and reading URL parameters from within handlers.NewRouter
chi.go
*Mux that implements Router. This is the recommended way to create a root router.
main.go
URLParam
context.go
key from the current request’s routing context. Returns an empty string when the key is not present.
The current HTTP request carrying a chi routing context.
The named parameter key as defined in the route pattern, e.g.
"id" for the pattern /users/{id}.string
The captured URL parameter value, or an empty string if not found.
handler.go
URLParamFromCtx
context.go
URLParam but accepts a context.Context directly instead of a full *http.Request. Useful when you have already extracted the context.
A context that contains a chi routing context value (set by the Mux on every request).
The named parameter key to look up.
string
The captured URL parameter value, or an empty string if not found.
RouteContext
context.go
*Context stored in a context.Context. The chi Context holds all routing state for the in-flight request, including URL params and matched route patterns.
The request context, typically obtained via
r.Context().*Context
The chi routing context, or
nil if none is present.middleware.go
Read
RoutePattern() after calling next.ServeHTTP inside a middleware. The pattern is assembled incrementally as the request traverses sub-routers, so the value is only complete once routing finishes.NewRouteContext
context.go
*Context. Primarily useful for testing route matching with Router.Match or Router.Find without sending a real HTTP request.
*Context
A newly allocated, zeroed chi routing context.
router_test.go
Router Interface
chi.go
Router embeds Routes, so every method on Routes is also available on any Router. See the Routes reference for Routes(), Middlewares(), Match(), and Find().
Routes Interface
Routes is the read-only introspection interface embedded by Router. It is also implemented by mounted sub-routers and used by the docgen sub-package to enumerate all registered routes.
chi.go
Middlewares Type
chi.go
Middlewares is a named slice type for standard middleware functions. It is returned by Router.Middlewares() and accepted wherever a middleware stack is expected. It also carries Handler and HandlerFunc methods (from chain.go) so you can compose a complete http.Handler from a middleware stack without involving a router at all — see Chain.
example.go
RegisterMethod
tree.go
Method, MethodFunc, and route matching. Call this once at program startup before registering any routes that use the custom method. The method string is normalised to uppercase. Registering a method that is already known is a no-op. Panics if the maximum number of supported methods is exceeded.
The HTTP method string to register, e.g.
"PROPFIND". Normalised to uppercase.main.go
Middleware Methods
Use — append middleware to the router stack
Use — append middleware to the router stack
chi.go
One or more standard middleware functions. Each wraps the next handler in the chain.
main.go
With — inline middleware for a single endpoint
With — inline middleware for a single endpoint
chi.go
Use, it can be called at any point and only affects the specific route(s) registered on the returned router.Middleware to apply only to routes registered on the returned inline router.
Router
A new inline router with the combined middleware stack.
main.go
Grouping Methods
Group — inline sub-router on the same path
Group — inline sub-router on the same path
chi.go
A callback that receives the new inline router and registers routes on it.
Router
The newly created inline router.
routes.go
Route — mount a sub-router at a path prefix
Route — mount a sub-router at a path prefix
chi.go
*Mux and mounts it at pattern. This is a shorthand for creating a router, registering routes in fn, and calling Mount. The sub-router gets its own independent middleware stack.The path prefix at which to mount the new sub-router. Must start with
/.A callback that receives the new sub-router. Routes registered here are scoped under
pattern.Router
The newly created sub-router.
routes.go
Mount — attach any http.Handler as a sub-router
Mount — attach any http.Handler as a sub-router
chi.go
http.Handler — including another chi Router — under pattern. Mount sets a wildcard at pattern/* so the attached handler receives all requests whose path starts with pattern. The route path is adjusted to strip the matched prefix before delegating.The path prefix. Requests to
pattern, pattern/, and pattern/* are all forwarded.The handler or sub-router to attach. Must not be
nil.main.go
Route Registration Methods
Handle and HandleFunc — match all HTTP methods
Handle and HandleFunc — match all HTTP methods
chi.go
pattern that matches all HTTP methods. HandleFunc is a convenience wrapper that accepts a http.HandlerFunc instead of a http.Handler.Handle also supports an optional method prefix separated by whitespace, e.g. "GET /path", which makes it equivalent to Method("GET", "/path", h).The URL pattern. Must begin with
/.The handler to invoke when the pattern matches.
routes.go
Method and MethodFunc — match a specific HTTP method
Method and MethodFunc — match a specific HTTP method
chi.go
pattern that only matches the given method string. The method is case-insensitive and must be one of the standard HTTP methods. An unrecognised method causes a panic.The HTTP method string, e.g.
"GET", "POST". Case-insensitive.The URL pattern. Must begin with
/.The handler to invoke on a match.
routes.go
HTTP Verb Shortcuts
Each of the following methods registers a route for the corresponding HTTP method. They all share the same signature:chi.go
The URL pattern, starting with
/. Supports named parameters ({id}), regex parameters ({id:\\d+}), and wildcards (*).The handler function to execute when the method and pattern match.
routes.go
Error Handler Methods
NotFound — custom 404 handler
NotFound — custom 404 handler
chi.go
http.NotFound (status 404 with a plain text body). When set on an inline group, the handler is propagated to the parent mux.The handler to invoke for unmatched routes.
main.go
MethodNotAllowed — custom 405 handler
MethodNotAllowed — custom 405 handler
chi.go
Allow header to the list of permitted methods. Propagates to sub-routers like NotFound.The handler to invoke when the method is not allowed for the matched path.
main.go