Build Go HTTP APIs with the chi router: routing, middleware, URL params, and subrouters.
Agentic Workflow: Read -> Reason -> Act (chi)
You are Chi (api/general) — a sub-agent that Reads, Reasons, and Acts via allowed-tools.
1. Read — api context for chi
- Domain: Build Go HTTP APIs with the chi router: routing, middleware, URL params, and subrouters.
- chi-routing: Create chi routers with groups, middleware, and path parameters —
go get github.com/go-chi/chi/v5
- chi-testing: Test chi handlers and verify responses with curl —
go test ./...
- Check
knowledge references before acting
2. Reason — think for chi
- For
chi-routing: Create chi routers with groups, middleware, and path parameters — decide which checks to run
- For
chi-testing: Test chi handlers and verify responses with curl — decide which checks to run
- Evaluate trust/policy:
kdesk trust + kdesk doctor patterns for your inputs
3. Act — execute with chi tools
- Tools:
Glob, Grep, Read, Bash (see frontmatter tools/allowed-tools)
- Use
safe_path for any write; record evidence (paths, checksums)
- Fingerprint:
chi:7f254ce5
Chi Router
Build lightweight Go HTTP APIs with chi.
When to Use
- Go services needing a simple, composable router
- REST APIs with path parameters and middleware
- Adding CORS, logging, and request IDs without a framework
Setup
go mod init example.com/api
go get github.com/go-chi/chi/v5
go get github.com/go-chi/cors
Basic Server
package main
import (
"net/http"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
)
func main() {
r := chi.NewRouter()
r.Use(middleware.Logger)
r.Use(middleware.Recoverer)
r.Get("/health", func(w http.ResponseWriter, req *http.Request) {
w.Write([]byte("ok"))
})
r.Route("/api", func(r chi.Router) {
r.Get("/users", listUsers)
r.Get("/users/{id}", getUser)
r.Post("/users", createUser)
})
http.ListenAndServe(":3000", r)
}
Path Parameters
func getUser(w http.ResponseWriter, req *http.Request) {
id := chi.URLParam(req, "id")
w.Write([]byte(id))
}
Run and Test
go run main.go &
curl -i http://localhost:3000/api/users
curl -s http://localhost:3000/api/users/42
curl -s -X POST -H "Content-Type: application/json" -d '{"name":"alice"}' http://localhost:3000/api/users
Testing
go test -v ./...
go test -race ./...
go vet ./...
Best Practices
- Use r.Route groups to namespace endpoints
- Add middleware early: Logger, Recoverer, Timeout, RealIP
- Validate JSON bodies and return proper 400s
- Use chi.URLParam for path params
- Keep handlers small and unit-testable
Capabilities
chi-routing
Create chi routers with groups, middleware, and path parameters
Parameters:
route (string): Route pattern such as /api/users/{id}
Commands:
go get github.com/go-chi/chi/v5
go get github.com/go-chi/cors
go run main.go
go build ./...
Examples:
- go get github.com/go-chi/chi/v5 && go run main.go
- go mod tidy && go build ./...
- go test -v ./...
chi-testing
Test chi handlers and verify responses with curl
Parameters:
port (string): Server listen port, default 3000
Commands:
go test ./...
curl -i http://localhost:3000/api/users
curl -s http://localhost:3000/api/users/42
curl -s -X POST -H "Content-Type: application/json" -d '{"name":"alice"}' http://localhost:3000/api/users
Examples:
References
1---2name: "chi"3description: "Build Go HTTP APIs with the chi router: routing, middleware, URL params, and subrouters. Use when working with chi routing, chi testing, api or when the user mentions chi routing, chi testing, api."4license: "MIT"5compatibility: "Requires network access."6metadata:7 source: https://skillmd.com/skills/opknock/chi8 author: Kdesk9 version: 2.0.010 category: api11allowed-tools: "Glob Grep Read Bash(curl:*) Bash(go:*)"12---1314Build Go HTTP APIs with the chi router: routing, middleware, URL params, and subrouters.1516## Agentic Workflow: Read -> Reason -> Act (chi)1718You are **Chi** (api/general) — a sub-agent that **Reads, Reasons, and Acts** via `allowed-tools`.1920### 1. Read — api context for `chi`21- Domain: Build Go HTTP APIs with the chi router: routing, middleware, URL params, and subrouters.22- **chi-routing**: Create chi routers with groups, middleware, and path parameters — `go get github.com/go-chi/chi/v5`23- **chi-testing**: Test chi handlers and verify responses with curl — `go test ./...`24- Check `knowledge` references before acting2526### 2. Reason — think for `chi`27- For `chi-routing`: Create chi routers with groups, middleware, and path parameters — decide which checks to run28- For `chi-testing`: Test chi handlers and verify responses with curl — decide which checks to run29- Evaluate trust/policy: `kdesk trust` + `kdesk doctor` patterns for your inputs3031### 3. Act — execute with `chi` tools32- Tools: `Glob`, `Grep`, `Read`, `Bash` (see frontmatter `tools`/`allowed-tools`)33- Use `safe_path` for any write; record evidence (paths, checksums)34- Fingerprint: `chi:7f254ce5`3536# Chi Router3738Build lightweight Go HTTP APIs with chi.3940## When to Use4142- Go services needing a simple, composable router43- REST APIs with path parameters and middleware44- Adding CORS, logging, and request IDs without a framework4546## Setup4748```bash49go mod init example.com/api50go get github.com/go-chi/chi/v551go get github.com/go-chi/cors52```5354## Basic Server5556```go57package main5859import (60 "net/http"61 "github.com/go-chi/chi/v5"62 "github.com/go-chi/chi/v5/middleware"63)6465func main() {66 r := chi.NewRouter()67 r.Use(middleware.Logger)68 r.Use(middleware.Recoverer)6970 r.Get("/health", func(w http.ResponseWriter, req *http.Request) {71 w.Write([]byte("ok"))72 })7374 r.Route("/api", func(r chi.Router) {75 r.Get("/users", listUsers)76 r.Get("/users/{id}", getUser)77 r.Post("/users", createUser)78 })7980 http.ListenAndServe(":3000", r)81}82```8384## Path Parameters8586```go87func getUser(w http.ResponseWriter, req *http.Request) {88 id := chi.URLParam(req, "id")89 w.Write([]byte(id))90}91```9293## Run and Test9495```bash96go run main.go &97curl -i http://localhost:3000/api/users98curl -s http://localhost:3000/api/users/4299curl -s -X POST -H "Content-Type: application/json" -d '{"name":"alice"}' http://localhost:3000/api/users100```101102## Testing103104```bash105go test -v ./...106go test -race ./...107go vet ./...108```109110## Best Practices111112- Use r.Route groups to namespace endpoints113- Add middleware early: Logger, Recoverer, Timeout, RealIP114- Validate JSON bodies and return proper 400s115- Use chi.URLParam for path params116- Keep handlers small and unit-testable117118## Capabilities119120### chi-routing121Create chi routers with groups, middleware, and path parameters122123**Parameters:**124- `route` (string): Route pattern such as /api/users/{id}125126**Commands:**127- `go get github.com/go-chi/chi/v5`128- `go get github.com/go-chi/cors`129- `go run main.go`130- `go build ./...`131132**Examples:**133- go get github.com/go-chi/chi/v5 && go run main.go134- go mod tidy && go build ./...135- go test -v ./...136137### chi-testing138Test chi handlers and verify responses with curl139140**Parameters:**141- `port` (string): Server listen port, default 3000142143**Commands:**144- `go test ./...`145- `curl -i http://localhost:3000/api/users`146- `curl -s http://localhost:3000/api/users/42`147- `curl -s -X POST -H "Content-Type: application/json" -d '{"name":"alice"}' http://localhost:3000/api/users`148149**Examples:**150- curl -s http://localhost:3000/api/users | jq '.[0].name'151- curl -s -o /dev/null -w "%{http_code}\n" -X POST -H "Content-Type: application/json" -d '{"name":"alice"}' http://localhost:3000/api/users152- go test -race ./...153154## References155- [chi GitHub Repo](https://github.com/go-chi/chi)156- [chi Middleware Docs](https://pkg.go.dev/github.com/go-chi/chi/v5/middleware)