import { CodeTabs, CodeTab } from "docs-ui"

export const metadata = {
  title: `Examples of the Pricing Module`,
}

# {metadata.title}

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

## Create a Price Set

<CodeTabs groupId="app-type">
  <CodeTab value="medusa" label="Medusa API Router">

  ```ts
  import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
  import { IPricingModuleService } from "@medusajs/types"
  import { Modules } from "@medusajs/utils"

export async function POST(
  request: MedusaRequest,
  res: MedusaResponse
): Promise<void> {
  const pricingModuleService: IPricingModuleService = request.scope.resolve(
    Modules.PRICING
  )

  const priceSet = await pricingModuleService.createPriceSets([
    {
      prices: [
        {
          currency_code: "eur",
          amount: 10,
          rules: {
            region_id: "reg_123",
          },
        },
      ],
    },
  ])

  res.json({ price_set: priceSet })
}
```

  </CodeTab>
  <CodeTab value="nextjs" label="Next.js App Router">

```ts
import { NextResponse } from "next/server"

import { initialize as initializePricingModule } from "@medusajs/pricing"

export async function POST(request: Request) {
  const pricingModuleService = await initializePricingModule()
  const body = await request.json()

  const priceSet = await pricingModuleService.createPriceSets([
    {
      prices: [
        {
          currency_code: "eur",
          amount: 10,
          rules: {
            region_id: "reg_123",
          },
        },
      ],
    },
  ])

  return NextResponse.json({ price_set: priceSet })
}
```

  </CodeTab>
</CodeTabs>

---

## List Price Sets

<CodeTabs groupId="app-type">
  <CodeTab value="medusa" label="Medusa API Router">

  ```ts
  import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
  import { IPricingModuleService } from "@medusajs/types"
  import { Modules } from "@medusajs/utils"

export async function GET(
  request: MedusaRequest,
  res: MedusaResponse
): Promise<void> {
  const pricingModuleService: IPricingModuleService = request.scope.resolve(
    Modules.PRICING
  )

  const priceSets = await pricingModuleService.listPriceSets()

  res.json({ price_sets: priceSets })
}
```

  </CodeTab>
  <CodeTab value="nextjs" label="Next.js App Router">

```ts
import { NextResponse } from "next/server"

import { initialize as initializePricingModule } from "@medusajs/pricing"

export async function GET(request: Request) {
  const pricingModuleService = await initializePricingModule()

  const priceSets = await pricingModuleService.listPriceSets()

  return NextResponse.json({ price_sets: priceSets })
}
```

  </CodeTab>
</CodeTabs>

---

## Retrieve a Price Set by its ID

<CodeTabs groupId="app-type">
  <CodeTab value="medusa" label="Medusa API Router">

  ```ts
  import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
  import { IPricingModuleService } from "@medusajs/types"
  import { Modules } from "@medusajs/utils"

export async function GET(
  request: MedusaRequest,
  res: MedusaResponse
): Promise<void> {
  const pricingModuleService: IPricingModuleService = request.scope.resolve(
    Modules.PRICING
  )

  const priceSet = await pricingModuleService.retrievePriceSet("pset_123")

  res.json({ price_set: priceSet })
}
```

  </CodeTab>
  <CodeTab value="nextjs" label="Next.js App Router">

```ts
import { NextResponse } from "next/server"

import { initialize as initializePricingModule } from "@medusajs/pricing"

export async function GET(request: Request) {
  const pricingModuleService = await initializePricingModule()

  const priceSet = await pricingModuleService.retrievePriceSet("pset_123")

  return NextResponse.json({ price_set: priceSet })
}
```

  </CodeTab>
</CodeTabs>

---

## Add Prices with Rules

