Back

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

Aug 16, 20245 minute read

Introduction

Hey there, fellow developer! Ready to supercharge your scheduling game? Let's dive into the world of Cal.com API integration using the nifty @calcom/sdk package. Trust me, it's easier than you might think!

Prerequisites

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

  • Node.js and npm (you're a pro, so I'm sure you've got this covered)
  • A Cal.com account and API key (if you don't have one, go grab it real quick)

Setting up the project

Alright, let's get our hands dirty:

mkdir calcom-integration && cd calcom-integration npm init -y npm install @calcom/sdk

Easy peasy, right?

Configuring the SDK

Now, let's get that SDK up and running:

import Cal from "@calcom/sdk"; const cal = new Cal({ apiKey: "your-api-key-here" });

Boom! You're authenticated and ready to roll.

Basic API operations

Let's start with some basics:

// Fetch user info const user = await cal.users.me(); // List event types const eventTypes = await cal.eventTypes.list();

See? It's like talking to an old friend.

Creating and managing bookings

Time to play with bookings:

// Create a booking const newBooking = await cal.bookings.create({ eventTypeId: 123, start: "2023-06-01T09:00:00Z", end: "2023-06-01T10:00:00Z", name: "John Doe", email: "[email protected]" }); // Update a booking await cal.bookings.update(newBooking.id, { name: "Jane Doe" }); // Cancel a booking await cal.bookings.cancel(newBooking.id);

You're basically a booking wizard now!

Working with availability

Let's peek at those schedules:

// Fetch availability const availability = await cal.availability.list(); // Update availability await cal.availability.update({ // Your availability update object here });

Handling webhooks

Webhooks are your friends. Treat them well:

// Set up a simple webhook endpoint app.post('/webhook', (req, res) => { const event = req.body; // Handle the event console.log('Received event:', event); res.sendStatus(200); });

Error handling and best practices

Remember, even the best of us hit snags sometimes. Wrap your API calls in try-catch blocks and keep an eye on those rate limits. The Cal.com API is pretty chill, but it's always good to play nice.

Advanced use cases

Once you've got the basics down, sky's the limit! Integrate with your favorite CRM, automate follow-up emails, or build a fancy dashboard. The world is your oyster!

Conclusion

And there you have it! You're now armed and dangerous with Cal.com API knowledge. Go forth and schedule like a boss! If you want to dive deeper, check out the Cal.com API docs. Happy coding!