liveBook · Manning

Api model

As explained in Functional Design and Architecture: Examples in Haskell

An API model is a data representation designed for communication across an application programming interface rather than for internal domain behavior.

An API model represents data crossing an application programming interface. It acts as a boundary type between networking code and business logic, rather than as a domain or database type. API models are usually value types with no attached logic; data arriving from outside is transformed into justified domain values before business operations use it. section 14.1.1 section 14.2.4

Use an API model to keep external communication separate from internal behavior:

  • Define request and response shapes specifically for communication, instead of exposing domain types directly. section 14.1.1
  • Keep API models mostly logic-free and place them only between networking code and business logic. section 14.1.1
  • Validate incoming API values before converting them into domain values, returning an error for invalid requests. section 14.2.4
  • Prefer focused endpoint models over one universal type filled with optional or undefined fields, while watching for needless duplication. section 14.1.1

Asteroid request model

An HTTP schema can accept an AsteroidTemplate containing size, mass, azimuth, and altitude, then return an AsteroidId. The template describes the network payload; it does not replace the protected domain model.

Validation at the boundary

An AstroObjectTemplate may contain an optional name plus objectClass and code. A handler rejects an empty name, object class, or code with a 400 Bad Request before calling business logic.

How Functional Design and Architecture: Examples in Haskell covers it

How deep it goes
Defines boundary responsibilities, contrasts specialized and duplicated types, and demonstrates validation before stopping short of a universal duplication solution.
Examples
Uses asteroid templates, HTTP request schemas, optional object names, validation errors, and Servant handlers as concrete examples.
What it assumes
Assumes familiarity with networking boundaries and business logic while teaching the distinction between API and domain representations.
What you can do after
Apply it via Haskell and Servant by defining boundary types, validating requests, and passing justified values to business logic.
How it connects
Connects API models with domain models, database models, HTTP schemas, data transfer objects, validation, REST, GraphQL, SRP, and ISP.

Also explained in

Compare how 3 books explain api model →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Functional Design and Architecture: Examples in Haskell. The section links point to the parts of the book it draws on.