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.
- Allowing secure credentials and information to be included in requests made from the browser
- Overcoming the need for the CORS behavior to avoid restrictions for accessing cross-domain resources.
- 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.
- Rate limit: 50 per minute for default. It is extendable.
- Timeout: 6 seconds by default. It is extendable for up to 10 seconds.
- Content types supported:
application/json
,application/x-www-form-urlencoded
,multipart/form-data
,text/html
, andapplication/xml
- 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.