Events 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 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:

  1. Set up the Wix Headless environment.
  2. Import the SDK modules and create an SDK client.
  3. Create a React component and state variables.
  4. Fetch events and ticket availability.
  5. Implement the checkout flow.
  6. Add the useEffect hook.
  7. Render 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 Events.

  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/events

    For Yarn:

    Copy
    1
    yarn add @wix/sdk
    2
    yarn add @wix/events
  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
    4
    import { createClient, OAuthStrategy } from '@wix/sdk';
    5
    import { wixEvents, checkout } from '@wix/events';
    6
    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: { wixEvents, checkout, redirects },
    3
    auth: OAuthStrategy({
    4
    clientId: `<YOUR-CLIENT-ID>`,
    5
    tokens: JSON.parse(Cookies.get('session') || '{"accessToken": {}, "refreshToken": {}}'),
    6
    }),
    7
    });

Step 3: Create a React component and state variables

The logic for our ticket reservation flow is contained in a React component called Tickets. To create the component, follow these steps:

  1. Add the following function component to your code file:
    Copy
    1
    export default function Tickets() {}
  2. Define state variables by adding the following code to the Tickets component.
    The eventsList variable stores the list of events from your project's Wix Events. The ticketsAvailability variable stores available ticket information.
    Copy
    1
    const [eventsList, setEventsList] = useState([]);
    2
    const [ticketsAvailability, setTicketsAvailability] = useState([]);

Step 4: Fetch events and ticket availability

  1. 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 the queryEventsV2() function to fetch your project's events. The function runs when when the component is first rendered.
    Copy
    1
    async function fetchEvents() {
    2
    const eventsList = await myWixClient.wixEvents.queryEventsV2({
    3
    query: { paging: { limit: 10 } },
    4
    });
    5
    setEventsList(eventsList.events);
    6
    }
  2. Define a function called 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.
    Copy
    1
    async function fetchTicketsAvailability(event) {
    2
    const tickets = await myWixClient.checkout.queryAvailableTickets({
    3
    filter: { eventId: event._id },
    4
    limit: 10,
    5
    });
    6
    setTicketsAvailability(tickets.definitions);
    7
    }

Step 5: Implement the checkout flow

Add a function called createRedirect to the Tickets function component. This function does the following:

  1. Uses the createReservation() function to create a reservation for the selected event.

  2. 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.

  3. 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.

    Copy
    1
    async function createRedirect(ticket) {
    2
    const eventSlug = eventsList.find(
    3
    (event) => event._id === ticket.eventId
    4
    ).slug;
    5
    const reservation = await myWixClient.checkout.createReservation(
    6
    ticket.eventId,
    7
    {
    8
    ticketQuantities: [
    9
    {
    10
    ticketDefinitionId: ticket._id,
    11
    quantity: 1,
    12
    },
    13
    ],
    14
    }
    15
    );
    16
    const redirect = await myWixClient.redirects.createRedirectSession({
    17
    eventsCheckout: { eventSlug, reservationId: reservation._id },
    18
    callbacks: { postFlowUrl: window.location.href },
    19
    });
    20
    window.location = redirect.redirectSession.fullUrl;
    21
    }

Step 6: Add the useEffect hook

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
1
useEffect(() => {
2
fetchEvents();
3
}, []);

Step 7: Render the UI

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
1
<div>
2
<h2>Choose Event:</h2>
3
{eventsList.map((event) => {
4
return <div key={event._id} onClick={() => fetchTicketsAvailability(event)}>{event.title}</div>;
5
})}
6
</div>
7
<div>
8
<h2>Choose Tickets:</h2>
9
{ticketsAvailability.map((ticket) => {
10
return <div key={ticket._id} onClick={() => createRedirect(ticket)}>{ticket.name}</div>;
11
})}
12
</div>

Complete code example

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

Copy
1
import Cookies from 'js-cookie';
2
import { useEffect, useState } from 'react';
3
import { createClient, OAuthStrategy } from '@wix/sdk';
4
import { wixEvents, checkout } from '@wix/events';
5
import { redirects } from '@wix/redirects';
6
7
const myWixClient = createClient({
8
modules: { wixEvents, checkout, redirects },
9
auth: OAuthStrategy({
10
clientId: `10c1663b-2cdf-47c5-a3ef-30c2e8543849`,
11
tokens: JSON.parse(Cookies.get('session') || null)
12
})
13
});
14
15
export default function Tickets() {
16
const [eventsList, setEventsList] = useState([]);
17
const [ticketsAvailability, setTicketsAvailability] = useState([]);
18
19
async function fetchEvents() {
20
const eventsList = await myWixClient.wixEvents.queryEventsV2({ query: { paging: { limit: 10 } } });
21
setEventsList(eventsList.events);
22
}
23
24
async function fetchTicketsAvailability(event) {
25
const tickets = await myWixClient.checkout.queryAvailableTickets({
26
filter: { eventId: event._id },
27
limit: 10,
28
});
29
setTicketsAvailability(tickets.definitions);
30
}
31
32
async function createRedirect(ticket) {
33
const eventSlug = eventsList.find((event) => event._id === ticket.eventId).slug;
34
const reservation = await myWixClient.checkout.createReservation(ticket.eventId, {
35
ticketQuantities: [{
36
ticketDefinitionId: ticket._id,
37
quantity: 1,
38
}],
39
});
40
const redirect = await myWixClient.redirects.createRedirectSession({
41
eventsCheckout: { eventSlug, reservationId: reservation._id },
42
callbacks: { postFlowUrl: window.location.href }
43
});
44
window.location = redirect.redirectSession.fullUrl;
45
}
46
47
useEffect(() => { fetchEvents(); }, []);
48
49
return (
50
<div>
51
<h2>Choose Event:</h2>
52
{eventsList.map((event) => {
53
return <div key={event._id} onClick={() => fetchTicketsAvailability(event)}>{event.title}</div>;
54
})}
55
</div>
56
<div>
57
<h2>Choose Tickets:</h2>
58
{ticketsAvailability.map((ticket) => {
59
return <div key={ticket._id} onClick={() => createRedirect(ticket)}>{ticket.name}</div>;
60
})}
61
</div>
62
)
63
}
Was this helpful?
Yes
No