Bookings Quick Start

Share your feedback
Reach out to us with feedback and suggestions to improve the Wix Headless experience, and join the Headless channel of the Devs on Wix Discord community to discuss features and connect with our growing community of developers.

The SDK bookings module allows you to take advantage of Wix Bookings business services in a site or app you build on any platform. This means you can allow your clients to book and pay for services online. This tutorial shows you how to use the bookings module to create a flow where a visitor can select a service from your Wix Bookings service list, pick an available time slot, and check out.

The tutorial is based on the Wix Headless example site. You can test out the live example site, or fork the site's code repo to use as a starting point for your own site or app.

This implementation focuses on simplicity and understandability, rather than feature richness, performance, or completeness. For details about additional functionality, see Wix Bookings in the API Reference.

Looking for a more comprehensive example site integrating Wix Headless APIs for bookings management? Check out our starter templates.

Note: The code in this tutorial is written in JSX, but you can use the SDK in any JavaScript environment.

Implementing the booking flow includes the following steps:

  1. Set up the Wix Headless environment.
  2. Import the SDK modules and create an SDK client.
  3. Create your services.
  4. Fetch your services and available slots.
  5. Implement checkout.
  6. Display the UI.

Step 1: Set up the Wix Headless environment

Before using the SDK, there are a few things you need to set up on your Wix account and in your external site or app's coding environment.

To set up the Wix Headless environment, follow these steps:

  1. If you haven't already, create a project.
    When prompted to add functionalities to your new project, select Bookings.

  2. Set up authorization for your site by creating and configuring an OAuth app.

  3. Set a domain to be used by Wix-managed pages.

  4. Set a domain that Wix can redirect to after completing a Wix-managed process.

  5. Install the API client and relevant SDK module packages by running the following commands:
    For NPM:

    Copy
    1
    npm install @wix/sdk
    2
    npm install @wix/bookings
    3
    npm install @wix/redirects

    For Yarn:

    Copy
    1
    yarn add @wix/sdk
    2
    yarn add @wix/bookings
    3
    yarn add @wix/redirects
  6. Install the react package to handle UI rendering and the js-cookie package to handle session cookies. Run the following commands:
    For NPM:

    Copy
    1
    npm install react
    2
    npm install js-cookie

    For Yarn:

    Copy
    1
    yarn add react
    2
    yarn add js-cookie

Step 2: Import the SDK modules and create an SDK client

