Skip to main content
Skip to main content

Examples of the Tax Module

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

Create a Tax Region

import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { ITaxModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"

export async function GET(
req: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const taxModuleService: ITaxModuleService = req.scope.resolve(
ModuleRegistrationName.TAX
)

const taxRegion = await taxModuleService.createTaxRegions({
country_code: "US",
default_tax_rate: {
rate: 10,
name: "Default rate",
},
})

res.json({
tax_region: taxRegion,
})
}

List Tax Regions

import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { ITaxModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"

export async function GET(
req: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const taxModuleService: ITaxModuleService = req.scope.resolve(
ModuleRegistrationName.TAX
)

res.json({
tax_regions: await taxModuleService.list(),
})
}

Create Tax Rate with Rules

import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { ITaxModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"

export async function POST(
req: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const taxModuleService: ITaxModuleService = req.scope.resolve(
ModuleRegistrationName.TAX
)

const taxRate = await taxModuleService.create({
tax_region_id: "txreg_123",
name: "Custom rate",
rate: 15,
rules: [
{
reference: "product_type",
reference_id: "ptyp_1",
},
{
reference: "product",
reference_id: "prod_123",
},
],
})

res.json({
tax_rate: taxRate,
})
}

List Tax Rates

import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { ITaxModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"

export async function GET(
req: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const taxModuleService: ITaxModuleService = req.scope.resolve(
ModuleRegistrationName.TAX
)

res.json({
tax_rates: await taxModuleService.list(),
})
}

Get Tax Lines

import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { ITaxModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"

export async function POST(
req: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const taxModuleService: ITaxModuleService = req.scope.resolve(
ModuleRegistrationName.TAX
)

const taxLines = await taxModuleService.getTaxLines(
[
{
id: "cali_123",
product_id: "prod_123",
unit_price: 1000,
},
{
id: "casm_123",
shipping_option_id: "so_123",
unit_price: 2000,
},
],
{
address: {
country_code: "us",
postal_code: "123456",
},
customer: {
id: "cus_123",
email: "user@example.com",
customer_groups: ["VIP"],
},
}
)

res.json({
tax_lines: taxLines,
})
}

More Examples

The module interface reference provides a reference to all the methods available for use with examples for each.

Was this section helpful?