Set an API response
Function: Set an API response
This function allows you to define the final data that your API endpoint will send back to whoever is calling it. It's crucial when you are building your own API within the platform, as it determines what information is returned to the requesting system or user. This function is specifically for creating an API response, not for processing responses from other APIs you might be calling.
Input
- Object
- Description: This is the data you want your API to send back. It should be structured as a collection of key-value pairs, similar to a list of properties and their corresponding values. For example, you might send back a user's name and email, or a status message.
- Type: OBJECT (A collection of key-value pairs)
- Required: Yes
Execution Flow
Real-Life Examples
Here are some practical ways you can use the "Set an API response" function:
Example 1: Confirming a Successful Operation
Imagine you have an API endpoint that creates a new customer record. After the customer is successfully added, you want to send a confirmation back.
- Inputs:
- Object:
\{
"status": "success",
"message": "Customer created successfully!",
"customerId": "CUST-00123"
\}
- Object:
- Result: The API endpoint will respond with a JSON object indicating success, a confirmation message, and the ID of the newly created customer.
Example 2: Returning Retrieved Data
Suppose you have an API endpoint that fetches details for a specific product.
- Inputs:
- Object:
\{
"productId": "PROD-456",
"name": "Wireless Headphones",
"price": 99.99,
"inStock": true
\}
- Object:
- Result: The API endpoint will return a JSON object containing the product's ID, name, price, and stock status.
Example 3: Handling an Error
If your API endpoint encounters an issue, like missing required information, you can use this function to send back a clear error message.
- Inputs:
- Object:
\{
"error": "Validation Failed",
"code": 400,
"details": "The 'productName' field is missing and is required."
\}
- Object:
- Result: The API endpoint will respond with a JSON object detailing the error, an error code, and specific information about what went wrong.