Your First Serverless App

The goal of this tutorial is to help you understand the basics of app development using the Freshworks app development platform.

To do this, we will create a new serverless app and understand how it works. Every time you create a new app using the Freshworks CLI, you have to choose a starting template. For this tutorial, we will choose the your_first_serverless_app template.

Create

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


Let’s get started by creating the app. First 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 freshservice --template your_first_serverless_app

The app will be created with the directories and files shown below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
├── README.md ├── config │   ├── iparam_test_data.json │   └── iparams.json ├── manifest.json └── server ├── lib │   └── handle-response.js ├── server.js └── test_data ├── 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

It is considered a sacred pact amongst developers to maintain a proper README file so that when there is a change in ownership of the code base, the new developer will have an easier time understanding 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": { "freshservice": {} } }

The platform-version key is automatically generated and used to infer the platform version that your app uses. This will be 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 inside the product key. Since this app does not have a UI component, it is empty.


server/server.js

This file contains the code that gets executed every time a specific event occurs. It also contains the registration wherein callback methods bind themselves to specific events. The execution happens on a Freshworks server.

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 Freshservice account. When invoked, the callback method then accesses the payload to find the ticket requester’s name which is then printed to the terminal window.

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


server/lib/

This sample app uses this folder to store library files that contain helper methods for your server.js file to use. Note that your library files can be added anywhere within 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 the events that trigger the app. When an event is simulated, a sample payload containing details about the corresponding event will be 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 will be one file for every backend event that is supported. You can modify the content of the files in this folder to test out 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?

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

Log in with your Freshservice account

Enter your helpdesk URL to proceed to login

Proceed

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