medusajs/medusa · error · MedusaError

invalid_data

invalid_data

Error message

Method calculatePrices requires currency_code in the pricing context

What it means

calculatePrices in the pricing module requires the pricing context to include a currency_code, because prices are matched per currency. The method deletes and checks context.currency_code first and throws INVALID_DATA when it is absent or empty.

Source

Thrown at packages/modules/pricing/src/repositories/pricing.ts:84

  async calculatePrices(
    pricingFilters: PricingFilters,
    pricingContext: PricingContext = { context: {} },
    sharedContext: Context = {}
  ): Promise<CalculatedPriceSetDTO[]> {
    const manager = this.getActiveManager<SqlEntityManager>(sharedContext)
    const knex = manager.getKnex()
    const context = { ...(pricingContext.context || {}) }

    // Extract quantity and currency from context
    const quantity = context.quantity as number | undefined
    delete context.quantity

    // Currency code is required
    const currencyCode = context.currency_code as string | undefined
    delete context.currency_code

    if (!currencyCode) {
      throw new MedusaError(
        MedusaError.Types.INVALID_DATA,
        `Method calculatePrices requires currency_code in the pricing context`
      )
    }

    // Generate flatten key-value pairs for rule matching
    const flattenedKeyValuePairs = flattenObjectToKeyValuePairs(context)

    // First filter by value presence
    let flattenedContext = Object.entries(flattenedKeyValuePairs).filter(
      ([, value]) => {
        const isValuePresent = !Array.isArray(value) && isPresent(value)
        const isArrayPresent = Array.isArray(value) && value.flat(1).length

        return isValuePresent || isArrayPresent
      }
    )

View on GitHub (pinned to 5e06e544a2)

Solutions

  1. Pass currency_code in the context: context: { currency_code: 'usd' }
  2. If pricing a cart, ensure the cart has a region (or explicit currency) before calculating prices
  3. Default the currency from the region/store settings before calling calculatePrices

Example fix

// before
await pricingModuleService.calculatePrices({ ids, context: {} })

// after
await pricingModuleService.calculatePrices({
  ids,
  context: { currency_code: 'usd' },
})
Defensive patterns

Strategy: validation

Validate before calling

function buildPricingContext(c?: { currency_code?: string }) {
  const currency_code = c?.currency_code ?? 'usd' // or throw early
  if (!currency_code) throw new Error('currency_code required')
  return { ...c, currency_code }
}
await pricingService.calculatePrices({ id, context: buildPricingContext(ctx) })

Type guard

const hasCurrencyCode = (
  ctx: unknown
): ctx is { currency_code: string } =>
  typeof ctx === 'object' && ctx !== null &&
  typeof (ctx as any).currency_code === 'string' &&
  (ctx as any).currency_code.length > 0

Try / catch

try {
  await pricingService.calculatePrices({ id, context })
} catch (e) {
  if (e instanceof MedusaError && e.type === MedusaError.Types.INVALID_DATA && /currency_code/.test(e.message)) {
    return computeFallbackPrice() // or return null price
  }
  throw e
}

Prevention

When it happens

Trigger: Calling pricingModuleService.calculatePrices({ context: {} }) or a context built from a cart/product request where currency_code was never set (e.g. missing region currency on the cart, or a workflow that forgot to pass it).

Common situations: Creating a cart without a region or currency, calling product pricing with an incomplete context object, or upstream code assuming currency_code is optional.

Related errors


AI-assisted analysis of medusajs/medusa@5e06e544a2 (2026-08-27). Data as JSON: /api/errors/1f476835013039ca. Report an issue: GitHub.