The SDK events
module allows you to integrate Wix Events business services with your site or app. This means you can create and manage events, sell tickets, and retrieve information about guests. This tutorial shows you how to use the events
module to create ticket reservations.
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 Events in the API Reference. Looking for a more comprehensive example site integrating Wix Headless APIs for ticket reservations and event 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 ticket reservation flow includes the following steps:
useEffect
hook.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 Events.
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:
Add the following import statements to the top of your code file:
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.
The logic for our ticket reservation flow is contained in a React component called Tickets
. To create the component, follow these steps:
Tickets
component.eventsList
variable stores the list of events from your project's Wix Events. The ticketsAvailability
variable stores available ticket information.
fetchEvents
that gets the list of events that you would like to display in your component from your project's backend. This function uses the queryEventsV2()
function to fetch your project's events. The function runs when when the component is first rendered.
fetchTicketsAvailability
that gets the list of available tickets when an event is selected. This function uses the queryAvailableTickets()
function to find the available tickets for the selected event.
Add a function called createRedirect
to the Tickets
function component. This function does the following:
Uses the createReservation()
function to create a reservation for the selected event.
Uses the createRedirectSession()
with the retrieved reservation ID to retrieve a checkout URL. This is the URL for a Wix-managed checkout page that the visitor can use to complete the checkout process.
Redirects the browser to the checkout URL. If the checkout is successful, the visitor is redirected to a Wix thank you page. After the thank you page, or if the checkout fails, the visitor is redirected to the URL passed in the postFlowUrl
property when calling createRedirectSession()
.
Note: When redirecting from Wix to an external site, Wix validates that the provided URL is registered an allowed domain for the given client ID. Therefore, you must add your domain to the OAuth app.
useEffect
hookAdd the following code to the Tickets
component to run the fetchEvents()
function after the component is rendered. This ensures that the data is retrieved when the component mounts:
Add the following code to the Tickets
component's return statement to render the UI. The UI displays the following:
You can use the following full code example as a starting point for developing your own site: