5 Modeling data
This chapter explains how to turn previously identified API operations and their HTTP representations into clear, consumer-friendly data models. It advocates modeling all data exchanged via HTTP—path and query parameters, headers, and bodies—using JSON’s portable types for broad interoperability. The goal is a versatile API that meets needs without exposing inner workings, achieved by carefully selecting names, types, structures, and required fields, while postponing debates on fine details until later design layers address usability, performance, security, and implementation constraints.
The method starts with designing complete theoretical resource models that capture all relevant concept data and identifiers, then deriving them into operation-specific inputs and outputs. Typical patterns streamline this work: complete, summarized, minimal, and identifier models for outputs and routing; creation, replacement, and modification models for inputs—excluding server-managed fields and adjusting required flags by context. CRUD operations follow consistent recipes (e.g., read returns the complete model, search uses summaries and aligned filters, create may return minimal data plus Location, update distinguishes PUT replacement from PATCH modification), and a simple provisional error model keeps outputs exhaustive. The same approach extends to “Do” operations, either by creating business concept resources (executions/results) or using action resources with clear input/output models.
Finally, the chapter shows how data models reveal gaps and sharpen focus: tracing input sources and output usages helps spot missing capabilities (such as lookup resources needed to supply valid inputs), and refining inputs uncovers complete business error cases. Designers must differentiate similarly named concepts across contexts, ensure consistency and clarity in naming and typing, and guard against leaking provider structures or overfitting to a single consumer or UI. The result is a coherent, future-proof set of models that are easy to use, exhaustive enough for implementation and testing, and adaptable across use cases.
Once we have represented operations with HTTP, including choosing data locations in HTTP requests and responses, we can work on data modeling.
We’ll model the data of the resources of the Online Shopping example.
We’ll model the data of the input and output of the Online Shopping example’s operations
We model all HTTP request and response data (path parameters, query parameters, headers, and bodies).
The data we model is the data exchanged between the consumer and the provider; it may differ from the underlying data stored in the database.
To ensure all consumers can understand our data, we use the JSON portable data format. It handles Boolean, number, string, array, and object data types.
We leverage theoretical resource models to design inputs and outputs. We ensure that data meets needs without exposing inner workings; we especially check input sources for gaps in capabilities.
The Catalog contains many products; it is an array. The product is an object.
The Product resource theoretical model contains all the information necessary to describe a product in the context of the API.
The operation output is the Product resource model; required properties are always returned. The productReference path parameter is based on the productReference property.
The product summary model is a subset of the product model containing essential information describing a product in the context of a list.
To model the data participating in product creation, we start with the complete model and remove the data managed by the server.
The model for PUT matches product creation. Modifying two properties requires sending all data. The model for PATCH is similar, but all properties are optional; consumers send only modified data.
To be exhaustive on output data modeling, we design a simplistic and generic error model that we will enhance later.
The typical API operations leverage typical data models.
Starting from the complete data model, we can design summarized (essential information), minimal (only the resource identifier property, and identifier (only the resource identifier value) models.)
Starting from the complete model, we can design the creation, replacement, and modification input data models that contain only consumer-managed data.
FAQ
What data should be modeled for a web API?
Model all data exchanged between the consumer and provider via HTTP: path parameters (identifiers), query parameters (modifiers/filters), header fields (metadata like Location), and request/response bodies (resource state). Design from a subject-matter perspective; do not mirror database schemas unless it also meets consumer needs.Why start with a theoretical resource model?
A theoretical (complete) resource model captures all relevant properties of a business concept, including its identifier. It becomes the single source of truth from which you derive consistent inputs and outputs for operations, reducing errors and improving usability.How do I pick property names, types, and requiredness?
- Use portable JSON types: string, number, boolean, object, array (and null when needed).- Choose clear, consistent names (one casing style across the API).
- Mark properties required only if the concept makes no sense without them or if the API guarantees they are always returned (outputs) or must be provided (inputs).
- Add short descriptions for format or domain rules (for example, date strings, currency).
How do I derive inputs and outputs from the resource model?
- Read (GET by id): return the complete model; map the path parameter to the resource’s identifier type/name.- Search (GET list): return a summarized model (subset) and base filters on properties in the returned model; ensure filters are backed by modeled data.
- Create/Update: start from the complete model and remove implementation-managed fields as appropriate.
What are the seven typical models and when do I use them?
- Complete: all properties (with identifier); use for read/update outputs and sometimes search outputs.- Summarized: essential subset (with identifier); use for list/search outputs.
- Minimal: only the identifier; use as a lean create output when performance matters.
- Identifier: the identifier’s type/value; use in path parameters.
- Creation: consumer-provided subset (no implementation-managed fields); use for POST bodies.
- Replacement: usually same as creation; use for PUT bodies.
- Modification: same fields as creation but optional; use for PATCH bodies.
How should I model create, update (PUT/PATCH), and delete operations?
- Create (POST): input = creation model; output = complete or minimal model; Location header carries the new resource URL.- Update (PUT): input = replacement model (send all consumer-managed data); output = complete model.
- Update (PATCH): input = modification model (only changed fields, all optional); output = complete model.
- Delete (DELETE): input = identifier in the path; no body output on success.
How can data modeling reveal missing capabilities?
Trace each input to its source and each output to its intended use. If consumers cannot supply a required value (for example, category for new products), add supporting operations/resources (for example, list/select categories). If outputs imply new use cases (for example, supplier filters), model them explicitly.How do I avoid exposing internals or overly specific consumer bias?
Model data to match the domain, not a database schema or a particular UI/workflow. Don’t leak implementation rules or structures; provide ready-to-use, meaningful fields. Avoid grouping or naming driven by one application’s screen when it doesn’t reflect the concept.How should I handle errors at this stage of modeling?
Create a temporary, simple error model for 4XX/5XX responses (for example, an object with a required message string). Use it consistently across operations and refine it later with codes, details, and actionable guidance.How do I model data for “Do” (non-CRUD) operations?
You can represent “Do” as:- A business concept resource (Executions/Results): POST a creation model and return a complete result object; or
- An action resource (POST /do): body contains inputs and the output returns the result (often echoing inputs plus result for clarity). In both cases, base the models on the same principles as CRUD.
The Design of Web APIs, Second Edition ebook for free