The next step is to set up your code file to run the SDK functions. To set up the code file, follow these steps:

  1. Add the following import statements to the top of your code file:
    Copy
    1
    import Cookies from 'js-cookie';
    2
    import { useEffect, useState } from 'react';
    3
    import { createClient, OAuthStrategy } from '@wix/sdk';
    4
    import { availabilityCalendar, services } from '@wix/bookings';
    5
    import { redirects } from '@wix/redirects';
  2. Create an SDK client by adding the following code to your code file. Replace the value for clientId with your OAuth app's client ID. You can find the ID in your project's Headless Settings menu.
    The value for tokens is the 'session' cookie on the site visitor's browser. It's used to make calls to the Wix API. This way, you can maintain previous visitor sessions. For information about managing session cookies, see Session Token Management.
    Copy
    1
    const myWixClient = createClient({
    2
    modules: { services, availabilityCalendar, redirects },
    3
    auth: OAuthStrategy({
    4
    clientId: `<YOUR-CLIENT-ID>`,
    5
    tokens: JSON.parse(Cookies.get('session') || '{"accessToken": {}, "refreshToken": {}}')
    6
    })

Step 3: Create your services

Create the services your visitors will book. It's important to also define your bookings forms.

Step 4: Fetch your services and available slots

Define functions to fetch your Wix Bookings services and availability. These functions use the queryServices() function to find the services and the queryAvailability() function to find the available slots for a given service and date.

  1. Fetch your Wix Bookings services.
    Use the useEffect hook to make sure this code runs after the component is rendered. This ensure that your bookings data is available when the component mounts.
Copy
1
async function fetchServices() {
2
const serviceList = await myWixClient.services.queryServices().find();
3
setServiceList(serviceList.items);
4
}
5
6
useEffect(() => {
7
fetchServices();
8
}, []);
  1. Fetch the service's related slots:
Copy
1
async function fetchAvailability(service) {
2
const today = new Date();
3
const tomorrow = new Date(today);
4
tomorrow.setDate(tomorrow.getDate() + 1);
5
6
const availability = await myWixClient.availabilityCalendar.queryAvailability(
7
{
8
filter: {
9
serviceId: [service._id],
10
startDate: today.toISOString(),
11
endDate: tomorrow.toISOString(),
12
},
13
},
14
{ timezone: 'UTC' }
15
);
16
setAvailabilityEntries(availability.availabilityEntries);
17
}

Note: Start and end dates are ISO formatted objects.

Step 5: Implement checkout

Once a slot is selected, the visitor can initiate Wix’s secure checkout and complete the booking process.

Define a function called createRedirect() that's called when the checkout button in your UI is clicked. The function uses the createRedirectSession() to create a checkout URL for a given bookings slot. The visitor is then redirected to the checkout URL to complete the checkout. A visitor can choose to log in to your site or app during the Wix checkout process.

Note: When redirecting from Wix to an external site, Wix validates that the provided URL is registered under an allowed domain for the given client ID. Therefore, you must add your domain to the OAuth app.

Copy
1
async function createRedirect(slotAvailability) {
2
const redirect = await myWixClient.redirects.createRedirectSession({
3
bookingsCheckout: { slotAvailability, timezone: 'UTC' },
4
callbacks: { postFlowUrl: window.location.href },
5
});
6
window.location = redirect.redirectSession.fullUrl;
7
}

Step 6: Display the UI

Create a dynamic services list page and connect the elements on your site or app to the relevant fields from your Wix Bookings services.

Use each services's slug or service ID as an identifier and present more information related to each service, such as its name.

  1. The example below uses ID for simplicity, but it's common practice to create friendly urls, using the service.mainSlug.name value instead of service ID:
Copy
1
<div>
2
<h2>Choose Service:</h2>
3
{serviceList.map((service) => {
4
return <div key={service._id} onClick={() => fetchAvailability(service)}>{service.name}</div>;
5
}
6
</div>
  1. Display the slots on a calendar, given a date range, for the selected service:
Copy
1
<div>
2
<h2>Choose Slot:</h2>
3
{availabilityEntries.map((entry) => {
4
return <div key={entry.slot.startDate} onClick={() => createRedirect(entry)}>{entry.slot.startDate}</div>;
5
}
6
</div>

Complete code example

You can use the following full code example as a starting point for developing your own site:

Copy
1
import { useEffect, useState } from 'react';
2
3
import { createClient, OAuthStrategy } from '@wix/sdk';
4
import { availabilityCalendar, services } from '@wix/bookings';
5
import { redirects } from '@wix/redirects';
6
7
const myWixClient = createClient({
8
modules: { services, availabilityCalendar, redirects },
9
auth: OAuthStrategy({ clientId: `10c1663b-2cdf-47c5-a3ef-30c2e8543849` })
10
});
11
12
export default function Booking() {
13
const [serviceList, setServiceList] = useState([]);
14
const [availabilityEntries, setAvailabilityEntries] = useState([]);
15
16
async function fetchServices() {
17
const serviceList = await myWixClient.services.queryServices().find();
18
setServiceList(serviceList.items);
19
}
20
21
async function fetchAvailability(service) {
22
const today = new Date();
23
const tomorrow = new Date(today);
24
tomorrow.setDate(tomorrow.getDate() + 1);
25
26
const availability = await myWixClient.availabilityCalendar.queryAvailability({
27
filter: { serviceId: [service._id], startDate: today.toISOString(), endDate: tomorrow.toISOString() }
28
}, { timezone: 'UTC' });
29
setAvailabilityEntries(availability.availabilityEntries);
30
}
31
32
async function createRedirect(slotAvailability) {
33
const redirect = await myWixClient.redirects.createRedirectSession({
34
bookingsCheckout: { slotAvailability, timezone: 'UTC' },
35
callbacks: { postFlowUrl: window.location.href }
36
});
37
window.location = redirect.redirectSession.fullUrl;
38
}
39
40
useEffect(() => { fetchServices(); }, []);
41
42
return (
43
<>
44
<div>
45
<h2>Choose Service:</h2>
46
{serviceList.map((service) => {
47
return (
48
<div key={service._id} onClick={() => fetchAvailability(service)}>
49
{service.name}
50
</div>
51
);
52
})}
53
</div>
54
<div>
55
<h2>Choose Slot:</h2>
56
{availabilityEntries.map((entry) => {
57
return (
58
<div
59
key={entry.slot.startDate}
60
onClick={() => createRedirect(entry)}
61
>
62
{entry.slot.startDate}
63
</div>
64
);
65
})}
66
</div>
67
</>
68
);
69
}
Was this helpful?
Yes
No