Skip to main content
This guide shows how to create a Mizu app from scratch. You’ll build a working API server that handles multiple endpoints, parses JSON, and organizes routes into groups.

Prerequisites

  • Go 1.22+ - Check with go version
  • A code editor - VS Code with the Go extension works well
  • Basic Go knowledge - Functions, structs, and packages

Step 1: Create a project

Open your terminal and create a new directory:
The go mod init command creates a go.mod file. This tracks your project’s dependencies.

Step 2: Install Mizu

Add Mizu as a dependency:
You’ll see Mizu added to your go.mod file.

Step 3: Write your first handler

Create main.go:
Run it:
Open http://localhost:3000. You’ll see “Hello, Mizu!” in the browser. The terminal shows request logs. Press Ctrl+C to stop the server.

Step 4: Add JSON responses

Most APIs return JSON. Update main.go:
Try these URLs:
  • http://localhost:3000/users → Returns a JSON array of users
  • http://localhost:3000/users/42 → Returns a single user with ID “42”

Step 5: Handle POST requests

APIs need to accept data too. Add a POST endpoint:
Test with curl:

Step 6: Organize with route groups

As your API grows, group related routes together:
Routes are now:
  • GET / → API info
  • GET /api/v1/users → List users
  • GET /api/v1/users/{id} → Get user
  • POST /api/v1/users → Create user
  • GET /api/v1/admin/stats → Admin stats

Step 7: Add error handling

Centralize how errors are reported:

Project structure

For larger projects, organize your code:
Example handlers/users.go:
Then in main.go:

What you learned

  • Create a Mizu app with mizu.New()
  • Define handlers that return errors
  • Return JSON with c.JSON(code, data)
  • Capture path parameters with {param} and c.Param()
  • Parse JSON bodies with c.BindJSON()
  • Organize routes with Group()
  • Handle errors centrally with ErrorHandler()

Next steps