Partial-response (fields/sparse) pattern
Let API consumers request only the fields they need. Reduces payload size, speeds up mobile clients, and keeps your responses lean.
How it works
Clients pass a ?fields= query parameter with a comma-separated list of property paths. The server strips every key not in that whitelist before serializing the response.
Request
GET /api/users/42?fields=id,email,profile.nameResponse (200)
{
"id": 42,
"email": "ada@meridian.dev",
"profile": {
"name": "Ada"
}
}Implementation sketch
- Parse
fieldsinto aSet<string>. - Walk the response object recursively. Drop any key whose dot-joined path is absent from the set.
- If
fieldsis missing or empty, return the full resource (backward-compatible). - Return
400 Bad Requestwhen an unknown field is requested.
When to use
- Mobile or low-bandwidth clients that need tiny payloads.
- List endpoints where each item carries dozens of fields.
- Public APIs where you want to minimize over-fetching.
Tip: Combine with cursor pagination for ultra-lean list responses.