Set an API response
Function: Set an API response
This action allows you to define the exact data that your API will send back as a response to any incoming request. It's crucial when you are building your own API endpoints within the platform, as it determines what information callers receive. This action is not used when you are calling an external API; it's specifically for crafting the response of an API you are creating.
Input
- Object (Type: OBJECT) Specify the data you want to send back as the API response. This can be a simple value, a list of items, or a structured collection of key-value pairs (an object). This input is required.
Output
This action does not produce a separate output variable that can be used by subsequent actions. Instead, it is the final output of your API endpoint. The "Object" you provide will be directly returned as the body of the API response.
Execution Flow
Real-Life Examples
Here are some examples of how you can use the "Set an API response" action to define what your API sends back.
Example 1: Confirming a successful operation
Imagine you have an API endpoint that creates a new user. After the user is successfully created, you want to send a simple confirmation message.
- Inputs:
- Object:
\{
"status": "success",
"message": "User created successfully!"
\}
- Object:
- Result: The API will respond with a JSON object indicating success, which the calling application can then use to update its user interface or log the event.
Example 2: Returning a list of products
Suppose you have an API endpoint that retrieves a list of products from your database. You want to return this list as the API response.
- Inputs:
- Object: (Assume this
productsListis a variable containing data retrieved from a database query action)[
\{
"id": 101,
"name": "Laptop Pro",
"price": 1200.00
\},
\{
"id": 102,
"name": "Wireless Mouse",
"price": 25.50
\},
\{
"id": 103,
"name": "USB-C Hub",
"price": 49.99
\}
]
- Object: (Assume this
- Result: The API will respond with a JSON array, where each element represents a product, allowing the calling application to display the product catalog.
Example 3: Indicating an error with details
If an API request fails, for example, due to missing required information, you might want to send back an error message with specific details.
- Inputs:
- Object:
\{
"status": "error",
"code": "400",
"message": "Invalid input provided.",
"details": "The 'email' field is missing."
\}
- Object:
- Result: The API will respond with a JSON object detailing the error, including a status, an error code, a general message, and specific details, helping the calling application understand and handle the issue.