This screen allows you to configure custom Cloud Code scripts to run immediately before (pre) or after (post) the core Client API methods.
To see and edit existing API hooks, go to the API Hooks page on the dashboard.
Click [+New API Hook] to open the Add Hook interface and select/fill out the essential items.
Note:
API Hooks are for client APIs only, not designed for s2s APIs.
There are two different types of API Hooks, pre-hook and post-hook. Pre-hook scripts are executed before the API call they are hooked to. They allow you to pre-process the data that gets passed to the API call, or even return an error immediately and skip the API call altogether. Post-hook scripts are called after executing a particular API call. Post-hook scripts can do some post-processing for an API call as well as optionally modify the data returned to the client.
The allowed API Hooks' accessing and returning data are defined on the API server side, which is slightly different on pre-hook and post-hook. Ensure that you use the correct fields on your script. Refer to our cloud code tutorial for more info on conventional accessing and returning data fields.
Once the hook is created, it will appear on the hooks list:
Code Example
A pre-hook script example -- add an extra event data to sendEvent() API:
message = data.message; //get the passed in API parameters
hookParms = data.parms; //get the custom hook parameters
bridge.logInfoJson("running from prehook_sendevent_addvalue hookparms: ", hookParms);
Object.assign(message.eventData, hookParms.extradata); //add extra json data value to eventdata that was passed in
bridge.logInfoJson("running from prehook_sendevent_adevalue message: ", message);
var retval = {}; //prepare the return object
retval.status = 200;
retval.messageOverride = message;
retval;
Note for the script:
The parameters passed into the hook can be accessed through the "data" object parms field. The parameters from the hooked API can be accessed from the message field.
The altered parameter that is passed to the hooked API through the script-defined object with the messageOverride field. For the script example, to finally modify the hooked API parameters, add this field to the return object.
The field status represents the script processing flag, when set to any non-200 value, the API call will be aborted with an error return to the calling place.
Call the hooked API sendEvent():
Check the result by calling getEvents() from the event recipient, notice the hook's parameter is added as an extra eventData: