Custom Code for API Processing
The Custom Code (Embed Code) feature lets you write custom JavaScript logic for each endpoint of a MicroService API.
It enables flexible addition of logic that is difficult to handle with standard features, such as validation, external API integration, and data transformation.
Execution Timing
Custom code runs at the following two points:
| Timing | Description |
|---|---|
| Before Hook | Runs after receiving the request, before database processing |
| After Hook | Runs after database processing, before returning the response |
Common Use Cases
Before Hook examples:
- Custom input validation
- Reformatting or transforming request data
- Pre-checks via external services (e.g., inventory check)
After Hook examples:
- External notifications (Slack, email, Webhook)
- Chained calls to related APIs
- Filtering or transforming response data
Steps to Configure Custom Code
- Select a team in the console and open the target MicroService API.
- Select the endpoint to which you want to add custom code.
- Open the "Embed Code" tab.
- Write your code in the Before Hook or After Hook editor.
- Click "Save".
Code Examples
Before Hook: Input validation
// Return an error if the price in the request body is 0 or less
if (request.body.price <= 0) {
throw new Error("price must be greater than 0");
}
After Hook: Slack notification
// Notify via Slack Webhook after a record is created
const webhookUrl = process.env.SLACK_WEBHOOK_URL;
await fetch(webhookUrl, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
text: `New record created: ID=${response.data.id}`
})
});
Tip
Secrets such as API keys can be referenced as environment variables.
See Secrets for System Integration via API for registration instructions.
Notes
- Custom code is supported only for MicroService APIs (not available for IAM API or Storage API).
- There is a time limit for execution. For long-running processes, consider an asynchronous design.
- If an error occurs, the Before Hook stops processing and returns an error response. The After Hook does not affect the response.