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:
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:
For Yarn:
Install the react
package to handle UI rendering and the js-cookie
package to handle session cookies. Run the following commands:
For NPM:
For Yarn:
The next step is to set up your code file to run the SDK functions. To set up the code file, follow these steps:
clientId
with your OAuth app's client ID. You can find the ID in your project's Headless Settings menu.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.
Create the services your visitors will book. It's important to also define your bookings forms.
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.
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.
Note: Start and end dates are ISO formatted objects.
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.
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.
service.mainSlug.name
value instead of service ID:
You can use the following full code example as a starting point for developing your own site: