API Client Library
Use our official TypeScript client for a better developer experience.
Installation
npm install @repo/webkasa-api-client
# or
yarn add @repo/webkasa-api-client
# or
pnpm add @repo/webkasa-api-clientQuick Start
app.ts
import { createWebKasaClient } from '@repo/webkasa-api-client';
// Initialize the client
const client = createWebKasaClient({
baseUrl: 'https://api.webkasa.app',
apiKey: process.env.WEBKASA_API_KEY!,
});
// Now you can use the client
async function main() {
// Get upcoming events
const events = await client.events.getUpcoming(10);
console.log('Upcoming events:', events);
// Create a booking
const booking = await client.bookings.create({
schedule_id: 'sch_xxx',
start_time: '2024-02-15T10:00:00Z',
end_time: '2024-02-15T11:00:00Z',
attendee: {
name: 'John Doe',
email: 'john@example.com',
},
});
console.log('Created booking:', booking);
}Events API
// List events with filters
const events = await client.events.list({
status: 'published',
upcoming: true,
limit: 20,
});
// Get upcoming events (shorthand)
const upcoming = await client.events.getUpcoming(10);
// Get a single event
const event = await client.events.get('evt_abc123');
// Create an event
const newEvent = await client.events.create({
title: 'Monthly Meditation',
start_date: '2024-02-15',
start_time: '19:00',
end_time: '21:00',
timezone: 'America/Los_Angeles',
location: 'Community Center',
slots: 30,
status: 'published',
});
// Update an event
const updated = await client.events.update('evt_abc123', {
slots: 50,
});
// Delete an event
await client.events.delete('evt_abc123');Bookings API
// List bookings with filters
const bookings = await client.bookings.list({
status: 'CONFIRMED',
upcoming: true,
});
// Get upcoming bookings (shorthand)
const upcoming = await client.bookings.getUpcoming(10);
// Get a single booking
const booking = await client.bookings.get('bk_abc123');
// Create a booking
const newBooking = await client.bookings.create({
schedule_id: 'sch_xxx',
event_type_id: 'evt_xxx',
start_time: '2024-02-15T10:00:00Z',
end_time: '2024-02-15T11:00:00Z',
attendee: {
name: 'John Doe',
email: 'john@example.com',
phone: '+1234567890',
},
});
// Confirm a booking
await client.bookings.confirm('bk_abc123');
// Complete a booking
await client.bookings.complete('bk_abc123');
// Cancel a booking
await client.bookings.cancel('bk_abc123');Posts API
// List posts with filters
const posts = await client.posts.list({
status: 'published',
category: 'wellness',
featured: true,
});
// Get published posts (shorthand)
const published = await client.posts.getPublished({ limit: 10 });
// Get a single post
const post = await client.posts.get('post_abc123');
// Create a post
const newPost = await client.posts.create({
title: 'Getting Started with Meditation',
slug: 'getting-started-meditation',
excerpt: 'A beginner\'s guide to meditation',
status: 'draft',
});
// Update a post
const updated = await client.posts.update('post_abc123', {
status: 'published',
});Error Handling
The client throws typed errors that you can catch and handle:
import { WebKasaError, RateLimitError, ValidationError } from '@repo/webkasa-api-client';
try {
await client.bookings.create({
schedule_id: 'sch_xxx',
start_time: '2024-02-15T10:00:00Z',
end_time: '2024-02-15T11:00:00Z',
attendee: {
name: 'John Doe',
email: 'john@example.com',
},
});
} catch (error) {
if (error instanceof RateLimitError) {
// Wait and retry
console.log(`Rate limited. Retry after ${error.retryAfter}s`);
} else if (error instanceof ValidationError) {
// Show validation errors to user
console.log('Validation errors:', error.errors);
} else if (error instanceof WebKasaError) {
// Handle other API errors
console.log(`API error: ${error.message}`);
} else {
// Handle network errors
throw error;
}
}TypeScript Support
The client is written in TypeScript and provides full type definitions:
import type {
Event,
Booking,
Post,
CreateBookingInput,
BookingStatus,
} from '@repo/webkasa-api-client';
// All responses are fully typed
const events: Event[] = await client.events.list();
// Input types help catch errors at compile time
const input: CreateBookingInput = {
schedule_id: 'sch_xxx',
start_time: '2024-02-15T10:00:00Z',
end_time: '2024-02-15T11:00:00Z',
attendee: {
name: 'John Doe',
email: 'john@example.com',
},
};
const booking: Booking = await client.bookings.create(input);Configuration Options
const client = createWebKasaClient({
// Required
baseUrl: 'https://api.webkasa.app',
apiKey: process.env.WEBKASA_API_KEY!,
// Optional
timeout: 30000, // Request timeout in ms (default: 30000)
retries: 3, // Number of retries for failed requests (default: 3)
// Custom fetch implementation (for testing or custom behavior)
fetch: customFetch,
});