Your First Backend App

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

To do this, we will start off with an extremely basic sample backend app and understand how it works. This app runs everytime a new ticket is created. It then accesses the payload of the ticket create event to get the ticket requester’s name and prints it to the console.

Setup

Note:
1. Ensure that you have installed Node.js and the Freshdesk SDK and Developer Extension.
2. This tutorial was created using v2.3.2 of the SDK.


To get started first visit the sample apps page on github and download the entire repository.

Next unzip the repository into a folder on your machine.

Navigate to the your_first_backend_app folder and run the following command to view the app's folder structure:

$ tree your_first_backend_app
Code Walkthrough

Let’s take a closer look at the files that were created in the your_first_backend_app folder.


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.yml

The manifest file is used to store configuration information for your app.

Copied Copy
1
2
3
4
5
platform-version: "1.0" features: - backend pages: - ticket

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 features key lists the advanced features that the app uses. The ability to execute based on backend events is an advanced feature and hence it is listed here. This value will be used by the SDK when a new app is created to ensure that all required files are in place. It will also be used when any validation is run.


app/app.js

This file exposes a function initialize that is called whenever your app is rendered in the user's UI on pages given in the manifest. It is expected to be written in standard ES5 JS.

Copied Copy
1
2
3
4
5
6
7
8
(function() { "use strict"; return { initialize: function() { // Empty method as this is a pure backend app } }; })();

Since this is a backend app, with no UI component, you can leave the method empty. However, the method itself must be present and an error will occur if it isn't.

Similarly, the two other files in the app folder - app/template.html and app/style.css files must be present but can be left empty.


backend/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 Freshdesk backend server.

Copied Copy
1
2
3
4
5
6
7
8
9
10
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"]); } };

Lines 2-4 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 then accesses the payload to find the ticket requester’s name which is then printed to the terminal window.

One important thing to notice is that the event registration and callback code is written inside an exports array (line 1,10). This ensures that the code can be imported and executed by the server. Helper methods can be placed outside the exports array.


backend/lib/

This folder can be used to store library files that contain helper methods for your server.js file to use. This will enable you to make your code modular and maintainable. Take a look at the Sample backend request app for an example of this. This app does not use any helper methods and hence the directory is empty.


backend/test_data/

To test a backend app, you have to simulate events using the Freshdesk Developer Extension. 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. 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 backend app, why not build your own?

If you want to see more complex backend apps, we have more sample apps that use the backend feature. These include:

If you run into any issues, you can reach out to 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.