Back

Step by Step Guide to Building a Freshdesk API Integration in Go

Aug 12, 20245 minute read

Introduction

Hey there, fellow Go enthusiast! Ready to supercharge your support system with Freshdesk? You're in the right place. We're going to walk through building a Freshdesk API integration using Go and the nifty github.com/fundedcity/freshdesk package. Buckle up!

Prerequisites

Before we dive in, make sure you've got:

  • Go installed (I know, obvious, right?)
  • A Freshdesk account with an API key
  • Some basic Go skills and a general idea of how RESTful APIs work

Got all that? Great! Let's roll.

Setting up the project

First things first, let's get our project off the ground:

mkdir freshdesk-integration cd freshdesk-integration go mod init freshdesk-integration go get github.com/fundedcity/freshdesk

Easy peasy, right?

Initializing the Freshdesk client

Now, let's get that Freshdesk client up and running:

package main import ( "github.com/fundedcity/freshdesk" ) func main() { client := freshdesk.NewClient("your-api-key", "your-domain.freshdesk.com") // We're ready to rock! }

Basic operations

Retrieving tickets

Let's fetch some tickets:

tickets, _, err := client.Tickets.List(&freshdesk.TicketListOptions{}) if err != nil { // Handle the error like a pro } // Do something awesome with the tickets

Creating a new ticket

Time to create a ticket:

newTicket := &freshdesk.Ticket{ Subject: "Houston, we have a problem", Description: "Just kidding, everything's fine!", Status: freshdesk.StatusOpen, Priority: freshdesk.PriorityMedium, } ticket, _, err := client.Tickets.Create(newTicket)

Updating a ticket

Oops, let's update that ticket:

updateTicket := &freshdesk.Ticket{ Status: freshdesk.StatusPending, } updatedTicket, _, err := client.Tickets.Update(ticket.ID, updateTicket)

Deleting a ticket

And if we need to, we can delete a ticket:

_, err := client.Tickets.Delete(ticket.ID)

Advanced usage

Working with custom fields

Custom fields? No problem:

newTicket := &freshdesk.Ticket{ Subject: "Custom ticket", CustomFields: map[string]interface{}{ "custom_field_1": "Value 1", "custom_field_2": 42, }, }

Handling pagination

For those long lists of tickets:

options := &freshdesk.TicketListOptions{ Page: 1, PerPage: 30, } for { tickets, resp, err := client.Tickets.List(options) // Process tickets if resp.NextPage == 0 { break } options.Page = resp.NextPage }

Error handling and rate limiting

Be nice to the API:

if err != nil { if rateLimitErr, ok := err.(*freshdesk.RateLimitError); ok { time.Sleep(rateLimitErr.RetryAfter) // Retry the request } // Handle other errors }

Best practices

  • Keep your API key safe! Use environment variables or a secure config management system.
  • Optimize your API calls. Batch operations when possible and use pagination wisely.

Testing the integration

Don't forget to test! Here's a quick example:

func TestCreateTicket(t *testing.T) { client := freshdesk.NewClient("test-api-key", "test-domain.freshdesk.com") newTicket := &freshdesk.Ticket{Subject: "Test Ticket"} ticket, _, err := client.Tickets.Create(newTicket) assert.NoError(t, err) assert.Equal(t, "Test Ticket", ticket.Subject) }

Conclusion

And there you have it! You're now equipped to build a robust Freshdesk integration in Go. Remember, the github.com/fundedcity/freshdesk package documentation is your friend for more advanced features.

Now go forth and create some awesome support systems! Happy coding!