Events Quick Start

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

    For Yarn:

    Copy
  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

    For Yarn:

    Copy

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

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

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

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

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

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

Complete code example

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

Copy
Was this helpful?
Yes
No