Back

Step by Step Guide to Building an Acuity Scheduling API Integration in Go

Aug 11, 2024 • 5 minute read

Introduction

Hey there, fellow Go enthusiast! Ready to dive into the world of appointment scheduling? We're about to embark on a journey to integrate the Acuity Scheduling API into your Go project. This powerful API will let you manage appointments like a pro, and we'll make it happen with some slick Go code.

Prerequisites

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

  • Go installed on your machine (you're a Go dev, so I'm sure you've got this covered!)
  • An Acuity Scheduling account with API credentials (if you don't have one, go grab it real quick)

Setting up the project

Let's kick things off by creating a new Go project:

mkdir acuity-integration cd acuity-integration go mod init acuity-integration

Now, let's grab the HTTP client we'll need:

go get github.com/go-resty/resty/v2

Authentication

Acuity uses API key authentication. Let's set that up:

package main import ( "github.com/go-resty/resty/v2" ) const ( baseURL = "https://acuityscheduling.com/api/v1" userID = "YOUR_USER_ID" apiKey = "YOUR_API_KEY" ) func main() { client := resty.New(). SetBaseURL(baseURL). SetBasicAuth(userID, apiKey) // We'll use this client for all our requests }

Making API requests

Now that we've got our authenticated client, let's make some requests!

func getAppointmentTypes(client *resty.Client) ([]AppointmentType, error) { var types []AppointmentType _, err := client.R(). SetResult(&types). Get("/appointment-types") return types, err }

Core API functionalities

Let's implement some key features:

func getAvailability(client *resty.Client, appointmentTypeID int) ([]TimeSlot, error) { var slots []TimeSlot _, err := client.R(). SetQueryParam("appointmentTypeID", strconv.Itoa(appointmentTypeID)). SetResult(&slots). Get("/availability/times") return slots, err } func createAppointment(client *resty.Client, appointment Appointment) (CreatedAppointment, error) { var created CreatedAppointment _, err := client.R(). SetBody(appointment). SetResult(&created). Post("/appointments") return created, err }

Error handling and response parsing

Don't forget to handle those pesky errors:

if err != nil { if restErr, ok := err.(*resty.ResponseError); ok { fmt.Printf("API error: %v\n", restErr.Response.Status()) // Handle specific error codes here } else { fmt.Printf("Request error: %v\n", err) } return }

Implementing a simple CLI interface

Let's make it interactive:

func main() { // ... client setup ... types, err := getAppointmentTypes(client) if err != nil { log.Fatal(err) } fmt.Println("Available appointment types:") for i, t := range types { fmt.Printf("%d. %s\n", i+1, t.Name) } // ... user input and appointment creation ... }

Testing the integration

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

func TestGetAppointmentTypes(t *testing.T) { client := setupTestClient() types, err := getAppointmentTypes(client) assert.NoError(t, err) assert.NotEmpty(t, types) }

Best practices and optimization

Remember to respect rate limits and consider caching frequently accessed data to optimize your integration.

Conclusion

And there you have it! You've just built a solid Acuity Scheduling API integration in Go. From here, you can expand on this foundation to create more complex scheduling applications. Keep coding, and happy scheduling!