Getting started with Echo in Go: routes, JSON handlers and middleware

Echo is a small web framework for Go. It adds a router with path parameters, request binding, response helpers and a middleware chain on top of net/http, without much else. That makes it a reasonable middle ground between the standard library’s http.ServeMux and heavier frameworks.
This post builds a small server with one GET route, one JSON POST route, and Echo’s logging and recovery middleware.
Set up the project
Section titled “Set up the project”Echo v5 requires Go 1.25 or later. Create a module and add Echo:
mkdir echo-demo && cd echo-demogo mod init example.com/echo-demogo get github.com/labstack/echo/v5A minimal server
Section titled “A minimal server”Create main.go:
package main
import ( "log/slog" "net/http"
"github.com/labstack/echo/v5")
func main() { e := echo.New()
e.GET("/", func(c *echo.Context) error { return c.String(http.StatusOK, "Hello, Echo!") })
if err := e.Start(":8080"); err != nil { slog.Error("failed to start server", "error", err) }}Run it with go run . and open http://localhost:8080 to see Hello, Echo!.
Every Echo handler returns an error. Returning one hands it to Echo’s central error handler, which turns it into an HTTP response, so you don’t write the error response in every handler.
Also check the error from e.Start. Ignoring it, as many examples do, hides problems like the port already being in use.
A JSON POST handler
Section titled “A JSON POST handler”Add a User type and a handler that binds the request body to it:
type User struct { Name string `json:"name"` Email string `json:"email"`}
func createUser(c *echo.Context) error { var u User if err := c.Bind(&u); err != nil { return err } if u.Name == "" || u.Email == "" { return echo.NewHTTPError(http.StatusBadRequest, "name and email are required") } return c.JSON(http.StatusCreated, u)}Register it in main, before e.Start:
e.POST("/users", createUser)c.Bind decodes the body based on the Content-Type header (JSON, XML or form data) and can also fill fields from path and query parameters tagged with param and query. If decoding fails it returns an error that Echo turns into a 400 Bad Request. Binding doesn’t validate anything, which is why the handler checks the fields itself; for larger structs, plug a validator such as go-playground/validator into e.Validator.
Test it with curl:
curl -X POST http://localhost:8080/users \ -H 'Content-Type: application/json' \ -d '{"name":"Ada","email":"ada@example.com"}'The response is the same user as JSON with status 201 Created.
Middleware
Section titled “Middleware”Middleware wraps handlers to run code before or after them. Echo ships common ones in the middleware package. Here is the complete main.go with request logging and panic recovery added:
package main
import ( "log/slog" "net/http"
"github.com/labstack/echo/v5" "github.com/labstack/echo/v5/middleware")
type User struct { Name string `json:"name"` Email string `json:"email"`}
func createUser(c *echo.Context) error { var u User if err := c.Bind(&u); err != nil { return err } if u.Name == "" || u.Email == "" { return echo.NewHTTPError(http.StatusBadRequest, "name and email are required") } return c.JSON(http.StatusCreated, u)}
func main() { e := echo.New()
e.Use(middleware.RequestLogger()) // logs each request with log/slog e.Use(middleware.Recover()) // turns panics into 500 responses instead of crashing
e.GET("/", func(c *echo.Context) error { return c.String(http.StatusOK, "Hello, Echo!") }) e.POST("/users", createUser)
if err := e.Start(":8080"); err != nil { slog.Error("failed to start server", "error", err) }}Middleware registered with e.Use runs for every route, in the order it was added. Restart the server and each request now produces a log line.
Other built-in middleware worth knowing: CORS, BodyLimit, Gzip, RateLimiter and ContextTimeout. You can also apply middleware to a single route or to a group created with e.Group("/api").
Gotchas
Section titled “Gotchas”- In v4, handlers take
echo.Context(an interface); in v5 they take*echo.Context. Code and examples written for one won’t compile against the other, so check which major version a tutorial targets. - v4’s
middleware.Logger()does not exist in v5; usemiddleware.RequestLogger(). - Be careful with
c.RealIP()and the IP-based rate limiter behind proxies. Configuree.IPExtractorto match your proxy setup, or clients can spoof their IP withX-Forwarded-For.
Further reading
Section titled “Further reading”© 2026 David Li · Home · Blog · RSS ·Classic site · Built with Astro, Starlight and three.js · Logos:Font Awesome Free (CC BY 4.0)