<CodeTabs groupId="app-type">
  <CodeTab value="medusa" label="Medusa API Router">

  ```ts
  import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
  import { IPricingModuleService } from "@medusajs/types"
  import { Modules } from "@medusajs/utils"

export async function POST(
  request: MedusaRequest,
  res: MedusaResponse
): Promise<void> {
  const pricingModuleService: IPricingModuleService = request.scope.resolve(
    Modules.PRICING
  )

  const priceSet = await pricingModuleService.addPrices({
    priceSetId: "pset_123",
    prices: [
      {
        amount: 500,
        currency_code: "USD",
        rules: {
          region_id: "reg_123",
        },
      },
    ],
  })

  res.json({ price_set: priceSet })
}
```

  </CodeTab>
  <CodeTab value="nextjs" label="Next.js App Router">

```ts
import { NextResponse } from "next/server"

import { initialize as initializePricingModule } from "@medusajs/pricing"

export async function POST(request: Request) {
  const pricingModuleService = await initializePricingModule()
  const body = await request.json()

  const priceSet = await pricingModuleService.addPrices({
    priceSetId: "pset_123",
    prices: [
      {
        amount: 500,
        currency_code: "USD",
        rules: {
          region_id: "reg_123",
        },
      },
    ],
  })

  return NextResponse.json({ price_set: priceSet })
}
```

  </CodeTab>
</CodeTabs>

---

## Create Price List

<CodeTabs groupId="app-type">
  <CodeTab value="medusa" label="Medusa API Router">

  ```ts collapsibleLines="1-8" expandButtonLabel="Show Imports"
  import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
  import { IPricingModuleService } from "@medusajs/types"
  import { PriceListType } from "@medusajs/utils"
  import { Modules } from "@medusajs/utils"

export async function POST(
  request: MedusaRequest,
  res: MedusaResponse
): Promise<void> {
  const pricingModuleService: IPricingModuleService = request.scope.resolve(
    Modules.PRICING
  )

  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: ["reg_123", "reg_321"],
      },
      prices: [
        {
          amount: 400,
          currency_code: "EUR",
          price_set_id: "pset_124",
        },
      ],
    },
  ])

  res.json({ price_lists: priceLists })
}
```

  </CodeTab>
  <CodeTab value="nextjs" label="Next.js App Router">

```ts
import { NextResponse } from "next/server"
import { PriceListType } from "@medusajs/medusa"

import { initialize as initializePricingModule } from "@medusajs/pricing"
import { PriceListType } from "@medusajs/utils"

export async function POST(request: Request) {
  const pricingModuleService = await initializePricingModule()

  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: ["reg_123", "reg_321"],
      },
      prices: [
        {
          amount: 400,
          currency_code: "EUR",
          price_set_id: "pset_124",
        },
      ],
    },
  ])

  return NextResponse.json({ price_lists: priceLists })
}
```

  </CodeTab>
</CodeTabs>

---

## Calculate Prices For a Currency

<CodeTabs groupId="app-type">
  <CodeTab value="medusa" label="Medusa API Router">

  ```ts
  import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
  import { IPricingModuleService } from "@medusajs/types"
  import { Modules } from "@medusajs/utils"

export async function POST(
  request: MedusaRequest,
  res: MedusaResponse
): Promise<void> {
  const pricingModuleService: IPricingModuleService = request.scope.resolve(
    Modules.PRICING
  )

  const price = await pricingModuleService.calculatePrices(
    {
      id: ["pset_123"],
    },
    {
      context: {
        currency_code: "eur",
      },
    }
  )

  res.json({ price })
}
```

  </CodeTab>
  <CodeTab value="nextjs" label="Next.js App Router">

```ts
import { NextResponse } from "next/server"

import { initialize as initializePricingModule } from "@medusajs/pricing"

export async function GET(request: Request) {
  const pricingModuleService = await initializePricingModule()

  const price = await pricingModuleService.calculatePrices(
    {
      id: ["pset_123"],
    },
    {
      context: {
        currency_code: "eur",
      },
    }
  )

  return NextResponse.json({ price })
}
```

  </CodeTab>
</CodeTabs>

---

## More Examples

The [Pricing Module's main service reference](/references/pricing) provides a reference to all the methods available for use with examples for each.
