Your First Serverless App

The goal of this tutorial is to help you understand the basics of serverless app development. Here, you will build a new serverless app using the Freshworks platform.

For this tutorial, we will choose the your_first_serverless_app template.

Create

Note:
Before proceeding, ensure that you have installed Node.js and Freshworks CLI.

To get started, open the terminal window and navigate to the directory under which you want to create your app. Note that this directory must be empty. $ fdk create --products freshdesk --template your_first_serverless_app

The app is created with the following directories and files.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
├── README.md ├── config │   ├── iparam_test_data.json │   └── iparams.json ├── manifest.json └── server ├── lib │   └── handle-response.js ├── server.js └── test_data ├── onAppInstall.json ├── onAppUninstall.json ├── onContactCreate.json ├── onContactUpdate.json ├── onConversationCreate.json ├── onExternalEvent.json ├── onTicketCreate.json └── onTicketUpdate.json 4 directories, 14 files
EXPAND ↓
Code Walkthrough

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

README.md

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 manifest file is used to store configuration information for your app.

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

The platform-version key is automatically generated and used to infer the platform version that your app uses. This is used to ensure backward compatibility when a new version of the framework is released.

The product key denotes the Freshworks product for which the app is applicable. Apps containing a UI component would also have to specify a location in the product key. For this app, the location is not specified as there is no UI component.

server/server.js

This 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: "onTicketCreate", callback: "onTicketCreateHandler" } ], // args is a JSON block containing the payload information. // args["iparam"] will contain the installation parameter values. onTicketCreateHandler: function(args) { console.log("Hello" + args["data"]["requester"]["name"]); } };
EXPAND ↓

Lines 3-5 contain the event registration code. The callback method onTicketCreateHandler binds itself to a onTicketCreate event, which means that it is run every time a new ticket is created in a Freshdesk account. When invoked, the callback method accesses the payload to find the ticket requester’s name which is then printed on the terminal 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. Take a look at the Serverless request app which uses library files.

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
3
4
5
6
7
8
9
├───test_data ├── onAppInstall.json ├── onAppUninstall.json ├── onContactCreate.json ├── onContactUpdate.json ├── onConversationCreate.json ├── onExternalEvent.json ├── onTicketCreate.json └── onTicketUpdate.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 more sample apps that use the serverless feature.

If you run into any issues, you can reach us at support@freshdesk.com or via the chat icon at the bottom-right corner of the screen.

Log in with your Freshdesk account

Enter your helpdesk URL to proceed to login

Proceed

By clicking "Proceed", you agree to our Terms of Use.