Examples of the Region Module
In this guide, you’ll find common examples of how you can use the Region Module in your application.
Create a Region
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { IRegionModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export async function POST(
req: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const regionModuleService: IRegionModuleService =
req.scope.resolve(ModuleRegistrationName.REGION)
const region = await regionModuleService.create({
name: "Europe",
currency_code: "eur",
})
res.json({
region,
})
}
import { NextResponse } from "next/server"
import {
initialize as initializeRegionModule,
} from "@medusajs/region"
export async function POST(request: Request) {
const regionModuleService = await initializeRegionModule()
const region = await regionModuleService.create({
name: "Europe",
currency_code: "eur",
})
res.json({
region,
})
}
List Regions
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { IRegionModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export async function GET(
req: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const regionModuleService: IRegionModuleService =
req.scope.resolve(ModuleRegistrationName.REGION)
res.json({
regions: await regionModuleService.list(),
})
}
Retrieve a Region by its ID
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { IRegionModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export async function GET(
req: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const regionModuleService: IRegionModuleService =
req.scope.resolve(ModuleRegistrationName.REGION)
const region = await regionModuleService.retrieve("reg_123")
res.json({ region })
}
import { NextResponse } from "next/server"
import {
initialize as initializeRegionModule,
} from "@medusajs/region"
export async function GET(
request: Request
) {
const regionModuleService = await initializeRegionModule()
const region = await regionModuleService.retrieve("reg_123")
return NextResponse.json({ region })
}
Update a Region
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { IRegionModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export async function POST(
req: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const regionModuleService: IRegionModuleService =
req.scope.resolve(ModuleRegistrationName.REGION)
const region = await regionModuleService.update("reg_123", {
automatic_taxes: false,
})
res.json({ region })
}
import { NextResponse } from "next/server"
import {
initialize as initializeRegionModule,
} from "@medusajs/region"
export async function POST(
request: Request
) {
const regionModuleService = await initializeRegionModule()
const region = await regionModuleService.update("reg_123", {
automatic_taxes: false,
})
return NextResponse.json({ region })
}
Delete a Region
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { IRegionModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export async function DELETE(
req: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const regionModuleService: IRegionModuleService =
req.scope.resolve(ModuleRegistrationName.REGION)
await regionModuleService.delete("reg_123")
res.status(200)
}
More Examples
The Region Module interface reference provides a reference to all the methods available for use with examples for each.
Was this section helpful?