Skip to main content
Skip to main content

Examples of the Promotion Module

In this document, you’ll find common examples of how you can use the Promotion Module in your application.

Create a Promotion

import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { IPromotionModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"

export async function POST(
request: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const promotionModuleService: IPromotionModuleService =
request.scope.resolve(ModuleRegistrationName.PROMOTION)

const promotion = await promotionModuleService.create({
code: "10%OFF",
type: "standard",
application_method: {
type: "percentage",
target_type: "order",
value: "10",
},
})

res.json({ promotion })
}

Create a Campaign

import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { IPromotionModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"

export async function POST(
request: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const promotionModuleService: IPromotionModuleService =
request.scope.resolve(ModuleRegistrationName.PROMOTION)

const campaign = await promotionModuleService.createCampaigns(
{
name: "Summer Discounts",
campaign_identifier: "G-123445",
starts_at: new Date("2024-05-02"),
ends_at: new Date("2024-07-20"),
promotions: [
{
id: "promo_123",
},
],
}
)

res.json({ campaign })
}

Create a Promotion with Flexible Rules

import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { IPromotionModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"

export async function POST(
request: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const promotionModuleService: IPromotionModuleService =
request.scope.resolve(ModuleRegistrationName.PROMOTION)

const promotion = await promotionModuleService.create({
code: "10%OFF",
type: "standard",
application_method: {
type: "percentage",
target_type: "order",
value: "10",
},
rules: [
{
attribute: "customer_group_id",
operator: "eq",
values: ["VIP"],
},
],
})

res.json({ promotion })
}

List Promotions

import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { IPromotionModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"

export async function GET(
request: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const promotionModuleService: IPromotionModuleService =
request.scope.resolve(ModuleRegistrationName.PROMOTION)

res.json({
promotions: await promotionModuleService.list(),
})
}

More Examples

The module interface reference provides a reference to all the methods available for use with examples for each.

Was this section helpful?