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:
- Set up the Wix Headless environment.
- Import the SDK modules and create an SDK client.
- Create a React component and state variables.
- Fetch events and ticket availability.
- Implement the checkout flow.
- Add the
useEffect
hook. - Render the UI.
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:Copy Codenpm install @wix/sdknpm install @wix/eventsFor Yarn:
Copy Codeyarn add @wix/sdkyarn add @wix/events -
Install the
react
package to handle UI rendering and thejs-cookie
package to handle session cookies. Run the following commands:
For NPM:Copy Codenpm install reactnpm install js-cookieFor Yarn:
Copy Codeyarn add reactyarn add js-cookie
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:
Copy Codeimport Cookies from 'js-cookie';import { useEffect, useState } from 'react';import { createClient, OAuthStrategy } from '@wix/sdk';import { wixEvents, checkout } from '@wix/events';import { 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.Copy Codeconst myWixClient = createClient({modules: { wixEvents, checkout, redirects },auth: OAuthStrategy({clientId: `<YOUR-CLIENT-ID>`,tokens: JSON.parse(Cookies.get('session') || null),}),});
The logic for our ticket reservation flow is contained in a React component called Tickets
. To create the component, follow these steps:
- Add the following function component to your code file:
Copy Codeexport default function Tickets() {}
- Define state variables by adding the following code to the
Tickets
component.
TheeventsList
variable stores the list of events from your project's Wix Events. TheticketsAvailability
variable stores available ticket information.Copy Codeconst [eventsList, setEventsList] = useState([]);const [ticketsAvailability, setTicketsAvailability] = useState([]);
- Define a function called
fetchEvents
that gets the list of events that you would like to display in your component from your project's backend. This function uses thequeryEventsV2()
function to fetch your project's events. The function runs when when the component is first rendered.Copy Codeasync function fetchEvents() {const eventsList = await myWixClient.wixEvents.queryEventsV2({query: { paging: { limit: 10 } },});setEventsList(eventsList.events);} - Define a function called
fetchTicketsAvailability
that gets the list of available tickets when an event is selected. This function uses thequeryAvailableTickets()
function to find the available tickets for the selected event.Copy Codeasync function fetchTicketsAvailability(event) {const tickets = await myWixClient.checkout.queryAvailableTickets({filter: { eventId: event._id },limit: 10,});setTicketsAvailability(tickets.definitions);}
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 callingcreateRedirectSession()
.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.
Copy Codeasync function createRedirect(ticket) {const eventSlug = eventsList.find((event) => event._id === ticket.eventId).slug;const reservation = await myWixClient.checkout.createReservation(ticket.eventId,{ticketQuantities: [{ticketDefinitionId: ticket._id,quantity: 1,},],});const redirect = await myWixClient.redirects.createRedirectSession({eventsCheckout: { eventSlug, reservationId: reservation._id },callbacks: { postFlowUrl: window.location.href },});window.location = redirect.redirectSession.fullUrl;}
Add 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:
Copy Code
useEffect(() => {fetchEvents();}, []);
Add the following code to the Tickets
component's return statement to render the UI. The UI displays the following:
- A Choose Events section with a list of your events. Clicking an event checks for available tickets.
- A Choose Tickets section with a list of tickets for the event.
Copy Code
<div><h2>Choose Event:</h2>{eventsList.map((event) => {return <div key={event._id} onClick={() => fetchTicketsAvailability(event)}>{event.title}</div>;})}</div><div><h2>Choose Tickets:</h2>{ticketsAvailability.map((ticket) => {return <div key={ticket._id} onClick={() => createRedirect(ticket)}>{ticket.name}</div>;})}</div>
You can use the following full code example as a starting point for developing your own site:
Copy Code
import Cookies from 'js-cookie';import { useEffect, useState } from 'react';import { createClient, OAuthStrategy } from '@wix/sdk';import { wixEvents, checkout } from '@wix/events';import { redirects } from '@wix/redirects';const myWixClient = createClient({modules: { wixEvents, checkout, redirects },auth: OAuthStrategy({clientId: `10c1663b-2cdf-47c5-a3ef-30c2e8543849`,tokens: JSON.parse(Cookies.get('session') || null)})});export default function Tickets() {const [eventsList, setEventsList] = useState([]);const [ticketsAvailability, setTicketsAvailability] = useState([]);async function fetchEvents() {const eventsList = await myWixClient.wixEvents.queryEventsV2({ query: { paging: { limit: 10 } } });setEventsList(eventsList.events);}async function fetchTicketsAvailability(event) {const tickets = await myWixClient.checkout.queryAvailableTickets({filter: { eventId: event._id },limit: 10,});setTicketsAvailability(tickets.definitions);}async function createRedirect(ticket) {const eventSlug = eventsList.find((event) => event._id === ticket.eventId).slug;const reservation = await myWixClient.checkout.createReservation(ticket.eventId, {ticketQuantities: [{ticketDefinitionId: ticket._id,quantity: 1,}],});const redirect = await myWixClient.redirects.createRedirectSession({eventsCheckout: { eventSlug, reservationId: reservation._id },callbacks: { postFlowUrl: window.location.href }});window.location = redirect.redirectSession.fullUrl;}useEffect(() => { fetchEvents(); }, []);return (<div><h2>Choose Event:</h2>{eventsList.map((event) => {return <div key={event._id} onClick={() => fetchTicketsAvailability(event)}>{event.title}</div>;})}</div><div><h2>Choose Tickets:</h2>{ticketsAvailability.map((ticket) => {return <div key={ticket._id} onClick={() => createRedirect(ticket)}>{ticket.name}</div>;})}</div>)}