Your First App

In this section, you can create a serverless app based on the your_first_serverless_app template.

Create

Note: Ensure that you installed Node.js and Freshworks CLI.

From the command line, navigate to the empty directory in which you want to create an app and run the following command. $ fdk create --products freshteam --template your_first_serverless_app

A new app is created with the following directories and files, based on the your_first_serverless_app template.

1
2
3
4
5
6
7
8
9
10
11
12
13
├── README.md ├── config │   ├── iparam_test_data.json │   └── iparams.json ├── manifest.json └── server ├── lib │   └── handle-response.js ├── server.js └── test_data └── onEmployeeCreate.json 4 directories, 7 files
EXPAND ↓
Code Walkthrough

Let’s take a closer look at the files that were created in the serverless app.

README.md

The file contains additional instructions, information, and specifications. Developers need to maintain a proper README file so when there is a change in ownership of the code base, a new developer can easily understand the various nuances of the code.

manifest.json

The file contains details about the app and its characteristics.

Copied Copy
1
2
3
4
5
6
{ "platform-version": "2.0", "product": { "freshteam": {} } }

When the app files are created, the platform-version is auto-generated and specifics the platform version used to build the app. The version information is used to ensure backward compatibility when a new platform version is released.

The product object associates a Freshworks product with the information that is necessary to render the app on the specified product.

server/server.js

The file contains code which is executed on a Freshworks server every time a specific event occurs. The file also contains registration wherein callback methods bind themselves to specific events.

Copied Copy
1
2
3
4
5
6
7
8
9
10
11
12
exports = { events: [ { event: "onEmployeeCreate", callback: "onEmployeeCreateHandler" } ], // args is a JSON block containing the payload information. // args["iparam"] will contain the installation parameter values. onEmployeeCreateHandler: function(args) { console.log("Hello" + args["data"]["requester"]["name"]); } };
EXPAND ↓

Lines 3-5 contain the event registration code. The callback method onEmployeeCreateHandler binds itself to a onEmployeeCreate event, which means that it is run every time a new employee is created in a Freshteam account. When invoked, the callback method accesses the payload to find the employee's name which is then printed on the computer window.

The event registration and callback code is written in an exports object which ensures that the code can be imported and executed by the server. Helper methods can also be placed outside the exports object.

server/lib/

The sample app uses this folder to store library files which contain helper methods that you could use in your server.js file. Note that your library files can be added anywhere in the server/ folder. This will enable you to make your code modular and maintainable. Although there is a library file present in this folder, this app does not actually use it.

server/test_data/

To test a serverless app, you have to simulate events that trigger the app. When an event is simulated, a sample payload containing details about the corresponding event is sent to the registered callback. The files containing the payload are present in this folder.

1
2
├───test_data └── onEmployeeCreate.json

There is one file for every backend event that is supported. You can modify the content of the files in this folder to test various use cases. Just remember not to change the names of the files.

Next Steps

Now that you understand the basic components of a serverless app, why not build your own?

Here are a few Freshteam and Freshdesk sample apps that use the serverless feature.

If you run into any issues, you can reach us at support@freshteam.com.