Response envelope patterns help organize API responses in a clear and consistent way. They make it easy to understand if a request worked and what data or errors came back.
Response envelope patterns in Rest API
{
"status": "success" | "error",
"data": {...},
"message": "Optional message string",
"errors": [...],
"meta": {...}
}The status field usually shows if the request was successful or not.
The data field holds the main response content when successful.
data.{
"status": "success",
"data": {
"user": {"id": 1, "name": "Alice"}
}
}{
"status": "error",
"message": "User not found",
"errors": ["No user with id 1"]
}{
"status": "success",
"data": [1, 2, 3],
"meta": {"count": 3}
}This small Flask API returns a response envelope. If the user exists, it sends a success status with user data. If not, it sends an error status with a message and error list.
from flask import Flask, jsonify, request app = Flask(__name__) users = {1: "Alice", 2: "Bob"} @app.route('/user/<int:user_id>') def get_user(user_id): if user_id in users: response = { "status": "success", "data": {"id": user_id, "name": users[user_id]} } else: response = { "status": "error", "message": "User not found", "errors": [f"No user with id {user_id}"] } return jsonify(response) if __name__ == '__main__': app.run(debug=True)
Always keep the response format consistent so clients can handle responses easily.
Include helpful messages and error details to make debugging easier.
Use the meta field for extra info like paging or timing if needed.
Response envelopes wrap API data with status and messages for clarity.
They help clients quickly know if a request succeeded or failed.
Consistent response patterns improve API usability and debugging.
Practice
response envelope in REST APIs?Solution
Step 1: Understand what a response envelope is
A response envelope is a structure that wraps the main data with extra information like status and messages.Step 2: Identify the purpose of wrapping data
This wrapping helps clients quickly understand if the request succeeded or failed and provides useful messages.Final Answer:
To wrap the actual data with status and messages for clarity -> Option BQuick Check:
Response envelope = Wrap data with status and messages [OK]
- Thinking response envelopes hide data
- Believing they slow down the API on purpose
- Confusing response envelopes with security features
Solution
Step 1: Identify the typical keys in a response envelope
Common keys include "status" for success/failure and "data" for the actual content.Step 2: Check each option for correct keys and structure
{"status": "success", "data": {"id": 1, "name": "Alice"}} uses "status" and "data" correctly. Others use different or missing keys.Final Answer:
{"status": "success", "data": {"id": 1, "name": "Alice"}} -> Option AQuick Check:
Correct keys = status + data [OK]
- Using 'result' or 'payload' instead of 'data'
- Missing the 'status' key
- Confusing 'error' with 'status'
{"status": "error", "message": "Not found", "data": null}What should the client understand from this response?
Solution
Step 1: Analyze the 'status' field
The status is "error", indicating the request did not succeed.Step 2: Check the 'message' and 'data' fields
The message says "Not found" and data is null, meaning no data was returned due to an error.Final Answer:
The request failed and no data is returned -> Option DQuick Check:
Status 'error' + null data = failure [OK]
- Assuming 'error' means success
- Ignoring the 'message' field
- Thinking null data means empty success
{"status": "success", "data": {"id": 5}}What is wrong with this response?
Solution
Step 1: Check JSON syntax
The JSON string ends after "id": 5 without closing the outer object with a brace.Step 2: Validate required fields
While 'message' is optional and 'data' can be an object, the missing brace is a syntax error.Final Answer:
Missing closing brace for JSON object -> Option AQuick Check:
JSON must have matching braces [OK]
- Assuming 'message' is mandatory
- Thinking 'data' must be an array
- Confusing status values
- A status code (number)
- A success boolean
- A message string
- The actual data
Which JSON structure best fits this requirement?
Solution
Step 1: Match required fields with options
Required fields are numeric status code, boolean success, message string, and data object.Step 2: Check each option for correct types and keys
{"code": 200, "success": true, "message": "OK", "data": {"user": "Bob"}} uses 'code' as number, 'success' as boolean, 'message' as string, and 'data' correctly. Others miss types or keys.Final Answer:
{"code": 200, "success": true, "message": "OK", "data": {"user": "Bob"}} -> Option CQuick Check:
Correct keys and types = {"code": 200, "success": true, "message": "OK", "data": {"user": "Bob"}} [OK]
- Using strings instead of boolean or number
- Missing some required fields
- Using inconsistent key names
Ready to prove it?
Pass the test and this pattern moves unknown
