Action links help users or programs know what steps they can take next in a process. They make it easy to move from one state to another in a clear way.
Action links for state transitions in Rest API
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Rest API
{
"state": "current_state",
"actions": {
"action_name": {
"href": "URL_to_perform_action",
"method": "HTTP_method",
"type": "content_type"
},
...
}
}The actions object lists possible next steps with their URLs.
Each action includes the HTTP method to use (GET, POST, etc.) and the content type expected.
Examples
Rest API
{
"state": "pending",
"actions": {
"approve": {
"href": "/orders/123/approve",
"method": "POST",
"type": "application/json"
},
"cancel": {
"href": "/orders/123/cancel",
"method": "POST",
"type": "application/json"
}
}
}Rest API
{
"state": "shipped",
"actions": {
"track": {
"href": "https://tracking.example.com/track/123",
"method": "GET",
"type": "text/html"
}
}
}Sample Program
This simple Flask API returns the current state of an order and the possible actions as links. It shows how action links guide the client on what to do next.
Rest API
from flask import Flask, jsonify, request app = Flask(__name__) orders = { 1: {"state": "pending"}, 2: {"state": "shipped"} } @app.route('/orders/<int:order_id>', methods=['GET']) def get_order(order_id): order = orders.get(order_id) if not order: return jsonify({"error": "Order not found"}), 404 state = order['state'] if state == 'pending': actions = { "approve": { "href": f"/orders/{order_id}/approve", "method": "POST", "type": "application/json" }, "cancel": { "href": f"/orders/{order_id}/cancel", "method": "POST", "type": "application/json" } } elif state == 'shipped': actions = { "track": { "href": f"https://tracking.example.com/track/{order_id}", "method": "GET", "type": "text/html" } } else: actions = {} return jsonify({"state": state, "actions": actions}) if __name__ == '__main__': app.run(debug=True)
Important Notes
Action links help keep your API flexible and easy to use.
Always include the HTTP method so clients know how to use the link.
Use clear and consistent naming for actions to avoid confusion.
Summary
Action links show what steps are possible next in a process.
They include URLs, HTTP methods, and content types for each action.
This makes APIs easier to understand and use without guesswork.
Practice
1. What is the main purpose of
action links in REST APIs for state transitions?easy
Solution
Step 1: Understand what action links represent
Action links are URLs included in API responses that show possible next steps or actions a client can take to change the state.Step 2: Identify the purpose of action links
They guide clients on how to move from one state to another by calling these URLs.Final Answer:
To provide URLs that clients can use to change the current state -> Option DQuick Check:
Action links = URLs for state change [OK]
Hint: Action links = URLs for next steps in state [OK]
Common Mistakes:
- Confusing action links with authentication tokens
- Thinking action links store data
- Assuming action links format data
2. Which of the following is the correct way to include an action link for a "cancel" operation in a REST API JSON response?
easy
Solution
Step 1: Recognize common pattern for action links
Action links are often grouped under an "actions" key with action names as keys and URLs as values.Step 2: Check each option's format
"actions": {"cancel": "https://api.example.com/orders/123/cancel"} correctly uses an "actions" object with "cancel" as key and the URL as value, which is a clear and common pattern.Final Answer:
"actions": {"cancel": "https://api.example.com/orders/123/cancel"} -> Option BQuick Check:
Action links grouped under "actions" = "actions": {"cancel": "https://api.example.com/orders/123/cancel"} [OK]
Hint: Group action links under "actions" key for clarity [OK]
Common Mistakes:
- Using HTTP method inside the URL string
- Not grouping actions under a common key
- Using incorrect HTTP method for cancel
3. Given this JSON response snippet from a REST API:
What will happen if the client calls the URL in the "approve" action link?
{
"state": "pending",
"actions": {
"approve": "https://api.example.com/items/42/approve",
"reject": "https://api.example.com/items/42/reject"
}
}What will happen if the client calls the URL in the "approve" action link?
medium
Solution
Step 1: Understand the meaning of the "approve" action link
The "approve" link is provided as a next step to change the state from "pending" to "approved" by calling that URL.Step 2: Predict the effect of calling the approve URL
Calling the approve URL triggers the state transition to "approved" as intended by the API design.Final Answer:
The item state will change to approved -> Option AQuick Check:
Calling "approve" URL = state changes to approved [OK]
Hint: Action link name hints the state change [OK]
Common Mistakes:
- Assuming the URL deletes the item
- Thinking the URL is invalid
- Believing state stays the same after action
4. A REST API response includes this action link:
But calling this URL returns a 405 Method Not Allowed error. What is the most likely cause?
"actions": {"complete": "https://api.example.com/tasks/99/complete"}But calling this URL returns a 405 Method Not Allowed error. What is the most likely cause?
medium
Solution
Step 1: Understand 405 Method Not Allowed error
This error means the HTTP method used is not supported by the URL endpoint.Step 2: Identify common cause with action links
Action links for state changes usually require POST, but clients often call them with GET by mistake.Final Answer:
The client used GET instead of POST to call the action link -> Option CQuick Check:
405 error = wrong HTTP method used [OK]
Hint: Use POST for action links, not GET [OK]
Common Mistakes:
- Assuming URL is misspelled without checking
- Thinking 405 means resource missing
- Blaming server downtime without evidence
5. You want to design a REST API for an order system with states:
new, paid, shipped, and cancelled. Which of the following JSON responses best uses action links to guide clients through valid state transitions when the order is in paid state?hard
Solution
Step 1: Identify valid next states from "paid"
From "paid", the order can be "shipped" or "cancelled" but not "pay" or "new" again.Step 2: Check which options provide correct action links
{ "state": "paid", "actions": { "ship": "https://api.example.com/orders/555/ship", "cancel": "https://api.example.com/orders/555/cancel" } } correctly offers "ship" and "cancel" actions, matching valid transitions.Final Answer:
JSON with "ship" and "cancel" actions for "paid" state -> Option AQuick Check:
Valid next actions for "paid" = ship, cancel [OK]
Hint: Only include valid next states as action links [OK]
Common Mistakes:
- Including actions that repeat previous states
- Missing valid next state actions
- Confusing state names with action names
