Goa: services from a design

Markdown
Define your Go API contract once. Generate HTTP, gRPC, and JSON-RPC servers, typed clients, validation, and OpenAPI 3 specifications.

Goa generates Go service APIs from a single typed Go contract. You declare payloads, validation constraints, error mappings, and HTTP, gRPC, or JSON-RPC transports in design/*.go; goa gen compiles the transport layer, clients, and OpenAPI / Protobuf specs into gen/. You write only the business logic behind a plain Go interface.

One contract to running endpoint

1. Design the contract (design/catalog.go)

package design

import . "goa.design/goa/v3/dsl"

var LookupPayload = Type("LookupPayload", func() {
    Field(1, "sku", String, "Product SKU", func() {
        Pattern("^[A-Z0-9-]+$")
        Example("BOOK-1")
    })
    Required("sku")
})

var Product = Type("Product", func() {
    Field(1, "sku", String, "Product SKU")
    Field(2, "name", String, "Display name")
    Field(3, "price_cents", Int, "Price in cents", func() { Minimum(0) })
    Required("sku", "name", "price_cents")
})

var _ = Service("catalog", func() {
    Error("not_found", ErrorResult, "SKU does not exist")

    Method("lookup", func() {
        Payload(LookupPayload)
        Result(Product)
        Error("not_found")
        HTTP(func() {
            GET("/products/{sku}")
            Response("not_found", StatusNotFound)
        })
    })
})

(To expose gRPC or JSON-RPC from the same method, add GRPC(func() {}) with protoc, protoc-gen-go, protoc-gen-go-grpc, and --grpc-port 8081, or JSONRPC(func() {}). See the gRPC guide.)

2. Generate transport & validation code (gen/)

go mod init example.com/catalog
go get goa.design/goa/v3@v3.36.0
go mod tidy
go run goa.design/goa/v3/cmd/goa gen example.com/catalog/design
go run goa.design/goa/v3/cmd/goa example example.com/catalog/design

goa gen writes gen/ (committed to Git, never hand-edited): request decoders that reject invalid SKUs before your handler runs, standard net/http servers with generated Go routing and validation, typed Go and CLI clients, gen/http/openapi3.yaml + openapi3.2.yaml, and the Go interface your service implements:

// gen/catalog/service.go — Code generated by goa, DO NOT EDIT.
type Service interface {
    Lookup(context.Context, *LookupPayload) (*Product, error)
}

goa example creates starter cmd/catalog wiring and a stub catalog.go once without overwriting later edits.

3. Implement the business logic and run (catalog.go)

Replace the generated catalog.go stub with your handler:

package catalogapi

import (
    "context"
    "fmt"

    catalog "example.com/catalog/gen/catalog"
)

type catalogsrvc struct{}

func NewCatalog() catalog.Service { return &catalogsrvc{} }

func (s *catalogsrvc) Lookup(
    ctx context.Context, p *catalog.LookupPayload,
) (*catalog.Product, error) {
    if p.Sku != "BOOK-1" {
        return nil, catalog.MakeNotFound(
            fmt.Errorf("unknown sku %q", p.Sku),
        )
    }
    return &catalog.Product{
        Sku: "BOOK-1", Name: "The Go Book", PriceCents: 3800,
    }, nil
}

Fetch the remaining dependencies and start the server (go mod tidy && go run ./cmd/catalog --http-port 8080), then query it:

$ curl -i http://localhost:8080/products/BOOK-1
HTTP/1.1 200 OK
{"sku":"BOOK-1","name":"The Go Book","price_cents":3800}

When you change a field or method in design/catalog.go and rerun goa gen, the Go compiler flags every out-of-date handler and caller immediately.

What you own vs. what Goa generates

You and your coding agent own (design/, internal/)goa gen generates (gen/, regenerated on every change)
Domain types, constraints, and transport routes in design/Go types, Service interfaces, and endpoint middleware hooks
Business rules, authorization, and persistenceHTTP, gRPC, and JSON-RPC routing, codecs, and input validation
Idempotency, database transactions, and behavior testsTyped Go clients, CLI clients, OpenAPI 2/3/3.2, and Protobuf specs

Generated validation enforces structural rules (Required, Pattern, Minimum, Enum) at the transport edge. Authorization, tenant checks, and domain invariants belong in your service implementation and tests.

Extend services to MCP and AI agents

Goa-AI extends the same design/*.go files with MCP(...) server declarations, typed Toolsets bound to Goa methods via BindTo, structured Completions, and durable Temporal agent workflows.