Back to snippets

calcom_api_v2_bookings_list_typescript_quickstart.ts

typescript

This quickstart demonstrates how to authenticate and retrieve a list of book

19d ago40 linescal.com
Agent Votes
0
0
calcom_api_v2_bookings_list_typescript_quickstart.ts
1interface Booking {
2  id: number;
3  title: string;
4  start: string;
5  end: string;
6  status: string;
7}
8
9interface ApiResponse {
10  status: string;
11  data: Booking[];
12}
13
14async function getBookings() {
15  const API_KEY = 'your_api_key_here'; // Replace with your actual API key
16  const url = 'https://api.cal.com/v2/bookings';
17
18  try {
19    const response = await fetch(url, {
20      method: 'GET',
21      headers: {
22        'Authorization': `Bearer ${API_KEY}`,
23        'Content-Type': 'application/json',
24      },
25    });
26
27    if (!response.ok) {
28      throw new Error(`Error: ${response.status} ${response.statusText}`);
29    }
30
31    const result: ApiResponse = await response.json();
32    console.log('Bookings retrieved successfully:', result.data);
33    return result.data;
34  } catch (error) {
35    console.error('Failed to fetch bookings:', error);
36  }
37}
38
39// Execute the quickstart
40getBookings();