Build an AI agent that books meetings
Booking a meeting comes down to three steps: know when you’re free, pick a time, and write it down without clashing with anything else. In this tutorial your agent does all three with Chronary. Then it creates a booking link, so people can pick a time without a back-and-forth.
You will:
- Limit the calendar to working hours, with a gap between meetings.
- Ask Chronary for free times and book the first one.
- Handle the case where someone takes the slot first.
- Create a booking page that people can use to book themselves.
Everything here works on the Free plan.
Before you start
Section titled “Before you start”You need an agent with a calendar, the SDK installed and CHRONARY_API_KEY set. If you don’t have those yet, do Give your AI agent a calendar first. It takes about five minutes. Create the calendar with prevent_overlaps: true, so it can’t be double-booked.
The examples use these two IDs:
import { Chronary } from '@chronary/sdk';
const chronary = new Chronary();const agentId = 'agt_your_agent_id';const calendarId = 'cal_your_calendar_id';from chronary import Chronary
client = Chronary()agent_id = "agt_your_agent_id"calendar_id = "cal_your_calendar_id"Step 1: Set working hours
Section titled “Step 1: Set working hours”Without rules, any empty minute counts as free, including 3am on a Sunday. Set working hours and a 10-minute buffer before and after each meeting. Chronary then only offers times inside those hours, with space around existing events.
const nineToFive = { start: '09:00', end: '17:00' };
await chronary.calendars.setAvailabilityRules(calendarId, { timezone: 'America/New_York', buffer_before_minutes: 10, buffer_after_minutes: 10, working_hours: { mon: nineToFive, tue: nineToFive, wed: nineToFive, thu: nineToFive, fri: nineToFive, },});nine_to_five = {"start": "09:00", "end": "17:00"}
client.calendars.set_availability_rules( calendar_id, timezone="America/New_York", buffer_before_minutes=10, buffer_after_minutes=10, working_hours={ "mon": nine_to_five, "tue": nine_to_five, "wed": nine_to_five, "thu": nine_to_five, "fri": nine_to_five, },)You only do this once per calendar. Days you leave out (here Saturday and Sunday) count as busy.
Step 2: Find free times
Section titled “Step 2: Find free times”Say the agent got this request: “Find 30 minutes with me sometime this week.” Ask Chronary for the agent’s free time over the next seven days, in slots of at least 30 minutes:
const now = new Date();const weekAhead = new Date(now.getTime() + 7 * 24 * 60 * 60 * 1000);
const availability = await chronary.availability.forAgent(agentId, { start: now.toISOString(), end: weekAhead.toISOString(), duration: '30m',});
if (availability.availability_state !== 'complete') { throw new Error('Availability is incomplete; try again shortly');}console.log(availability.slots.slice(0, 3));// [{ start: '…T13:00:00Z', end: '…T16:20:00Z' }, …]from datetime import datetime, timedelta, timezone
now = datetime.now(timezone.utc)
availability = client.availability.get( agent_id, start=now.isoformat(), end=(now + timedelta(days=7)).isoformat(), duration="30m",)
if availability.availability_state != "complete": raise RuntimeError("Availability is incomplete; try again shortly")print(availability.slots[:3])Two things to know about the answer:
- Each slot is a free stretch of at least 30 minutes, not exactly 30. A slot can run for hours, so book from its
startfor the length you want. - Only book from a
completeanswer. If the agent’s person connected their Google or Outlook calendar and it hasn’t synced recently, Chronary returns no slots and a warning, so it never offers a time it can’t vouch for.
Step 3: Book the meeting
Section titled “Step 3: Book the meeting”Take the first slot, book 30 minutes from its start, and handle the case where someone else got there first. Because the calendar has prevent_overlaps on, a clashing write is refused with 409 and the code event_conflict. Nothing is written, so the agent can move on to the next slot.
import { ChronaryError } from '@chronary/sdk';
const MEETING_MS = 30 * 60 * 1000;
async function bookFirstFreeSlot(title: string) { for (const slot of availability.slots) { const start = new Date(slot.start); const end = new Date(start.getTime() + MEETING_MS); try { return await chronary.events.create(calendarId, { title, start_time: start.toISOString(), end_time: end.toISOString(), }); } catch (err) { if (err instanceof ChronaryError && err.code === 'event_conflict') { continue; // taken in the meantime: try the next slot } throw err; } } return null; // no free time this week}
const event = await bookFirstFreeSlot('Intro call with Sam');console.log(event ? `Booked ${event.startTime}` : 'No free time this week');from chronary import APIStatusError
MEETING = timedelta(minutes=30)
def book_first_free_slot(title: str): for slot in availability.slots: start = datetime.fromisoformat(slot.start.replace("Z", "+00:00")) try: return client.events.create( calendar_id, title=title, start_time=start.isoformat(), end_time=(start + MEETING).isoformat(), ) except APIStatusError as err: if err.code != "event_conflict": raise # taken in the meantime: try the next slot return None # no free time this week
event = book_first_free_slot("Intro call with Sam")print(f"Booked {event.start_time}" if event else "No free time this week")This is safe even when many agents book at once: Chronary lets exactly one write win each slot. You don’t need locks or a check-then-write loop of your own. The double-booking guide explains what’s guaranteed.
Step 4: Let people book themselves
Section titled “Step 4: Let people book themselves”Often the easiest move is to send a link. A booking page is a public page for one calendar. People see the open times, pick one, and the meeting lands on the agent’s calendar. It never offers a time that’s already taken on the calendar. A booking page has its own working hours, so set them here too.
const page = await chronary.bookingPages.create({ calendar_id: calendarId, title: 'Intro call', description: '30 minutes to see if we can help.', duration_minutes: 30, buffer_minutes: 10, min_notice_minutes: 120, window_days: 14, timezone: 'America/New_York', availability_constraints: { mon: nineToFive, tue: nineToFive, wed: nineToFive, thu: nineToFive, fri: nineToFive, },});
console.log(page.booking_url); // send this to peoplepage = client.booking_pages.create( calendar_id=calendar_id, title="Intro call", description="30 minutes to see if we can help.", duration_minutes=30, buffer_minutes=10, min_notice_minutes=120, window_days=14, timezone="America/New_York", availability_constraints={ "mon": nine_to_five, "tue": nine_to_five, "wed": nine_to_five, "thu": nine_to_five, "fri": nine_to_five, },)
print(page.booking_url) # send this to peopleThe page reuses the weekday hours from step 1. min_notice_minutes: 120 stops anyone booking less than two hours ahead, and window_days: 14 shows the next two weeks. The booking pages guide covers the other options.
Step 5: Hear about new bookings
Section titled “Step 5: Hear about new bookings”When someone books through the page, Chronary sends an event.created webhook with a booking_page_id, so your agent can react right away: send a confirmation, prepare notes, or add the call to its plan. The same webhook also fires for the events your agent creates itself.
What you built
Section titled “What you built”Your agent can now find free time, book it without clashes, and hand out a link so people can book themselves. Next: