Back

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

Aug 16, 2024 • 7 minute read

Introduction

Hey there, fellow developer! Ready to dive into the world of Zendesk Sell API integration using Go? You're in for a treat. We'll be using the awesome go-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 (I know you probably do, but just checking!)
  • A Zendesk Sell account with API credentials (if you don't have this, go grab it real quick)

Setting up the project

First things first, let's set up our project:

mkdir zendesk-sell-integration cd zendesk-sell-integration go mod init github.com/yourusername/zendesk-sell-integration go get github.com/nukosuke/go-zendesk

Authenticating with Zendesk Sell API

Now, let's create a client to authenticate with the Zendesk Sell API:

import ( "github.com/nukosuke/go-zendesk/zendesk" ) client, err := zendesk.NewClient(nil) if err != nil { log.Fatal(err) } client.SetSubdomain("your_subdomain") client.SetCredential(zendesk.NewAPITokenCredential("your_email", "your_api_token"))

Basic API Operations

Fetching Leads

Let's grab some leads:

leads, _, err := client.GetLeads() if err != nil { log.Fatal(err) } for _, lead := range leads { fmt.Printf("Lead: %s\n", lead.Name) }

Creating a New Lead

Time to add a new lead:

newLead := &zendesk.Lead{ Name: "John Doe", Email: "[email protected]", } createdLead, _, err := client.CreateLead(newLead) if err != nil { log.Fatal(err) } fmt.Printf("Created lead: %s\n", createdLead.Name)

Updating a Lead

Let's update that lead:

createdLead.Name = "John Smith" updatedLead, _, err := client.UpdateLead(createdLead.ID, createdLead) if err != nil { log.Fatal(err) } fmt.Printf("Updated lead: %s\n", updatedLead.Name)

Deleting a Lead

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

_, err := client.DeleteLead(createdLead.ID) if err != nil { log.Fatal(err) } fmt.Println("Lead deleted successfully")

Advanced Operations

Searching for Leads

Need to find specific leads? No problem:

query := "email:[email protected]" searchResults, _, err := client.SearchLeads(query) if err != nil { log.Fatal(err) } for _, lead := range searchResults { fmt.Printf("Found lead: %s\n", lead.Name) }

Handling Pagination

For those long lists of leads:

page := 1 for { leads, resp, err := client.GetLeads(zendesk.PageOptions(page)) if err != nil { log.Fatal(err) } for _, lead := range leads { fmt.Printf("Lead: %s\n", lead.Name) } if resp.NextPage == 0 { break } page = resp.NextPage }

Working with Custom Fields

Got custom fields? We've got you covered:

lead.CustomFields = map[string]interface{}{ "custom_field_1": "Value 1", "custom_field_2": 42, } updatedLead, _, err := client.UpdateLead(lead.ID, lead) if err != nil { log.Fatal(err) }

Error Handling and Best Practices

Always check for errors (I know you know this, but it's worth repeating):

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

And don't forget about rate limiting! The go-zendesk package handles this for you, but be mindful of your API usage.

Testing the Integration

Writing tests is crucial. Here's a quick example:

func TestCreateLead(t *testing.T) { client, _ := zendesk.NewClient(nil) // Mock the API response httpmock.Activate() defer httpmock.DeactivateAndReset() httpmock.RegisterResponder("POST", "/api/v2/leads", httpmock.NewStringResponder(200, `{"lead": {"id": 1, "name": "Test Lead"}}`)) lead, _, err := client.CreateLead(&zendesk.Lead{Name: "Test Lead"}) assert.NoError(t, err) assert.Equal(t, "Test Lead", lead.Name) }

Conclusion

And there you have it! You've just built a Zendesk Sell API integration in Go. Pretty cool, right? Remember, this is just scratching the surface. The Zendesk Sell API has a ton more features you can explore.

For more info, check out the go-zendesk documentation and the Zendesk Sell API docs.

Now go forth and integrate! You've got this. 💪