Events that occur when an app is installed from the Freshworks Marketplace, updated to the latest version, or uninstalled are termed as app setup events. An app setup event is a synchronous event and you can enable your app to decide whether the event should reach completion. For example, if a webhook has to be registered during app installation, you can use the app setup event to disallow installation if the webhook registration fails. You can configure event listeners in the server.js file. When an app setup event occurs, the corresponding event listener invokes a callback method and passes a standard payload to the method.
Configure Events
To register an app setup event and the corresponding callback:
- From your app’s root directory, navigate to the manifest.json file.
- Include the events attribute, specifying the app setup event (possible events: onAppInstall, afterAppUpdate, and onAppUninstall) and the corresponding callback methods as follows:
Copied
Copy
12345
"events": { "<eventName>": { "handler": "<eventCallbackMethod>" } } Note: Include only one callback method for an event.
- Navigate to the server.js file.
- In the exports block, enter the callback function definition as follows:
Copied
Copy
EXPAND ↓1234567891011exports = { // args is a JSON block containing the payload information // args["iparam"] will contain the installation parameter values //eventCallbackMethod is the call-back function name specified in manifest.json eventCallbackMethod: function(args) { console.log("Logging arguments from the event: " + JSON.stringify(payload)); // If the setup is successful renderData(); // If there is an error during the setup renderData({message: "Error message"}); }};
Payload Attributes
When an app setup event occurs, a payload is passed to the callback method.
Copied Copy1 2 3 4 5 6 7 8 9 10 11 | { "account_id" : "value", "domain" : "value", "event" : "value", "region" : "value", "timestamp" : "value", "iparams" : { "Param1" : "value", "Param2" : "value" } } |
The payload is a JSON object with the following attributes.
Attribute | Type | Description |
---|---|---|
account_id | string | Identifier of the Freshcaller account, auto-generated when the account is configured for a business. |
domain | string | Domain name for the Freshcaller account. For example, acn.freshcaller.com. |
event | string | Identifier of the app setup event. Possible values: onAppInstall, onAppUninstall. |
region | string | Region where the Freshcaller account is deployed. Possible values: US, EU, EUC, AUS, and IND. |
timestamp | number | Timestamp of when the app setup event occurs, specified in the epoch format. |
iparams | object | Installation parameters specified as a JSON object of <parameter name>: <parameter value> pairs. |
Sample payload Copied Copy
1 2 3 4 5 6 7 | { "timestamp": 1583839686, "account_id": "12345", "domain": "https://sample.freshcaller.com", "event": "onAppInstall", "region": "US" } |
onAppInstall
When an app user clicks the Install button corresponding to the app, the onAppInstall event is triggered.
For app installation to reach completion, in the callback function definition, use the renderData() method without any arguments. To disallow app installation if a mandatory action fails, use an error object as the argument - renderData({error-object}).
Attribute Name | Data Type | Description |
---|---|---|
message Mandatory |
string | Message indicating why the mandatory action failed. Should not exceed 60 characters. |
For example,
renderData({message: "Installation failed due to network error."});Register the onAppInstall event by using the following sample manifest.json content:
Copied Copy1 2 3 4 5 | "events": { "onAppInstall": { "handler": "onAppInstallCallback" } } |
Define the corresponding callback that allows completion of installation by using the following sample server.js content:
Copied Copy1 2 3 4 5 6 7 | exports = { onAppInstallCallback: function(payload) { console.log("Logging arguments from onAppInstallevent: " + JSON.stringify(payload)); // If the setup is successful renderData(); } } |
Define the corresponding callback that disallows installation if a mandatory action fails, by using the following sample server.js content:
Copied Copy1 2 3 4 5 6 7 | exports = { onAppInstallCallback: function(payload) { console.log("Logging arguments from onAppInstallevent: " + JSON.stringify(payload)); // If there is an error during the setup, see screenshot below renderData({message: "Invalid API Key"}); } } |
afterAppUpdate
After you upload a new version of your app, in the Apps gallery > MANAGE APPS an Update button is displayed next to the app (for admin users). If the admin clicks the button and updates the app, the new app version is installed and the afterAppUpdate event is triggered.
In your new version’s app files, you can:
- Subscribe to the event by registering the event and its corresponding callback function.
- Use the registered callback function to carry out certain app actions. To let an app user continue using the updated version, in the callback function definition, as part of the app logic, use the renderData() method without any arguments.
- If any mandatory action that must be carried out as part of the app setup (update) fails, if the mandatory parameters that are required to carry out the app actions are unavailable, or in any such error scenarios, use the registered callback function to revert to the earlier installed app version. To perform this, use an error object as the argument of the renderData() method - renderData({error-object}).
Attribute Name | Data Type | Description |
---|---|---|
message Mandatory |
string | Message indicating why the update failed. Should not exceed 60 characters. |
For example,
renderData({message: "Invalid API key. App reverted to previous version. Try updating again."});Register the afterAppUpdate event by using the following sample manifest.json content:
Copied Copy1 2 3 4 5 | "events": { "afterAppUpdate": { "handler": "afterAppUpdateCallback" } } |
Define the corresponding callback that does not interrupt usage of the updated app, by using the following sample server.js content:
Copied Copy1 2 3 4 5 6 7 | exports = { afterAppUpdateCallback: function(payload) { console.log("Logging arguments from afterAppUpdate event: " + JSON.stringify(payload)); // To continue usage of the updated app. renderData(); } } |
Define the corresponding callback that reverts the app to the earlier installed version if a mandatory action fails, by using the following sample server.js content:
Copied Copy1 2 3 4 5 6 7 | exports = { afterAppUpdateCallback: function(payload) { console.log("Logging arguments from afterAppUpdate event: " + JSON.stringify(payload)); // To revert to earlier installed app version. renderData({message: "Updating to the latest version of the app failed due to network error."}); } } |
onAppUninstall
When an app user clicks the Uninstall button corresponding to the app, the onAppUninstall event is triggered.
For app uninstallation to reach completion, in the callback function definition, use the renderData() method without any arguments. To disallow app uninstallation if a mandatory action fails, use an error object as the argument - renderData({error-object}).
Attribute Name | Data Type | Description |
---|---|---|
message Mandatory |
string | Message indicating why the mandatory action failed. Should not exceed 60 characters. |
For example,
renderData({message: "Uninstallation failed due to network error."});Register the onAppUninstall event by using the following sample manifest.json content:
Copied Copy1 2 3 4 5 | "events": { "onAppUninstall": { "handler": "onAppUninstallCallback" } } |
Define the corresponding callback that allows completion of uninstallation by using the following sample server.js content:
Copied Copy1 2 3 4 5 6 7 | exports = { onAppUninstallCallback: function(payload) { console.log("Logging arguments from onAppUninstallevent: " + JSON.stringify(payload)); // If all mandatory uninstallation actions are successful renderData(); } } |
Define the corresponding callback that disallows uninstallation if a mandatory action fails, by using the following sample server.js content:
Copied Copy1 2 3 4 5 6 7 | exports = { onAppUninstallCallback: function(payload) { console.log("Logging arguments from onAppUninstallevent: " + JSON.stringify(payload)); // If there is an error during uninstallation renderData({message: "Uninstallation failed due to network error"}); } } |
Test
For information on how to test app setup events, see Test the App.