How to use Axios to make API calls in Freshworks apps?

In Freshworks apps, there are multiple ways to make API requests. The Freshworks developer platform provides the Request Method feature to make API without any dependencies.

The Request Method provides several benefits for making API requests.

  1. Allowing secure credentials and information to be included in requests made from the browser
  2. Overcoming the need for the CORS behavior to avoid restrictions for accessing cross-domain resources.
  3. It provides the Static IP feature to enable the source IP of the API requests to be from the same set of IP addresses that can be added to allowlist in your private network.

The Request Method has some boundaries.

  1. Rate limit: 50 per minute for default. It is extendable.
  2. Timeout: 6 seconds by default. It is extendable for up to 10 seconds.
  3. Content types supported: application/json, application/x-www-form-urlencoded, multipart/form-data, text/html, and application/xml
  4. Request and Response payload size limit: 100 KB

While these limitations can be overcome with a third-party HTTP library, doing so would require sacrificing the benefits provided by the Request Method. In cases where the advantages of the Request Method are not necessary for an app, it may be a good idea to use a third-party HTTP library. If this is required, any NPM library can be used in Freshworks apps.

In the Serverless environment, the benefits of the Request Method related to secure credentials and CORS behavior are not applicable, as everything occurs in a secure headless environment that cannot be accessed by unauthorized users.

With this context, let’s use the Axios library to make API requests from the Freshworks apps.

Note: For this example, we use Axios. But any other HTTP library can also be used to make API requests and other NPM libraries for other purposes in your app.

To use the Axios library for making API requests in Freshworks apps, you must add it to the dependencies list on the manifest.json file. This can be done by specifying the library and its version in the “dependencies” object, as shown in the following code snippet:

{
  ...
  "dependencies": {
    "axios": "1.3.5"
  }
}

In this example,

  • We have added the latest version of the Axios library as a dependency. However, you can use any available version of NPM libraries.
  • The mentioned dependencies will be downloaded and imported to use readily in the serverless apps.

Note: If an NPM library is too big to download with the Serverless compute power the app gets in the set timeout, the app will fail. Check out the next steps for this case.

After adding Axios as a dependency, you can use it in your Freshworks app serverless component. The following code snippet demonstrates how to use Axios in a serverless app:

const axios = require('axios/dist/node/axios.cjs'); // Importing axios library

// API endpoint
const URL = 'https://subdomain.freshdesk.com/api/v2/tickets';

// API headers and other options
const OPTIONS = {
  "headers": {
    "Authorization": "Basic xxxxxxxxxxxxxxxxxxxx",
    "Content-Type": "application/json"
  }
}

try {
  const response = await axios.get(URL, OPTIONS);
  console.info(response);
} catch (error) {
  console.error(error);
});

In this example,

  • We first import the Axios library by requiring it. Notice that we specifically import the common JS module since Axios exports the ES module by default in this library version.
  • Then, we define the API endpoint URL and options we want to use for the API request.
  • We then use Axios to send a GET request to the specified URL and log the response data to the console. Additionally, we catch any error scenarios and print the error to the console.

Note: Depending on the specific API you are accessing, you may need to set up authentication and authorization headers or other request parameters.

2 posts were split to a new topic: Unable to pack app with axios common js import

Hello @Raviraj , With the upgrade to fdk-9.0.5. We noticed that running the fdk run command using the specified import statement for axios in your example

const axios = require(‘axios/dist/node/axios.cjs’); // Importing axios library

resulted in us having the following errors

The local server could not be started due to the following issue(s):
:heavy_multiplication_x: server\lib\conversationCreate.js::5: ‘axios/dist/node/axios.cjs’ is not listed in manifest.
:heavy_multiplication_x: server\lib\smsClient.js::2: ‘axios/dist/node/axios.cjs’ is not listed in manifest.
:heavy_multiplication_x: server\server.js::1: The following dependencies are not used: axios

From the error above, you can denote that our freshdesk app is a serverless application.
We changed the import statement to this though,

const axios = require(‘axios’);

After doing that the fdk run command works however our serverless app has stopped working. trace logs are no longer being seen when we interact with a test ticket, however when we roll back to a previous version of this app we see the trace logs

1 Like

Hey @ChukwuemekaNweke,

Have you found another solution? I am facing the same issue

Thank you!

@ChukwuemekaNweke I used the request library as a work around if you are still looking for one

@Raviraj the issue with the SDK needs to be resolved, could we get an ETA?

Hi @Kiska_Sanchez @ChukwuemekaNweke,

We have recorded a feature request to support ES module dependencies in the Serverless apps.

The error from FDK cannot be removed as it prevents the right use case where a dependency might be used without defining them in the manifest.json file.

Please use any other library that supports commonJS module for now. Needle and Node-fetch (V2) support the CommonJS module.