Back

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

Aug 1, 20248 minute read

Introduction

Hey there, fellow developer! Ready to dive into the world of Zendesk API integration using Go? You're in for a treat. We'll be using the awesome github.com/nukosuke/go-zendesk/zendesk package to make our lives easier. Let's get cracking!

Prerequisites

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

  • Go installed on your machine
  • A Zendesk account with API credentials handy

Got those? Great! Let's move on.

Setting up the project

First things first, let's create a new Go module:

mkdir zendesk-integration && cd zendesk-integration go mod init zendesk-integration

Now, let's grab the go-zendesk package:

go get github.com/nukosuke/go-zendesk/zendesk

Initializing the Zendesk client

Time to get our hands dirty with some code. Open up your favorite editor and create a main.go file:

package main import ( "github.com/nukosuke/go-zendesk/zendesk" ) func main() { client, err := zendesk.NewClient(nil) if err != nil { panic(err) } client.SetSubdomain("your-subdomain") client.SetCredential(zendesk.NewAPITokenCredential("your-email", "your-token")) }

Replace "your-subdomain", "your-email", and "your-token" with your actual Zendesk credentials. Easy peasy!

Basic API operations

Fetching tickets

Let's grab some tickets:

tickets, _, err := client.Tickets.List(context.Background(), &zendesk.TicketListOptions{}) if err != nil { panic(err) } for _, ticket := range tickets { fmt.Printf("Ticket ID: %d, Subject: %s\n", ticket.ID, ticket.Subject) }

Creating a new ticket

Creating tickets is a breeze:

newTicket := &zendesk.Ticket{ Subject: "Houston, we have a problem", Comment: &zendesk.TicketComment{Body: "The coffee machine is broken!"}, } ticket, _, err := client.Tickets.Create(context.Background(), newTicket) if err != nil { panic(err) } fmt.Printf("Created ticket with ID: %d\n", ticket.ID)

Updating a ticket

Need to update a ticket? No sweat:

ticketID := 12345 // Replace with an actual ticket ID updateTicket := &zendesk.Ticket{ Status: "solved", } updatedTicket, _, err := client.Tickets.Update(context.Background(), ticketID, updateTicket) if err != nil { panic(err) } fmt.Printf("Updated ticket %d status to: %s\n", updatedTicket.ID, updatedTicket.Status)

Deleting a ticket

Sometimes you gotta say goodbye:

ticketID := 12345 // Replace with an actual ticket ID _, err := client.Tickets.Delete(context.Background(), ticketID) if err != nil { panic(err) } fmt.Printf("Deleted ticket %d\n", ticketID)

Working with users

Retrieving user information

Let's get some user details:

userID := 67890 // Replace with an actual user ID user, _, err := client.Users.Show(context.Background(), userID) if err != nil { panic(err) } fmt.Printf("User: %s %s, Email: %s\n", user.Name, user.Email)

Creating a new user

Adding a new user to the mix:

newUser := &zendesk.User{ Name: "John Doe", Email: "[email protected]", } user, _, err := client.Users.Create(context.Background(), newUser) if err != nil { panic(err) } fmt.Printf("Created user with ID: %d\n", user.ID)

Updating user details

People change, and so do their details:

userID := 67890 // Replace with an actual user ID updateUser := &zendesk.User{ Phone: "+1 555-1234", } updatedUser, _, err := client.Users.Update(context.Background(), userID, updateUser) if err != nil { panic(err) } fmt.Printf("Updated user %d phone to: %s\n", updatedUser.ID, updatedUser.Phone)

Handling attachments

Uploading attachments

Let's add some pizzazz with attachments:

file, err := os.Open("funny_cat.jpg") if err != nil { panic(err) } defer file.Close() upload, _, err := client.Attachments.Upload(context.Background(), file, "image/jpeg", "funny_cat.jpg") if err != nil { panic(err) } fmt.Printf("Uploaded attachment with token: %s\n", upload.Token)

Associating attachments with tickets

Now, let's attach that funny cat to a ticket:

newTicket := &zendesk.Ticket{ Subject: "Check out this funny cat!", Comment: &zendesk.TicketComment{ Body: "This cat is hilarious!", Uploads: []string{upload.Token}, }, } ticket, _, err := client.Tickets.Create(context.Background(), newTicket) if err != nil { panic(err) } fmt.Printf("Created ticket with ID: %d and attachment\n", ticket.ID)

Error handling and best practices

Always check for errors, folks! It's a good habit:

if err != nil { log.Printf("An error occurred: %v", err) // Handle the error appropriately }

And don't forget about rate limiting. Be a good API citizen:

time.Sleep(time.Second) // Add a delay between requests

Conclusion

And there you have it! You're now equipped to build awesome Zendesk integrations with Go. Remember, this is just scratching the surface. There's so much more you can do with the Zendesk API and the go-zendesk package.

Keep exploring, keep coding, and most importantly, have fun! If you need more info, check out the go-zendesk documentation and the Zendesk API docs.

Now go forth and integrate!