My App
RoomKitFrontend

useCreateBooking

Create a new booking with automatic idempotency

Overview

useCreateBooking is a mutation hook that creates a new booking. It calls POST {apiUrl}/bookings and automatically generates an idempotency key.

import { useCreateBooking } from '@roomkit/react';

Usage

function NewBooking({ roomId }: { roomId: string }) {
    const { mutate, data, isLoading, error } = useCreateBooking({
        onSuccess: (booking) => {
            console.log('Booking created:', booking.id);
        },
        onError: (err) => {
            console.error('Failed to create booking:', err.message);
        },
    });

    const handleSubmit = () => {
        mutate({
            roomId,
            requesterId: 'user-1',
            title: 'Team Meeting',
            startsAt: '2026-03-02T10:00:00Z',
            endsAt: '2026-03-02T11:00:00Z',
            purposeType: 'SEMINAR',
        });
    };

    return (
        <div>
            <button onClick={handleSubmit} disabled={isLoading}>
                {isLoading ? 'Creating...' : 'Book Room'}
            </button>
            {error && <p>Error: {error.message}</p>}
            {data && <p>Booked: {data.id}</p>}
        </div>
    );
}

CreateBookingInput

interface CreateBookingInput {
    roomId: string;
    requesterId: string;
    onBehalfOfId?: string | null;
    title: string;
    description?: string | null;
    startsAt: string;
    endsAt: string;
    priority?: number;
    purposeType: string;
    recurrenceRuleId?: string | null;
    recurrenceModType?: RecurrenceModType | null;
    metadata?: string | null;
}
FieldTypeRequiredDescription
roomIdstringYesTarget room ID
requesterIdstringYesID of the person requesting
titlestringYesBooking title
startsAtstringYesISO 8601 start time
endsAtstringYesISO 8601 end time
purposeTypestringYesPurpose (e.g., "LECTURE", "SEMINAR")
onBehalfOfIdstring | nullNoDelegate ID
descriptionstring | nullNoDescription
prioritynumberNoExplicit priority (overrides purpose-based resolution)
recurrenceRuleIdstring | nullNoLink to recurrence series
recurrenceModTypeRecurrenceModType | nullNoRecurrence modification type
metadatastring | nullNoArbitrary JSON string

UseCreateBookingOptions

interface UseCreateBookingOptions {
    onSuccess?: (data: Booking) => void;
    onError?: (error: Error) => void;
}

Return Value

FieldTypeDescription
mutate(input: CreateBookingInput) => Promise<Booking>Trigger the creation
dataBooking | nullThe created booking
isLoadingbooleantrue while the request is in flight
errorError | nullThe error if the request failed

API Endpoint

POST {apiUrl}/bookings

An idempotencyKey is automatically generated using crypto.randomUUID() to prevent duplicate bookings on network retries.

On this page