www/apps/resources/app/recipes/ticket-booking/page.mdx
import { AcademicCapSolid, PuzzleSolid } from "@medusajs/icons";
export const metadata = {
title: Ticket Booking System Recipe,
};
This recipe provides the general steps to implement a ticket booking system in your Medusa application.
<Note>Follow the step-by-step Ticket Booking System Example to learn how to implement a ticket booking system in your Medusa application.
</Note>A ticket booking system allows customers to book tickets for events, such as shows or sports games. By using a ticket booking system, you can manage shows, venues, and seat availability, among other features.
Medusa's Framework facilitates building a ticket booking system on top of the existing commerce functionalities, such as products, pricing, inventory, carts, and orders. All of these are provided by Medusa's Commerce Modules.
To support ticket-booking features, you can customize the Medusa application by creating a Ticket Booking Module, linking its data models to Medusa's existing models, and building flows around the module.
Your custom features and functionalities are implemented inside modules. The module is integrated into the Medusa application without any implications on existing functionalities.
The module will hold your custom data models and the service implementing ticket-booking-related features.
<Card href="!docs!/learn/fundamentals/modules" title="How to Create a Module" text="Learn how to create a module." icon={AcademicCapSolid} />
A data model represents a table in the database. You can define in your module data models to store data related to your custom features, such as shows, venues, and seats.
For example, you can define:
Venue model for the venue where the event takes place.TicketProduct model for a show, and a TicketProductVariant model for the ticket variants (for example, different seating sections).TicketPurchase model for a ticket purchase.Then, you can link your custom data model to data models from other modules. For example, you can link:
TicketProduct model to the Product Module's Product data model, benefiting from existing features related to products and collections.TicketProductVariant model to the Product Module's ProductVariant data model, benefiting from existing features related to inventory and pricing.TicketPurchase model to the Order Module's Order data model, benefiting from existing features related to orders and payments.<CardList itemsPerRow={2} items={[ { href: "!docs!/learn/fundamentals/modules#1-create-data-model", title: "How to Create a Data Model", text: "Learn how to create a data model.", icon: AcademicCapSolid, }, { href: "!docs!/learn/fundamentals/module-links", title: "Define Module Links", text: "Define links between data models.", icon: AcademicCapSolid, }, ]} />
Your module’s main service holds data-management and other related features. Then, in other resources, such as an API route, you can resolve the service from the Medusa container and use its functionalities.
Medusa facilitates implementing data-management features using the service factory. Your module's main service can extend this service factory, and it generates data-management methods for your data models.
<Card href="!docs!/learn/fundamentals/modules/service-factory" title="Service Factory" text="Learn about the service factory and how to use it." icon={AcademicCapSolid} />
You can implement ticket-booking-related business logic in workflows. A workflow is a series of queries and actions, called steps, that complete a task.
By using workflows, you benefit from features like rollback mechanisms, error handling, and retrying failed steps. You can then execute workflows from other resources, such as an API route.
You can implement workflows that create venues and ticket products, complete carts with ticket products, and more. In the workflow's steps, you can resolve the Ticket Booking Module's service and use its data-management methods to manage ticket-booking-related data.
You can then utilize the API routes that execute these workflows in your client applications, such as your Medusa Admin customizations or your storefront.
<CardList itemsPerRow={2} items={[ { href: "!docs!/learn/fundamentals/workflows", title: "Workflows", text: "Learn how to create a workflow.", icon: AcademicCapSolid, }, { href: "!docs!/learn/fundamentals/api-routes", title: "API Routes", text: "Learn how to create API routes.", icon: AcademicCapSolid, }, ]} />
By linking the TicketProductVariant model to the Product Module's ProductVariant model, you can leverage Medusa's inventory management features, implemented in the Inventory Module, to manage ticket product variants' inventory.
You can set up inventory items for ticket product variants based on show dates and seating sections. You can set the available quantity for each inventory item to reflect the number of seats available in that section for that show date.
This setup ensures that customers can only book tickets for available seats, preventing overbooking.
<CardList itemsPerRow={2} items={[ { href: "/commerce-modules/inventory", title: "Inventory Module", text: "Learn about the Inventory Module and its features.", icon: AcademicCapSolid, }, { href: "/commerce-modules/product/variant-inventory", title: "Product Variant Inventory", text: "Learn how to manage product variant inventory.", icon: AcademicCapSolid, }, ]} />
By default, Medusa product variants require shipping, which prompts the customer to provide a shipping address and choose a shipping method during checkout.
You can disable shipping on ticket product variants by setting the requires_shipping property of the product variant's inventory item to false.
Then, you can remove shipping-related steps from the checkout flow in the storefront.
<Card href="/commerce-modules/product/selling-products#configure-shipping-requirements" title="Configure Shipping Requirements" text="Learn how to configure shipping requirements for product variants." icon={AcademicCapSolid} />
You can extend the Medusa Admin to provide merchants with an interface to manage ticket-booking-related data, such as shows and venues. You can inject widgets into existing pages or create new pages.
In your customizations, you send requests to the API routes you created that execute the workflows implementing ticket-booking-related features.
<CardList items={[ { href: "!docs!/learn/fundamentals/admin/widgets", title: "Create a Widget", text: "Learn how to create a widget in the Medusa Admin.", icon: AcademicCapSolid, }, { href: "!docs!/learn/fundamentals/admin/ui-routes", title: "Create UI Route", text: "Learn how to create a UI route in the Medusa Admin.", icon: AcademicCapSolid, }, ]} />
Medusa's workflows provide hooks that allow you to inject custom logic at specific points in the workflow execution.
Specifically, workflows like addToCartWorkflow and completeCartWorkflow provide a validate hook that you can consume to add custom validation logic.
For example, you can consume the validate hook in the addToCartWorkflow to check if the selected seat is available before adding a ticket product variant to the cart.
<Card href="!docs!/learn/fundamentals/workflows/workflow-hooks" title="Workflow Hooks" text="Learn how to use workflow hooks." icon={AcademicCapSolid} />
When an order is placed, Medusa emits an order.placed event. You can listen to this event in a subscriber, which is an asynchronous function executed in the background when its associated event is emitted.
In the subscriber, you can send an email using the Notification Module. You can register a Notification Module Provider, such as SendGrid or Resend, that sends the email.
You can include a QR code representing the ticket in the email, which you can generate using a library like qrcode.
You can also implement an API route that verifies the QR code at the event entrance.
<CardList itemsPerRow={2} items={[ { href: "!docs!/learn/fundamentals/events-and-subscribers", title: "Events and Subscribers", text: "Learn how to create a subscriber.", icon: AcademicCapSolid, }, { href: "/infrastructure-modules/notification", title: "Notification Module", text: "Learn about available Notification Module Providers.", icon: AcademicCapSolid, }, ]} />
Customers can book tickets through your storefront. They need features like listing shows, selecting show dates and seats, and placing orders through a checkout flow that supports ticket products.
Medusa provides a Next.js Starter Storefront with standard commerce features including listing products, placing orders, and managing accounts. You can customize the storefront and tailor its functionalities to support ticket booking features.
Alternatively, you can build the storefront with your preferred tech stack.
<CardList items={[ { href: "/nextjs-starter", title: "Next.js Starter Storefront", text: "Learn how to install and use the Next.js Starter Storefront.", icon: AcademicCapSolid, }, { href: "/storefront-development", title: "Storefront Guides", text: "Learn how to build a storefront for your Medusa application.", icon: AcademicCapSolid, }, ]} />