Back

Step by Step Guide to Building a Cal.com API Integration in Go

Aug 16, 20247 minute read

Introduction

Hey there, fellow developer! Ready to dive into the world of Cal.com API integration using Go? You're in for a treat. Cal.com's API is a powerful tool that allows you to tap into their scheduling capabilities, and with Go's simplicity and efficiency, we're going to create something awesome. Let's get started!

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!)
  • A Cal.com account and API key (if you don't have one, hop over to Cal.com and set it up)
  • Your favorite code editor ready to rock

Setting up the project

Alright, let's kick things off by creating a new Go project:

mkdir calcom-integration cd calcom-integration go mod init github.com/yourusername/calcom-integration

Now, let's grab the packages we'll need:

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

We're using resty to make our HTTP requests nice and easy.

Authentication

Time to get that API key working for us. Create a new file called main.go and let's set up our client:

package main import ( "github.com/go-resty/resty/v2" ) const ( baseURL = "https://api.cal.com/v1" apiKey = "your-api-key-here" ) func main() { client := resty.New(). SetBaseURL(baseURL). SetHeader("Authorization", "Bearer "+apiKey) // We'll use this client for our requests }

Making API requests

Now that we're all set up, let's make some requests! Here's how you can fetch user information:

resp, err := client.R(). SetResult(&UserInfo{}). Get("/me") if err != nil { log.Fatalf("Error fetching user info: %v", err) } userInfo := resp.Result().(*UserInfo) fmt.Printf("Hello, %s!\n", userInfo.Name)

And here's how you might create an event:

event := Event{ Title: "Coffee Chat", Length: 30, } resp, err := client.R(). SetBody(event). SetResult(&CreatedEvent{}). Post("/events") if err != nil { log.Fatalf("Error creating event: %v", err) } createdEvent := resp.Result().(*CreatedEvent) fmt.Printf("Event created with ID: %s\n", createdEvent.ID)

Handling responses

As you can see, we're using structs to handle our responses. Define them like this:

type UserInfo struct { Name string `json:"name"` Email string `json:"email"` } type Event struct { Title string `json:"title"` Length int `json:"length"` } type CreatedEvent struct { ID string `json:"id"` }

Remember to always check for errors and handle them gracefully!

Implementing key features

Now that you've got the basics down, you can implement more complex features. Here's a quick example of fetching available time slots:

resp, err := client.R(). SetQueryParams(map[string]string{ "eventTypeId": "your-event-type-id", "date": "2023-06-01", }). SetResult(&AvailableSlots{}). Get("/availability") if err != nil { log.Fatalf("Error fetching availability: %v", err) } slots := resp.Result().(*AvailableSlots) for _, slot := range slots.Slots { fmt.Printf("Available slot: %s\n", slot.Start) }

Testing the integration

Don't forget to test your code! Here's a simple example using the built-in testing package:

func TestFetchUserInfo(t *testing.T) { // Set up your client here resp, err := client.R(). SetResult(&UserInfo{}). Get("/me") if err != nil { t.Fatalf("Error fetching user info: %v", err) } userInfo := resp.Result().(*UserInfo) if userInfo.Name == "" { t.Error("Expected non-empty user name") } }

Best practices and optimization

Remember to implement rate limiting to stay within Cal.com's API limits. You can use a package like golang.org/x/time/rate for this.

Also, consider caching responses for frequently accessed data to reduce API calls and improve performance.

Conclusion

And there you have it! You've just built a Cal.com API integration in Go. Pretty cool, right? Remember, this is just the beginning. There's so much more you can do with the Cal.com API, so don't be afraid to explore and experiment.

Keep coding, keep learning, and most importantly, have fun with it! If you run into any issues, the Cal.com API docs and the Go community are great resources. Happy coding!