Home

How to create a Booking

In this guide, you will learn how to create a booking step-by-step using the kleesto Distribution API. The process involves fetching all the available Products, retrieving Availabilities for a specific Product, getting Pricing Options for a selected date and time, and finally creating the Booking.

The following coding examples are written in JavaScript and use Axios for sending HTTP calls. If you have already set up your environment, jump directly to Step 3.

Step 1: Install axios packageCopied!

npm install axios

Step 2: Set up the global variables for your API credentialsCopied!

Never store the API credentials (X-Api-Key and X-Secret-Key) directly in your code. Instead, store them safely using environment variables or a secure secrets management tool to prevent unauthorized access.

const axios = require('axios');

// Global variables for API keys
const BASE_URL = 'https://distribution-api.kleesto.com';
const API_KEY = 'your-api-key';  // Replace with your actual api key
const SECRET_KEY = 'your-secret-key';  // Replace with your actual secret key

// Authorization headers
const authHeaders = {
  'X-Api-Key': API_KEY,
  'X-Secret-Key': SECRET_KEY
};

Step 3: Fetch All Available ProductsCopied!

The first step is to fetch all the available Products. Products include tours, activities, or other services that can be booked. For more details about the Product structure and other filtering options, please refer to List all Products.

Here is a sample code to fetch the Products:

const fetchProducts = async () => {
	// Optional filters for products
	const filters = {}

	// Pagination settings
	const pagination = {
		page: 0,
		size: 10,
	}

	// Send GET request to fetch products
	const response = await axios({
		url: `${BASE_URL}/products`,
		method: 'get',
		headers: authHeaders, // Authorization headers
		params: { filters, pagination }, // Query parameters
	})

	// Extract products from response
	const { data } = response
	const { products } = data

	// Return products for the next step
	return products
}

Step 4: Fetch Availabilities for a Specific ProductCopied!

After selecting a Product, the next step is to retrieve its Availabilities for a specific date range. Availabilities are the available time slots for a Product to book. For more details about the Availability structure and other filtering options, please refer to List all bookable Availabilities.

Here is a sample code to fetch the Product's Availabilities:

const fetchAvailabilities = async ({ productId, from, until, travelers }) => {
	// Set filters for the availability search
	const filters = {
		from,
		until,
		travelers,
	}

	// Send GET request to fetch availabilities
	const response = await axios({
		url: `${BASE_URL}/products/${productId}/availabilities`,
		method: 'get',
		headers: authHeaders, // Authorization headers
		params: { filters },  // Query parameters
	})

	// Extract availabilities from response
	const { data } = response
	const { availabilities } = data

	// Return availabilities for the next step
	return availabilities
}

Step 5: Fetch Pricing Options for a Specific Date and TimeCopied!

Once you have selected an Availability, the next step is to retrieve the Pricing Options based on the selected date, time, and the number of travelers. For more details about the Pricing Option structure and filtering options, please refer to List all bookable Pricing Options.

Here is a sample code to fetch the Product's Pricing Options:

const fetchPricingOptions = async ({ productId, startDate, startTime, travelers }) => {
	// Set filters for pricing options
	const filters = {
		startDate,
		startTime,
		travelers,
	}

	// Send GET request to fetch pricing options
	const response = await axios({
		url: `${BASE_URL}/products/${productId}/pricingoptions`,
		method: 'get',
		headers: authHeaders, // Authorization headers
		params: { filters },  // Query parameters
	})

	// Extract pricing options from response
	const { data } = response
	const { pricingOptions } = data

	// Return pricing options for the next step
	return pricingOptions
}

Step 6: Create a BookingCopied!

Once you have all the necessary information in place (Product, Availability and Pricing Option), you can now create a Booking. When making a booking request, you also need to specify the Contact Details, the Price Items and (optionally) the Additional Service Items purchased by the Customer during the checkout.

To make sure your Booking request will succeed, please refer to Booking Validation and check all the validation rules. For more details about the Booking structure and how to access a Booking after it's creation, please refer to Create a Booking and Retrieve a Booking.

Here is a sample code to create your Booking:

const createBooking = async ({
	productId,
	startDate,
        startTime,
        travelers,
	pricingOptionId,
	priceItems,
	additionalServiceItems,
	contactDetails,
}) => {
	// Prepare booking data
	const bookingData = {
		productId,
		startDate,
                startTime,
                travelers,
		pricingOptionId,
		priceItems,
		additionalServiceItems,
		contactDetails,
	}

	// Send POST request to create booking
	const response = await axios({
		url: `${BASE_URL}/bookings`,
		method: 'post',
		headers: authHeaders, // Authorization headers
		data: bookingData, // Request body
	})

	// Extract booking from response
	const { data } = response
	const { booking } = data

	return booking
}

ConclusionCopied!

By following the previous steps, you will be able to:

  1. Get all the available Products that customers can book

  2. Get all the available time slots (Availabilities) for a specific Product

  3. Get all the Pricing Options for a specific date and time

  4. Create your Booking with the appropriate product, availability and price information