Back

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

Aug 14, 20246 minute read

Introduction

Hey there, fellow Go enthusiast! Ready to supercharge your scheduling game? Let's dive into building a YouCanBookMe API integration. This nifty tool will let you programmatically manage bookings, fetch available slots, and more. Buckle up!

Prerequisites

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

  • Go installed (you're a Gopher, right?)
  • A YouCanBookMe account with an API key (if you don't have one, grab it from your account settings)

Setting up the project

Let's get our hands dirty:

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

We'll need a HTTP client, but Go's standard library has us covered. No extra dependencies required!

Authentication

YouCanBookMe uses API key authentication. Let's create a client that'll handle this for us:

type Client struct { apiKey string baseURL string httpClient *http.Client } func NewClient(apiKey string) *Client { return &Client{ apiKey: apiKey, baseURL: "https://api.youcanbook.me/v1", httpClient: &http.Client{}, } }

Making API requests

Now, let's create a method to make authenticated requests:

func (c *Client) doRequest(method, path string, body io.Reader) (*http.Response, error) { req, err := http.NewRequest(method, c.baseURL+path, body) if err != nil { return nil, err } req.Header.Set("Authorization", "Bearer "+c.apiKey) req.Header.Set("Content-Type", "application/json") return c.httpClient.Do(req) }

Implementing core functionalities

Let's implement some key features:

Fetching available time slots

func (c *Client) GetAvailableSlots(profileID string, start, end time.Time) ([]Slot, error) { path := fmt.Sprintf("/profiles/%s/slots?start=%s&end=%s", profileID, start.Format(time.RFC3339), end.Format(time.RFC3339)) resp, err := c.doRequest("GET", path, nil) if err != nil { return nil, err } defer resp.Body.Close() var slots []Slot err = json.NewDecoder(resp.Body).Decode(&slots) return slots, err }

Creating a booking

func (c *Client) CreateBooking(profileID string, booking Booking) (*Booking, error) { body, _ := json.Marshal(booking) resp, err := c.doRequest("POST", "/profiles/"+profileID+"/bookings", bytes.NewBuffer(body)) if err != nil { return nil, err } defer resp.Body.Close() var createdBooking Booking err = json.NewDecoder(resp.Body).Decode(&createdBooking) return &createdBooking, err }

Error handling and response parsing

Always check the response status and handle errors gracefully:

if resp.StatusCode != http.StatusOK { return fmt.Errorf("API request failed with status %d", resp.StatusCode) }

Creating a reusable YouCanBookMe client

Wrap all your methods in the Client struct we created earlier. This gives you a nice, reusable package:

client := ycbm.NewClient("your-api-key") slots, err := client.GetAvailableSlots("profile-id", start, end)

Testing the integration

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

func TestGetAvailableSlots(t *testing.T) { client := NewClient("test-api-key") slots, err := client.GetAvailableSlots("test-profile", time.Now(), time.Now().Add(24*time.Hour)) assert.NoError(t, err) assert.NotEmpty(t, slots) }

Best practices and optimization

  • Implement rate limiting to stay within API constraints
  • Cache frequently accessed data to reduce API calls
  • Use context for cancellation and timeouts

Conclusion

And there you have it! You've just built a solid YouCanBookMe API integration in Go. With this foundation, you can expand to cover more endpoints, build a CLI tool, or even create a full-fledged scheduling application. The possibilities are endless!

Remember, the best way to learn is by doing. So go ahead, tweak this code, break things, and most importantly, have fun building!

Resources

Happy coding, Gophers!