medusajs/medusa · error · MedusaError

Country with code ${shippingAddress.country_code} is not wit

Error message

Country with code ${shippingAddress.country_code} is not within region ${data.region.name}

What it means

updateCartWorkflow validates that, when a shipping address country_code is provided, that country belongs to the cart's region (regions enumerate allowed countries). A mismatch throws invalid_data because taxes and shipping depend on region-country consistency.

Source

Thrown at packages/core/core-flows/src/cart/workflows/update-cart.ts:78

    const data_ = {
      ...updateCartData,
      currency_code: data.region?.currency_code,
      region_id: data.region?.id, // This is either the region from the input or the region from the cart or null
    }

    // When the region is updated, we do a few things:
    // - We need to make sure the provided shipping address country code is in the new region
    // - We clear the shipping address if the new region has more than one country
    const regionIsNew = data.region?.id !== data.cartToUpdate.region?.id
    const shippingAddress = data.input.shipping_address

    if (shippingAddress?.country_code) {
      const country = data.region.countries.find(
        (c: { iso_2: string }) => c.iso_2 === shippingAddress.country_code
      )

      if (!country) {
        throw new MedusaError(
          MedusaError.Types.INVALID_DATA,
          `Country with code ${shippingAddress.country_code} is not within region ${data.region.name}`
        )
      }

      data_.shipping_address = {
        ...shippingAddress,
        country_code: country.iso_2,
      }
    }

    if (regionIsNew) {
      if (data.region.countries.length === 1) {
        data_.shipping_address = {
          country_code: data.region.countries[0].iso_2,
        }
      }

View on GitHub (pinned to 5e06e544a2)

Solutions

  1. Add the country to the region's countries via Admin (POST /admin/regions/:id/countries) and retry
  2. Or set the cart's region_id to a region that includes the customer's country before updating the address
  3. Restrict the address form's country selector to the cart region's countries

Example fix

// before
await updateCartWorkflow(container).run({ input: { id: cart_id, shipping_address: { country_code: 'ca', ... } } })

// after (only allow region countries in the form)
const { data: [region] } = await query.graph({ entity: 'region', fields: ['countries.iso_2'], filters: { id: cart.region_id } })
const allowed = region.countries.map((c) => c.iso_2)
if (!allowed.includes(address.country_code)) throw new Error('Pick a supported country')
await updateCartWorkflow(container).run({ input: { id: cart_id, shipping_address: address } })
Defensive patterns

Strategy: validation

Validate before calling

const { data: [region] } = await query.graph({ entity: 'region', fields: ['countries.iso_2'], filters: { id: cart.region_id } })
const allowed = region.countries.map((c) => c.iso_2)
if (!allowed.includes(address.country_code)) throw new Error('Country not in cart region — switch region or pick allowed country')

Type guard

const isCountryInRegion = (code: string, region: { countries: { iso_2: string }[] }) =>
  region.countries.some((c) => c.iso_2 === code)

Prevention

When it happens

Trigger: Calling updateCart with shipping_address.country_code set to a country that is not in the cart region's countries list — e.g. address in 'us' while the region only includes EU countries.

Common situations: Default region covering few countries while customers enter addresses from anywhere; address forms not filtered by region; changing the address without switching the cart's region; regions misconfigured after launch excluding expected countries.

Related errors


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