Composite operations let you do many related tasks in one go. This saves time and keeps things simple.
Composite operations (multi-resource) in Rest API
POST /composite-operation
{
"operations": [
{"method": "POST", "path": "/users", "body": {"name": "Alice"}},
{"method": "POST", "path": "/profiles", "body": {"userId": "${results[0].body.id}", "bio": "Hello!"}}
]
}The request is usually a POST to a special endpoint like /composite-operation.
Each operation inside has a method, path, and optional body.
POST /composite-operation
{
"operations": [
{"method": "POST", "path": "/tasks", "body": {"title": "Buy milk"}},
{"method": "POST", "path": "/reminders", "body": {"taskId": "${results[0].body.id}", "time": "9am"}}
]
}POST /composite-operation
{
"operations": [
{"method": "DELETE", "path": "/orders/123"},
{"method": "DELETE", "path": "/payments/456"}
]
}This example shows creating a user and their profile together. The profile uses the user ID from the first operation.
POST /composite-operation
{
"operations": [
{"method": "POST", "path": "/users", "body": {"name": "Bob"}},
{"method": "POST", "path": "/profiles", "body": {"userId": "${results[0].body.id}", "bio": "I love coding."}}
]
}
Response:
{
"results": [
{"status": 201, "body": {"id": "u123", "name": "Bob"}},
{"status": 201, "body": {"id": "p456", "userId": "u123", "bio": "I love coding."}}
]
}Composite operations help keep related changes together, so you don't have to worry about partial updates.
Make sure the API supports referencing results from earlier operations, like using ${results[0].body.id}.
Check the response carefully to handle errors in any part of the composite request.
Composite operations combine multiple API calls into one request.
This saves time and keeps data changes consistent.
Use them when working with related resources that depend on each other.
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
