What if you could update many things at once with just one simple API call?
Why Composite operations (multi-resource) in Rest API? - Purpose & Use Cases
Imagine you need to update a user's profile and their related settings by calling separate API endpoints one by one.
You have to send multiple requests manually, waiting for each to finish before starting the next.
This manual approach is slow because each request waits for the previous one.
It's also error-prone: if one request fails, you might end up with inconsistent data.
Handling all these steps manually makes your code complex and hard to maintain.
Composite operations let you bundle multiple API calls into a single request.
This means you can update the user profile and settings together, atomically.
The server handles all steps, ensuring consistency and faster execution.
PATCH /user/123 PATCH /user/123/settings
POST /composite
{
"operations": [
{"method": "PATCH", "path": "/user/123", "body": {...}},
{"method": "PATCH", "path": "/user/123/settings", "body": {...}}
]
}Composite operations enable you to perform multiple related changes in one go, making your app faster and more reliable.
When a user updates their profile and notification preferences, a composite operation updates both at once, avoiding partial updates that confuse the user.
Manual multiple API calls are slow and risky.
Composite operations bundle calls into one request.
This improves speed, consistency, and code simplicity.
Practice
Solution
Step 1: Understand what composite operations do
Composite operations bundle several API calls into one request to reduce network overhead.Step 2: Identify the main benefit
This bundling saves time and ensures related data changes happen together, keeping consistency.Final Answer:
They combine multiple API calls into a single request to save time and keep data consistent. -> Option AQuick Check:
Composite operations = combine calls + consistency [OK]
- Confusing composite operations with documentation tools
- Thinking composite operations encrypt data
- Assuming composite operations run SQL queries
Solution
Step 1: Identify the correct JSON structure for composite operations
Composite operations usually use an "operations" array with each operation having "method" and "path" keys.Step 2: Compare options
{"operations": [{"method": "POST", "path": "/users"}, {"method": "PUT", "path": "/accounts/123"}]} matches this structure exactly, while others use incorrect keys or formats.Final Answer:
{"operations": [{"method": "POST", "path": "/users"}, {"method": "PUT", "path": "/accounts/123"}]} -> Option DQuick Check:
Composite request = "operations" array with method and path [OK]
- Using singular "operation" instead of plural "operations"
- Using wrong keys like "url" or "endpoint"
- Not wrapping operations inside an array
{
"operations": [
{"method": "POST", "path": "/users", "body": {"name": "Alice"}},
{"method": "POST", "path": "/orders", "body": {"userId": "${1.id}", "item": "Book"}}
]
}
What is the expected behavior?Solution
Step 1: Understand operation order and referencing
The first operation creates a user named Alice. The second operation uses "${1.id}" to reference the ID from the first operation's result.Step 2: Determine expected behavior
This means the order is created linked to the new user. This chaining is a key feature of composite operations.Final Answer:
Create a user named Alice, then create an order linked to that user using the new user ID. -> Option AQuick Check:
Reference previous result with ${1.id} = chained operations [OK]
- Assuming operations run in parallel without order
- Thinking referencing previous results is invalid
- Confusing operation indexes starting at 0 instead of 1
{
"operations": [
{"method": "POST", "path": "/products", "body": {"name": "Pen"}},
{"method": "PUT", "path": "/inventory/${1.id}", "body": {"stock": 100}}
]
}
But the API returns an error saying "Invalid reference ${1.id}". What is the likely cause?Solution
Step 1: Check the reference usage
The error "Invalid reference ${1.id}" means the first operation's response does not contain an "id" field.Step 2: Understand why this causes error
If the first operation's response lacks "id", the reference ${1.id} is invalid and causes failure.Final Answer:
The first operation did not return an ID, so ${1.id} is invalid. -> Option BQuick Check:
Reference error = missing id in first operation response [OK]
- Assuming index starts at 0 instead of 1
- Thinking PUT is disallowed in composite operations
- Ignoring that referenced fields must exist in response
Solution
Step 1: Identify correct operation order and referencing
The customer creation is first (operation 1), so its ID is referenced as ${1.id} in subsequent operations.Step 2: Verify all references use ${1.id} and order is logical
Orders and update use ${1.id} correctly. { "operations": [ {"method": "POST", "path": "/customers", "body": {"name": "Bob"}}, {"method": "POST", "path": "/orders", "body": {"customerId": "${1.id}", "item": "Laptop"}}, {"method": "POST", "path": "/orders", "body": {"customerId": "${1.id}", "item": "Mouse"}}, {"method": "PUT", "path": "/customers/${1.id}", "body": {"status": "active"}} ] } matches this perfectly. { "operations": [ {"method": "POST", "path": "/customers", "body": {"name": "Bob"}}, {"method": "POST", "path": "/orders", "body": {"customerId": "${2.id}", "item": "Laptop"}}, {"method": "POST", "path": "/orders", "body": {"customerId": "${2.id}", "item": "Mouse"}}, {"method": "PUT", "path": "/customers/${2.id}", "body": {"status": "active"}} ] } incorrectly uses ${2.id} which refers to the first order, not the customer.Final Answer:
{ "operations": [ {"method": "POST", "path": "/customers", "body": {"name": "Bob"}}, {"method": "POST", "path": "/orders", "body": {"customerId": "${1.id}", "item": "Laptop"}}, {"method": "POST", "path": "/orders", "body": {"customerId": "${1.id}", "item": "Mouse"}}, {"method": "PUT", "path": "/customers/${1.id}", "body": {"status": "active"}} ] } -> Option CQuick Check:
Use ${1.id} to reference first operation's customer ID [OK]
- Referencing wrong operation index for customer ID
- Hardcoding IDs instead of referencing dynamically
- Incorrect operation order breaking dependencies
Ready to prove it?
Pass the test and this pattern moves unknown
