Field Manual PHP / Laravel reference
← Laravel

API responses

Responses contain a status, headers, and a body.

What it is

Responses contain a status, headers, and a body.

When to use it

Return a consistent contract and meaningful status codes.

Syntax

return response()->json(["id" => $order->id], 201);

Example

return response()->json([
    "error" => ["code" => "conflict", "message" => "Order is already settled"],
], 409);
// Deletion without a response body:
return response()->noContent();

Common mistakes

A 204 response must not contain JSON; do not return 200 for every failure.

Related topics

Error responses08. Design API / interfaces