Published on

Retrieving Freshdesk Support Ticket via API in Node.js

Authors

Freshdesk is a widely used customer support system used by many companies. In this post, let's see how to use the API to get all the details related to a support ticket.

You can do a lot of things with it. For example, you can leverage AI and build a ticket deflection system based on your past support system and internal knowledge.

Prerequisites

You will need to have an API key (obviously!) and also know the domain for your Freshdesk account.

Dependencies

Create a new Node.js project

mkdir freshdesk-ticket-fetcher && $_
npm init -y; npm pkg set type="module";

And install the required package. In my case, I'll be using axios for interacting with APIs. You can use fetch itself if it's supported in your environment, like in the latest Node.js or some edge environment.

npm install axios

Code Snippet

import axios from 'axios';

const apiKey = process.env.FRESHDESK_API_KEY; // 
const domain = 'your-company-here';

const ticketId = 123456;

const fetchSupportTicket = async (ticketId) => {
  try {
    return axios.get(
      `https://${domain}.freshdesk.com/api/v2/tickets/${ticketId}`,
      {
        headers: {
          "Content-Type": "application/json",
          Authorization: `Basic ${Buffer.from(apiKey).toString("base64")}`,
        },
      },
    );
  } catch (error) {
    // Handle errors
    console.log(error);
  }
};

Happy fetching data!