This guide walks you through creating a working HTTP service with chi from scratch. By the end you will have a server running onDocumentation 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.
localhost:3333 that handles requests with structured middleware and dynamic URL parameters. The only prerequisite is a working Go 1.23+ installation and a Go module (go mod init) for your project.
Steps
Install chi
Add chi to your Go module. Run the following command inside your project directory:This fetches chi v5 and records it in your
go.mod file. chi has zero external dependencies, so nothing else will be pulled in beyond chi itself.Make sure you have initialized a Go module first. If you haven’t, run
go mod init your/module/name before the go get command.Write a hello-world server
Create Three built-in middlewares are applied to every request:
main.go in your project directory with the following content. This is the canonical hello-world example drawn directly from chi’s own _examples/hello-world/ directory:main.go
- RequestID — injects a unique request ID into the context and response headers.
- Logger — logs the start and end of each request with the elapsed processing time.
- Recoverer — catches any panics in downstream handlers, prints the stack trace, and returns a
500response instead of crashing the server.
Run the server
Start the server with:You should see the Logger middleware print a line for each incoming request. In a separate terminal, test the endpoint:Expected response:The Logger middleware will output something similar to:
Add a URL parameter route
Extend your server to handle a dynamic URL segment. chi uses
{paramName} syntax in route patterns and provides chi.URLParam to read the value at runtime.chi.URLParam(r, "name") reads the {name} segment from the URL. The value is stored on the request’s context.Context by chi’s router — no global state is involved.Explore a richer example
For a more complete picture, here is a server with route groups, a middleware stack, and a Key patterns demonstrated here:
Timeout. This pattern mirrors real-world chi services:main.go
r.Route(...)creates an inline sub-router at a path prefix with its own middleware stack.- Middleware added inside
r.Route(...)only runs for requests matching that group. middleware.Timeoutsignals throughctx.Done()when the deadline is exceeded, allowing handlers to abort cleanly.
What’s next?
Routing Overview
Deep dive into URL patterns, wildcards, regexp params, and sub-router mounting.
Middleware Overview
Browse chi’s full built-in middleware suite and learn how to write your own.
REST API Guide
Build a complete CRUD REST API with chi, including context-based resource loading.
Installation
Go module setup, version requirements, and upgrading from chi v4.