Goa API Design
Relevant source files
The following files were used as context for generating this wiki page:
Goa is the design-first API framework for the Speakeasy backend. It provides a Domain Specific Language (DSL) to define service interfaces, endpoints, and data types in a centralized location. The project uses these definitions to generate transport-independent service interfaces, HTTP servers, and clients.
Sources: README.md:104, REVIEW.md:126
Codebase Organization
The API design infrastructure divides into two primary directories within the server/ module.
- Design Files (
server/design/): Contains the Go source files utilizing the Goa DSL to define the API contract. These files serve as the single source of truth for the API surface. - Generated Code (
server/gen/): Contains the artifacts produced by thegoa gencommand. Developers must not edit these files directly, as the generator overwrites them during the build process.
Sources: CLAUDE.md:46-52, REVIEW.md:13-14
Artifact Generation Flow
The following diagram illustrates the transformation from design definitions to executable server components.
The diagram shows how the Goa generator processes DSL files to create service interfaces and transport layers that implementation handlers then satisfy.
Sources: REVIEW.md:13-14, server/gen/about/service.go:1-11
Design Conventions
The project enforces specific naming and structural conventions to ensure consistency across the Speakeasy platform.
Naming and Metadata
Service and method names use camelCase, while DSL types use PascalCase. Package names remain lowercase without separators. Every method definition requires three specific OpenAPI metadata keys for Speakeasy SDK generation and frontend hook integration.
Sources: REVIEW.md:126-129
| Meta Key | Description |
|---|---|
operationId | Unique identifier for the API operation. |
x-speakeasy-name-override | Controls the naming of the generated SDK method. |
x-speakeasy-react-hook | Defines the name of the generated React Query hook. |
HTTP Method Mapping
Designers must follow strict HTTP verb mappings based on the operation type.
- Read Methods: Use
GET. - Mutations: Use
POST. - Deletions: Use
DELETE(typically restricted to endpoints with only an ID parameter).
Sources: REVIEW.md:127-128
Service Definition Structure
A Goa service definition groups related methods and defines common attributes like error responses and descriptions.
var _ = Service("about", func() {
Description("Information about the Speakeasy platform and its components.")
shared.DeclareErrorResponses()
Method("openapi", func() {
Description("The OpenAPI description of the Speakeasy API.")
Result(func() {
Attribute("contentType", String)
Attribute("contentLength", Int64)
Required("contentType", "contentLength")
})
HTTP(func() {
GET("/openapi.yaml")
Response(StatusOK)
})
})
})
Sources: server/design/about/design.go:8-32
Error Handling
The design framework generates specialized error builder functions for each service. Handlers use the oops package for internal error mapping and then leverage Goa's generated builders to return consistent HTTP error responses.
Common Error Builders
Generated services include builders for standard failure states:
MakeUnauthorized: Returns a 401 response.MakeForbidden: Returns a 403 response.MakeBadRequest: Returns a 400 response.MakeNotFound: Returns a 404 response.MakeUnexpected: Returns a 500 response.
Sources: server/gen/about/service.go:42-83, REVIEW.md:38-41
Request Lifecycle
The following sequence diagram represents how a request moves from the transport layer to the core business logic.
The sequence shows the separation between generated decoding/encoding logic and the handwritten service implementation.
Sources: server/gen/about/service.go:15-20, server/design/about/design.go:23-28
Implementation Constraints
Handlers must never return repository (repo) types directly. Instead, they pass data through Model View builders (e.g., mv.BuildSubjectView) to ensure the external API representation remains decoupled from the database schema.
Sources: REVIEW.md:131-132