REST API
Beginner
1 min read
API Response Envelopes and JSON:API
Example
// Standard envelope (custom)
{
"data": [ ... ],
"meta": { "total": 200, "page": 1, "perPage": 20 },
"links": { "self": "/products?page=1", "next": "/products?page=2" }
}
// JSON:API format (spec-compliant)
{
"data": [
{
"type": "products",
"id": "42",
"attributes": {
"name": "Running Shoes",
"price": 89.99,
"status": "active"
},
"relationships": {
"category": {
"data": { "type": "categories", "id": "5" }
}
},
"links": { "self": "/products/42" }
}
],
"included": [
{
"type": "categories",
"id": "5",
"attributes": { "name": "Footwear" }
}
],
"meta": { "totalCount": 200 },
"links": { "self": "/products", "next": "/products?page[number]=2" }
}
// Error envelope
{
"errors": [
{
"status": "422",
"code": "VALIDATION_FAILED",
"title": "Validation Error",
"detail": "The price must be a positive number.",
"source": { "pointer": "/data/attributes/price" }
}
]
}