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:
- Set up the Wix Headless environment.
- Import the SDK modules and create an SDK client.
- Create your services.
- Fetch your services and available slots.
- Implement checkout.
- 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:
-
If you haven't already, create a project.
When prompted to add functionalities to your new project, select Bookings. -
Set up authorization for your site by creating and configuring an OAuth app.
-
Set a domain that Wix can redirect to after completing a Wix-managed process.
-
Install the API client and relevant SDK module packages by running the following commands:
For NPM:Copy1npm install @wix/sdk2npm install @wix/bookings3npm install @wix/redirectsFor Yarn:
Copy1yarn add @wix/sdk2yarn add @wix/bookings3yarn add @wix/redirects -
Install the
react
package to handle UI rendering and thejs-cookie
package to handle session cookies. Run the following commands:
For NPM:Copy1npm install react2npm install js-cookieFor Yarn:
Copy1yarn add react2yarn 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:
- Add the following import statements to the top of your code file:
Copy1import Cookies from 'js-cookie';2import { useEffect, useState } from 'react';3import { createClient, OAuthStrategy } from '@wix/sdk';4import { availabilityCalendar, services } from '@wix/bookings';5import { redirects } from '@wix/redirects';
- 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 fortokens
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.Copy1const myWixClient = createClient({2modules: { services, availabilityCalendar, redirects },3auth: OAuthStrategy({4clientId: `<YOUR-CLIENT-ID>`,5tokens: 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.
- Fetch your Wix Bookings services.
Use theuseEffect
hook to make sure this code runs after the component is rendered. This ensure that your bookings data is available when the component mounts.
1async function fetchServices() {2 const serviceList = await myWixClient.services.queryServices().find();3 setServiceList(serviceList.items);4}5
6useEffect(() => {7 fetchServices();8}, []);
- Fetch the service's related slots:
1async 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.
1async 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.
- 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:
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>
- Display the slots on a calendar, given a date range, for the selected service:
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:
1import { useEffect, useState } from 'react';2
3import { createClient, OAuthStrategy } from '@wix/sdk';4import { availabilityCalendar, services } from '@wix/bookings';5import { redirects } from '@wix/redirects';6
7const myWixClient = createClient({8 modules: { services, availabilityCalendar, redirects },9 auth: OAuthStrategy({ clientId: `10c1663b-2cdf-47c5-a3ef-30c2e8543849` })10});11
12export 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 <div59 key={entry.slot.startDate}60 onClick={() => createRedirect(entry)}61 >62 {entry.slot.startDate}63 </div>64 );65 })}66 </div>67 </>68 );69}