Add note with attachments on freshservice ticket with python

When working with the Freshservice API, you may come across the need to add notes with attachments to a ticket using Python. In this article, we will explore three different ways to achieve this task.

Option 1: Using the requests library

The first option involves using the popular requests library in Python to make HTTP requests to the Freshservice API. Here’s a sample code snippet that demonstrates how to add a note with attachments to a ticket:


import requests

def add_note_with_attachments(ticket_id, note, attachments):
    url = f"https://yourdomain.freshservice.com/api/v2/tickets/{ticket_id}/reply"
    headers = {
        "Content-Type": "application/json",
        "Authorization": "Bearer your_api_key"
    }
    payload = {
        "body": note,
        "attachments": attachments
    }
    response = requests.post(url, headers=headers, json=payload)
    if response.status_code == 201:
        print("Note added successfully!")
    else:
        print("Failed to add note.")

# Usage example
ticket_id = 12345
note = "This is a sample note."
attachments = ["path/to/attachment1.txt", "path/to/attachment2.txt"]
add_note_with_attachments(ticket_id, note, attachments)

This code snippet uses the requests library to send a POST request to the Freshservice API’s ticket reply endpoint. The note content and attachments are passed as JSON payload in the request body. The API key is included in the Authorization header for authentication.

Option 2: Using the freshdesk Python SDK

If you prefer a more high-level approach, you can utilize the freshdesk Python SDK, which provides a convenient wrapper around the Freshservice API. Here’s an example of how to use the SDK to add a note with attachments:


from freshdesk.api import API

def add_note_with_attachments(ticket_id, note, attachments):
    api = API(api_key="your_api_key", domain="yourdomain.freshservice.com")
    response = api.tickets.reply(ticket_id, body=note, attachments=attachments)
    if response.status_code == 201:
        print("Note added successfully!")
    else:
        print("Failed to add note.")

# Usage example
ticket_id = 12345
note = "This is a sample note."
attachments = ["path/to/attachment1.txt", "path/to/attachment2.txt"]
add_note_with_attachments(ticket_id, note, attachments)

This code snippet utilizes the freshdesk Python SDK to create an API instance and call the `tickets.reply` method to add a note with attachments to a ticket. The SDK takes care of authentication and simplifies the API interaction.

Option 3: Using the freshservice Python SDK

If you specifically want to work with the Freshservice API, you can use the freshservice Python SDK, which is similar to the freshdesk SDK but tailored for Freshservice. Here’s an example of how to use the freshservice SDK to achieve the same task:


from freshservice.api import API

def add_note_with_attachments(ticket_id, note, attachments):
    api = API(api_key="your_api_key", domain="yourdomain.freshservice.com")
    response = api.tickets.reply(ticket_id, body=note, attachments=attachments)
    if response.status_code == 201:
        print("Note added successfully!")
    else:
        print("Failed to add note.")

# Usage example
ticket_id = 12345
note = "This is a sample note."
attachments = ["path/to/attachment1.txt", "path/to/attachment2.txt"]
add_note_with_attachments(ticket_id, note, attachments)

This code snippet is similar to the previous option but uses the freshservice Python SDK instead. The SDK provides a more tailored experience for working with the Freshservice API.

After exploring these three options, it is evident that using the freshservice Python SDK is the better choice when working with the Freshservice API. It provides a more streamlined and intuitive way to interact with the API, reducing the amount of boilerplate code required. Additionally, the SDK handles authentication and simplifies the API calls, making the code more readable and maintainable.

Rate this post

4 Responses

  1. Option 2 is definitely the way to go! The freshdesk Python SDK makes life so much easier. Who needs the hassle of using requests library?

    1. I respectfully disagree. While the freshdesk Python SDK may be convenient, the requests library offers greater flexibility and control. Why limit yourself to a specific SDK when you can have the freedom to customize your requests as needed?

Leave a Reply

Your email address will not be published. Required fields are marked *

Table of Contents