Examples of Pricing Module
In this document, you’ll find common examples of how you can use the Pricing module in your application.
Create a Price Set
import { NextResponse } from "next/server"
import {
initialize as initializePricingModule,
} from "@medusajs/pricing"
export async function POST(request: Request) {
const pricingService = await initializePricingModule()
const body = await request.json()
// A rule type with `rule_attribute=region_id` should
// already be present in the database
const priceSet = await pricingService.create([
{
rules: [{ rule_attribute: "region_id" }],
prices: [
{
currency_code: body.currency_code,
amount: body.amount,
rules: {
region_id: body.region_id,
},
},
],
},
])
return NextResponse.json({ price_set: priceSet })
}
import {
MedusaRequest,
MedusaResponse
} from "@medusajs/medusa";
import {
PricingModuleService
} from "@medusajs/pricing"
export async function POST(
request: MedusaRequest,
response: MedusaResponse
): Promise<void> {
const pricingModuleService: PricingModuleService =
request.scope.resolve(
"pricingModuleService"
)
// A rule type with `rule_attribute=region_id` should
// already be present in the database
const priceSet = await pricingModuleService.create([
{
rules: [{ rule_attribute: "region_id" }],
prices: [
{
currency_code: request.body.currency_code,
amount: request.body.amount,
rules: {
region_id: request.body.region_id,
},
},
],
},
])
response.json({ price_set: priceSet })
}
List Price Sets
import { NextResponse } from "next/server"
import {
initialize as initializePricingModule,
} from "@medusajs/pricing"
export async function GET(request: Request) {
const pricingService = await initializePricingModule()
const priceSets = await pricingService.list()
return NextResponse.json({ price_sets: priceSets })
}
import {
MedusaRequest,
MedusaResponse
} from "@medusajs/medusa";
import {
PricingModuleService
} from "@medusajs/pricing"
export async function GET(
request: MedusaRequest,
response: MedusaResponse
): Promise<void> {
const pricingModuleService: PricingModuleService =
request.scope.resolve(
"pricingModuleService"
)
const priceSets = await pricingModuleService.list()
response.json({ price_sets: priceSets })
}
Retrieve a Price Set by its ID
import { NextResponse } from "next/server"
import {
initialize as initializePricingModule,
} from "@medusajs/pricing"
type ContextType = {
params: {
id: string
}
}
export async function GET(
request: Request,
{ params }: ContextType
) {
const pricingService = await initializePricingModule()
const priceSet = await pricingService.retrieve(params.id)
return NextResponse.json({ price_set: priceSet })
}
import {
MedusaRequest,
MedusaResponse
} from "@medusajs/medusa";
import {
PricingModuleService
} from "@medusajs/pricing"
export async function GET(
request: MedusaRequest,
response: MedusaResponse
): Promise<void> {
const pricingModuleService: PricingModuleService =
request.scope.resolve(
"pricingModuleService"
)
const priceSet = await pricingModuleService.retrieve(request.params.id)
response.json({ price_set: priceSet })
}
Create a Rule Type
import { NextResponse } from "next/server"
import {
initialize as initializePricingModule,
} from "@medusajs/pricing"
type ContextType = {
params: {
id: string
}
}
export async function GET(
request: Request,
{ params }: ContextType
) {
const pricingService = await initializePricingModule()
const priceSet = await pricingService.retrieve(params.id)
return NextResponse.json({ price_set: priceSet })
}
import {
MedusaRequest,
MedusaResponse
} from "@medusajs/medusa";
import {
PricingModuleService
} from "@medusajs/pricing"
export async function POST(
request: MedusaRequest,
response: MedusaResponse
): Promise<void> {
const pricingModuleService: PricingModuleService =
request.scope.resolve(
"pricingModuleService"
)
const priceSet = await pricingModuleService.retrieve(request.params.id)
response.json({ price_set: priceSet })
}
Add Prices with Rules
import { NextResponse } from "next/server"
import {
initialize as initializePricingModule,
} from "@medusajs/pricing"
export async function POST(request: Request) {
const pricingService = await initializePricingModule()
const body = await request.json()
const priceSet = await pricingService.addPrices({
priceSetId: body.price_set_id,
prices: [
{
amount: 500,
currency_code: "USD",
rules: {
region_id: body.region_id,
},
},
],
})
return NextResponse.json({ price_set: priceSet })
}
import {
MedusaRequest,
MedusaResponse
} from "@medusajs/medusa";
import {
PricingModuleService
} from "@medusajs/pricing"
export async function POST(
request: MedusaRequest,
response: MedusaResponse
): Promise<void> {
const pricingModuleService: PricingModuleService =
request.scope.resolve(
"pricingModuleService"
)
const priceSet = await pricingModuleService.addPrices({
priceSetId: request.body.price_set_id,
prices: [
{
amount: 500,
currency_code: "USD",
rules: {
region_id: request.body.region_id,
},
},
],
})
response.json({ price_set: priceSet })
}
Create a Currency
import { NextResponse } from "next/server"
import {
initialize as initializePricingModule,
} from "@medusajs/pricing"
export async function POST(request: Request) {
const pricingService = await initializePricingModule()
const body = await request.json()
const currencies = await pricingService.createCurrencies([{
code: "EUR",
symbol: "€",
symbol_native: "€",
name: "Euro",
}])
return NextResponse.json({ currencies })
}
import {
MedusaRequest,
MedusaResponse
} from "@medusajs/medusa";
import {
PricingModuleService
} from "@medusajs/pricing"
export async function POST(
request: MedusaRequest,
response: MedusaResponse
): Promise<void> {
const pricingModuleService: PricingModuleService =
request.scope.resolve(
"pricingModuleService"
)
const currencies = await pricingModuleService.createCurrencies([{
code: "EUR",
symbol: "€",
symbol_native: "€",
name: "Euro",
}])
response.json({ currencies })
}
List Currencies
import { NextResponse } from "next/server"
import {
initialize as initializePricingModule,
} from "@medusajs/pricing"
export async function GET(request: Request) {
const pricingService = await initializePricingModule()
const currencies = await pricingService.listCurrencies()
return NextResponse.json({ currencies })
}
import {
MedusaRequest,
MedusaResponse
} from "@medusajs/medusa";
import {
PricingModuleService
} from "@medusajs/pricing"
export async function GET(
request: MedusaRequest,
response: MedusaResponse
): Promise<void> {
const pricingModuleService: PricingModuleService =
request.scope.resolve(
"pricingModuleService"
)
const currencies = await pricingModuleService.listCurrencies()
response.json({ currencies })
}
Create Price List
import { NextResponse } from "next/server"
import { PriceListType } from "@medusajs/medusa";
import {
initialize as initializePricingModule,
} from "@medusajs/pricing"
export async function POST(request: Request) {
const pricingService = await initializePricingModule()
const priceLists = await pricingService.createPriceLists([
{
title: "My Sale",
description: "This is my sale",
type: PriceListType.SALE,
starts_at: Date.parse("01/10/2023").toString(),
ends_at: Date.parse("31/10/2023").toString(),
rules: {
region_id: ["DE", "DK"],
},
prices: [
{
amount: 400,
currency_code: "EUR",
price_set_id: "pset_124",
},
],
}
])
return NextResponse.json({ price_lists: priceLists })
}
import {
MedusaRequest,
MedusaResponse,
PriceListType
} from "@medusajs/medusa";
import {
PricingModuleService
} from "@medusajs/pricing"
export async function POST(
request: MedusaRequest,
response: MedusaResponse
): Promise<void> {
const pricingModuleService: PricingModuleService =
request.scope.resolve(
"pricingModuleService"
)
const priceLists = await pricingModuleService.createPriceLists([
{
title: "My Sale",
description: "This is my sale",
type: PriceListType.SALE,
starts_at: Date.parse("01/10/2023").toString(),
ends_at: Date.parse("31/10/2023").toString(),
rules: {
region_id: ["DE", "DK"],
},
prices: [
{
amount: 400,
currency_code: "EUR",
price_set_id: "pset_124",
},
],
}
])
response.json({ price_lists: priceLists })
}
Calculate Prices For a Currency
import { NextResponse } from "next/server"
import {
initialize as initializePricingModule,
} from "@medusajs/pricing"
type ContextType = {
params: {
id: string
currency_code: string
}
}
export async function GET(
request: Request,
{ params }: ContextType
) {
const pricingService = await initializePricingModule()
const price = await pricingService.calculatePrices({
id: [params.id],
}, {
context: {
currency_code: params.currency_code,
},
})
return NextResponse.json({ price })
}
import {
MedusaRequest,
MedusaResponse,
PriceListType
} from "@medusajs/medusa";
import {
PricingModuleService
} from "@medusajs/pricing"
export async function POST(
request: MedusaRequest,
response: MedusaResponse
): Promise<void> {
const pricingModuleService: PricingModuleService =
request.scope.resolve(
"pricingModuleService"
)
const price = await pricingModuleService.calculatePrices({
id: [request.params.id],
}, {
context: {
currency_code: request.params.currency_code,
},
})
response.json({ price })
}
More Examples
The module interface reference provides a reference to all the methods available for use with examples for each.
Was this section helpful?