Back

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

Aug 16, 20246 minute read

Introduction

Hey there, fellow Go enthusiast! Ready to dive into the world of customer feedback with Delighted? In this guide, we'll walk through building a slick integration with the Delighted API using Go. Whether you're looking to automate surveys or pull insights programmatically, you're in the right place. Let's get cracking!

Prerequisites

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

  • Go installed (I know you probably do, but just checking!)
  • A Delighted API key (grab one from your Delighted account)
  • Your favorite code editor at the ready

Setting up the project

Let's kick things off by setting up our project:

mkdir delighted-go-integration cd delighted-go-integration go mod init delighted-integration

Now, let's grab the Delighted Go client:

go get github.com/delighted/delighted-go

Initializing the Delighted client

Time to get our hands dirty with some code. Create a main.go file and let's set up our client:

package main import ( "fmt" "github.com/delighted/delighted-go" ) func main() { client := delighted.NewClient("YOUR_API_KEY") // We'll add more code here soon! }

Replace YOUR_API_KEY with your actual Delighted API key. Easy peasy!

Implementing key API functionalities

Creating a survey response

Let's add a function to create a survey response:

func createSurveyResponse(client *delighted.Client) { resp, err := client.CreateSurveyResponse(&delighted.SurveyResponseParams{ Score: 9, Comment: "Loving this Go integration!", }) if err != nil { fmt.Printf("Error creating survey response: %v\n", err) return } fmt.Printf("Created survey response: %+v\n", resp) }

Retrieving survey responses

Now, let's fetch some responses:

func getSurveyResponses(client *delighted.Client) { responses, err := client.ListSurveyResponses(&delighted.ListSurveyResponseParams{ PerPage: 10, }) if err != nil { fmt.Printf("Error retrieving survey responses: %v\n", err) return } for _, resp := range responses { fmt.Printf("Response: %+v\n", resp) } }

Listing people

Let's get a list of people:

func listPeople(client *delighted.Client) { people, err := client.ListPeople(&delighted.ListPeopleParams{ PerPage: 10, }) if err != nil { fmt.Printf("Error listing people: %v\n", err) return } for _, person := range people { fmt.Printf("Person: %+v\n", person) } }

Creating/updating a person

Here's how to create or update a person:

func createOrUpdatePerson(client *delighted.Client) { person, err := client.CreateOrUpdatePerson(&delighted.PersonParams{ Email: "[email protected]", Name: "Gopher", }) if err != nil { fmt.Printf("Error creating/updating person: %v\n", err) return } fmt.Printf("Created/Updated person: %+v\n", person) }

Unsubscribing a person

Finally, let's unsubscribe someone:

func unsubscribePerson(client *delighted.Client) { err := client.Unsubscribe("[email protected]") if err != nil { fmt.Printf("Error unsubscribing person: %v\n", err) return } fmt.Println("Person unsubscribed successfully") }

Error handling and best practices

As you've seen, we're checking for errors after each API call. This is crucial! Also, keep an eye on rate limits. The Delighted API has some restrictions, so consider implementing retries with exponential backoff for production use.

Testing the integration

Now that we've got our functions, let's tie it all together in our main function:

func main() { client := delighted.NewClient("YOUR_API_KEY") createSurveyResponse(client) getSurveyResponses(client) listPeople(client) createOrUpdatePerson(client) unsubscribePerson(client) }

Run your code with go run main.go and watch the magic happen!

Conclusion

And there you have it! You've just built a Delighted API integration in Go. Pretty cool, right? You can now automate surveys, manage contacts, and pull data like a pro. As you get more comfortable, try expanding on this foundation – maybe add some command-line flags or build a simple web interface.

Resources

Now go forth and delight your users with your newfound powers! Happy coding, Gophers! 🐹✨