All Collections
Portal Pages
Design | Cloud Code | API Hooks
Design | Cloud Code | API Hooks

API Hooks, pre-hook

Jason Liang avatar
Written by Jason Liang
Updated over a week ago

API hooks allow you to configure custom Cloud Code scripts to run immediately before (Pre) or after (Post) the core Client API methods.

To see and edit exiting API hooks, go to the API Hooks page on the dashboard.

API Hooks

Click [+Create] 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&returning data are defined on the APIs server side, which is slightly different on pre-hook and post-hook. Ensure using cthe orrect fields on your script. Refer to our cloud code tutorial for more info on conventional accessing&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 message field.

  • The altered parameter that is passed to the hooked API through the script-defined object with messageOverride field. For the script example, in order 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(), notice the hook's parameter is added as an extra eventData: 

Did this answer your question?