1 {"slug":"/references/tax/provider","title":"How to Create a Tax Module Provider","frontmatter":{"slug":"/references/tax/provider","tags":["tax","server","how to"],"sidebar_label":"Create Tax Provider","keywords":["tax","provider","integration"]},"toc":[{"title":"Overview","id":"overview","level":2},{"title":"Implementation Example","id":"implementation-example","level":2},{"title":"Understanding Tax Module Provider Implementation","id":"understanding-tax-module-provider-implementation","level":2},{"title":"1. Create Module Provider Directory","id":"1-create-module-provider-directory","level":2},{"title":"2. Create the Tax Module Provider's Service","id":"2-create-the-tax-module-provider-s-service","level":2},{"title":"Identifier Property","id":"identifier-property","level":3},{"title":"constructor","id":"constructor","level":3},{"title":"getIdentifier","id":"getidentifier","level":3},{"title":"Example","id":"example","level":4},{"title":"Returns","id":"getidentifier-returns","level":4},{"title":"getTaxLines","id":"gettaxlines","level":3},{"title":"Example","id":"example-1","level":4},{"title":"Parameters","id":"gettaxlines-parameters","level":4},{"title":"Returns","id":"gettaxlines-returns","level":4},{"title":"3. Create Module Provider Definition File","id":"3-create-module-provider-definition-file","level":2},{"title":"4. Use Module Provider","id":"4-use-module-provider","level":2}],"blocks":[{"kind":"markdown","html":"In this document, you’ll learn how to create a Tax Module Provider to use with the Tax Module, and the methods to implement."},{"kind":"markdown","html":"---"},{"kind":"heading","level":2,"text":"Overview","id":"overview"},{"kind":"markdown","html":"A Tax Module Provider is used to retrieve the tax lines in a provided context. The Tax Module provides a default `system` provider. You can create your own Tax Module Provider, either in a plugin, in a module provider, or directly in your Medusa application's codebase, then use it in any tax region.\n\n---"},{"kind":"heading","level":2,"text":"Implementation Example","id":"implementation-example"},{"kind":"markdown","html":"As you implement your Tax Module Provider, it can be useful to refer to an existing provider and how it's implemeted.\n\nIf you need to refer to an existing implementation as an example, check the [Avalara Tax Module Provider tutorial](https://docs.medusajs.com/resources/integrations/guides/avalara).\n\n---"},{"kind":"heading","level":2,"text":"Understanding Tax Module Provider Implementation","id":"understanding-tax-module-provider-implementation"},{"kind":"markdown","html":"The Tax Module Provider handles calculating taxes with a third-party provider. However, it's not responsible for managing tax concepts within Medusa, such as creating a tax region. The Tax Module uses your Tax Module Provider within core operations.\n\nFor example, during checkout, the Tax Module Provider of the tax region that the customer is in is used to calculate the tax for the cart and order. So, you only have to implement the third-party tax calculation logic in your Tax Module Provider.\n\n---"},{"kind":"heading","level":2,"text":"1. Create Module Provider Directory","id":"1-create-module-provider-directory"},{"kind":"markdown","html":"Start by creating a new directory for your module provider.\n\nIf you're creating the module provider in a Medusa application, create it under the `src/modules` directory. For example, `src/modules/my-tax`.\n\nIf you're creating the module provider in a plugin, create it under the `src/providers` directory. For example, `src/providers/my-tax`."},{"kind":"note","variant":"note","html":"The rest of this guide always uses the `src/modules/my-tax` directory as an example."},{"kind":"markdown","html":"---"},{"kind":"heading","level":2,"text":"2. Create the Tax Module Provider's Service","id":"2-create-the-tax-module-provider-s-service"},{"kind":"markdown","html":"Create the file `src/modules/my-tax/service.ts` that holds the module provider's main service. It must implement the `ITaxProvider` interface imported from `@medusajs/framework/types`:\n\n```ts title=\"src/modules/my-tax/service.ts\"\nimport { ITaxProvider } from \"@medusajs/framework/types\"\n\nexport default class MyTaxProvider implements ITaxProvider {\n // TODO implement methods\n}\n```"},{"kind":"heading","level":3,"text":"Identifier Property","id":"identifier-property"},{"kind":"markdown","html":"Each tax provider has a unique identifier defined in its class. The provider's ID\nwill be stored as `tp_{identifier}_{id}`, where `{id}` is the provider's `id`\nproperty in the `medusa-config.ts`.\n\nFor example:\n\n```ts title=\"src/modules/my-tax/service.ts\"\nexport default class MyTaxProvider implements ITaxProvider {\n static identifier = \"my-tax\"\n // ...\n}\n```"},{"kind":"heading","level":3,"text":"constructor","id":"constructor"},{"kind":"markdown","html":"You can use the `constructor` of your Tax Module Provider's service to access the resources registered in the [Module Container](https://docs.medusajs.com/resources/medusa-container-resources#module-container-resources).\n\nYou can also use the constructor to initialize your integration with the third-party provider. For example, if you use a client to connect to the third-party provider’s APIs, you can initialize it in the constructor and use it in other methods in the service.\n\nAdditionally, if you’re creating your tax provider as a plugin or a module provider to be installed in any Medusa application and you want to access its options, you can access them in the second parameter of the constructor.\n\nFor example:\n\n```ts\nimport {\n ITaxProvider,\n Logger\n} from \"@medusajs/framework/types\"\n\ntype InjectedDependencies = {\n logger: Logger\n}\n\ntype Options = {\n apiKey: string\n}\n\nexport default class MyTaxProvider implements ITaxProvider {\n static identifier = \"my-tax\"\n protected logger_: Logger\n protected options_: Options\n // assuming you're initializing a client\n protected client\n\n constructor (\n { logger }: InjectedDependencies,\n options: Options\n ) {\n this.logger_ = logger\n this.options_ = options\n\n // assuming you're initializing a client\n this.client = new Client(options)\n }\n}\n```\n\n---"},{"kind":"heading","level":3,"text":"getIdentifier","id":"getidentifier"},{"kind":"markdown","html":"This method is used to retrieve the unique identifier of the tax provider."},{"kind":"heading","level":4,"text":"Example","id":"example"},{"kind":"markdown","html":"```ts\nexport default class MyTaxProvider implements ITaxProvider {\n static identifier = \"my-tax\"\n \n getIdentifier(): string {\n return MyTaxProvider.identifier\n }\n}\n```"},{"kind":"heading","level":4,"text":"Returns","id":"getidentifier-returns"},{"kind":"typeList","sectionTitle":"getIdentifier","types":[{"name":"string","type":"`string`","optional":false,"defaultValue":"","description":"The unique identifier of the tax provider.","expandable":false,"children":[]}]},{"kind":"heading","level":3,"text":"getTaxLines","id":"gettaxlines"},{"kind":"markdown","html":"This method is used to retrieve the tax lines of items and shipping methods. It's used during checkout \nwhen the `getTaxLines` method of the Tax Module's main service is called for a tax\nregion that uses this tax provider."},{"kind":"heading","level":4,"text":"Example","id":"example-1"},{"kind":"markdown","html":"An example of how this method is implemented in the `system` provider:\n\n```ts\n// ...\n\nexport default class SystemTaxService implements ITaxProvider {\n // ...\n\n async getTaxLines(\n itemLines: TaxTypes.ItemTaxCalculationLine[],\n shippingLines: TaxTypes.ShippingTaxCalculationLine[],\n _: TaxTypes.TaxCalculationContext\n ): Promise<(TaxTypes.ItemTaxLineDTO | TaxTypes.ShippingTaxLineDTO)[]> {\n let taxLines: (TaxTypes.ItemTaxLineDTO | TaxTypes.ShippingTaxLineDTO)[] =\n itemLines.flatMap((l) => {\n return l.rates.map((r) => ({\n rate_id: r.id, // this is optional. When integrating with a third-party, you don't need to provide it\n rate: r.rate || 0, // For example, 10 for 10%\n name: r.name,\n code: r.code,\n line_item_id: l.line_item.id,\n provider_id: this.getIdentifier(),\n }))\n })\n\n taxLines = taxLines.concat(\n shippingLines.flatMap((l) => {\n return l.rates.map((r) => ({\n rate_id: r.id, // this is optional. When integrating with a third-party, you don't need to provide it\n rate: r.rate || 0, // For example, 10 for 10%\n name: r.name,\n code: r.code,\n shipping_line_id: l.shipping_line.id,\n provider_id: this.getIdentifier(),\n }))\n })\n )\n\n return taxLines\n }\n}\n```"},{"kind":"heading","level":4,"text":"Parameters","id":"gettaxlines-parameters"},{"kind":"typeList","sectionTitle":"getTaxLines","types":[{"name":"itemLines","type":"[ItemTaxCalculationLine](/references/tax_provider/types/tax_provider.ItemTaxCalculationLine)[]","description":"The line item lines to calculate taxes for.","optional":false,"defaultValue":"","expandable":false,"children":[{"name":"line_item","type":"[TaxableItemDTO](/references/tax/interfaces/tax.TaxableItemDTO)","description":"The line item to calculate taxes for.","optional":false,"defaultValue":"","expandable":false,"children":[{"name":"id","type":"`string`","description":"The ID of the taxable item.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"product_id","type":"`string`","description":"The associated product's ID.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"product_type_id","type":"`string`","description":"The ID of the type of the item's product.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"quantity","type":"[BigNumberInput](/references/tax/types/tax.BigNumberInput)","description":"The quantity of the taxable item.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"unit_price","type":"[BigNumberInput](/references/tax/types/tax.BigNumberInput)","description":"The unit price of the taxable item.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"currency_code","type":"`string`","description":"The ISO 3 character currency code of the taxable item.","optional":true,"defaultValue":"","expandable":false,"children":[]}]},{"name":"rates","type":"[TaxRateDTO](/references/tax/interfaces/tax.TaxRateDTO)[]","description":"The rates applicable on the item.","optional":false,"defaultValue":"","expandable":false,"children":[{"name":"id","type":"`string`","description":"The ID of the tax rate.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"rate","type":"`null` \\| `number`","description":"The rate to charge.","optional":false,"defaultValue":"","example":"10","expandable":false,"children":[]},{"name":"code","type":"`null` \\| `string`","description":"The code the tax rate is identified by.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"name","type":"`string`","description":"The name of the Tax Rate.","optional":false,"defaultValue":"","example":"VAT","expandable":false,"children":[]},{"name":"metadata","type":"`null` \\| `Record<string, unknown>`","description":"Holds custom data in key-value pairs.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"tax_region_id","type":"`string`","description":"The ID of the associated tax region.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"is_combinable","type":"`boolean`","description":"Whether the tax rate should be combined with parent rates.\n\nLearn more [here](https://docs.medusajs.com/resources/commerce-modules/tax/tax-rates-and-rules#combinable-tax-rates).","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"is_default","type":"`boolean`","description":"Whether the tax rate is the default rate for the region.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"created_at","type":"`string` \\| `Date`","description":"The creation date of the tax rate.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"updated_at","type":"`string` \\| `Date`","description":"The update date of the tax rate.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"deleted_at","type":"`null` \\| `Date`","description":"The deletion date of the tax rate.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"created_by","type":"`null` \\| `string`","description":"Who created the tax rate. For example, the ID of the user that created the tax rate.","optional":false,"defaultValue":"","expandable":false,"children":[]}]}]},{"name":"shippingLines","type":"[ShippingTaxCalculationLine](/references/tax_provider/types/tax_provider.ShippingTaxCalculationLine)[]","description":"The shipping method lines to calculate taxes for.","optional":false,"defaultValue":"","expandable":false,"children":[{"name":"shipping_line","type":"[TaxableShippingDTO](/references/tax/interfaces/tax.TaxableShippingDTO)","description":"The shipping method to calculate taxes for.","optional":false,"defaultValue":"","expandable":false,"children":[{"name":"id","type":"`string`","description":"The ID of the taxable shipping.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"shipping_option_id","type":"`string`","description":"The associated shipping option's ID.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"unit_price","type":"[BigNumberInput](/references/tax/types/tax.BigNumberInput)","description":"The unit price of the taxable shipping.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"currency_code","type":"`string`","description":"The ISO 3 character currency code of the taxable shipping.","optional":true,"defaultValue":"","expandable":false,"children":[]}]},{"name":"rates","type":"[TaxRateDTO](/references/tax/interfaces/tax.TaxRateDTO)[]","description":"The rates applicable on the shipping method.","optional":false,"defaultValue":"","expandable":false,"children":[{"name":"id","type":"`string`","description":"The ID of the tax rate.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"rate","type":"`null` \\| `number`","description":"The rate to charge.","optional":false,"defaultValue":"","example":"10","expandable":false,"children":[]},{"name":"code","type":"`null` \\| `string`","description":"The code the tax rate is identified by.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"name","type":"`string`","description":"The name of the Tax Rate.","optional":false,"defaultValue":"","example":"VAT","expandable":false,"children":[]},{"name":"metadata","type":"`null` \\| `Record<string, unknown>`","description":"Holds custom data in key-value pairs.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"tax_region_id","type":"`string`","description":"The ID of the associated tax region.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"is_combinable","type":"`boolean`","description":"Whether the tax rate should be combined with parent rates.\n\nLearn more [here](https://docs.medusajs.com/resources/commerce-modules/tax/tax-rates-and-rules#combinable-tax-rates).","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"is_default","type":"`boolean`","description":"Whether the tax rate is the default rate for the region.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"created_at","type":"`string` \\| `Date`","description":"The creation date of the tax rate.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"updated_at","type":"`string` \\| `Date`","description":"The update date of the tax rate.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"deleted_at","type":"`null` \\| `Date`","description":"The deletion date of the tax rate.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"created_by","type":"`null` \\| `string`","description":"Who created the tax rate. For example, the ID of the user that created the tax rate.","optional":false,"defaultValue":"","expandable":false,"children":[]}]}]},{"name":"context","type":"[TaxCalculationContext](/references/tax/interfaces/tax.TaxCalculationContext)","description":"The context relevant and useful for the taxes calculation.","optional":false,"defaultValue":"","expandable":false,"children":[{"name":"locale","type":"`string`","description":"The locale of the tax calculation.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"address","type":"`object`","description":"The customer's address","optional":false,"defaultValue":"","expandable":false,"children":[{"name":"country_code","type":"`string`","description":"The ISO 2 character currency code.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"province_code","type":"`null` \\| `string`","description":"The lower-case [ISO 3166-2](https://en.wikipedia.org/wiki/ISO\\_3166-2) province or state code.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"address_1","type":"`string`","description":"The first line of the address.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"address_2","type":"`null` \\| `string`","description":"The second line of the address","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"city","type":"`string`","description":"The city.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"postal_code","type":"`string`","description":"The postal code.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"metadata","type":"`null` \\| `Record<string, unknown>`","description":"Address metadata.","optional":true,"defaultValue":"","expandable":false,"children":[]}]},{"name":"customer","type":"`object`","description":"The customer's details.","optional":true,"defaultValue":"","expandable":false,"children":[{"name":"id","type":"`string`","description":"The ID of the customer.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"email","type":"`string`","description":"The email of the customer.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"customer_groups","type":"`string`[]","description":"The groups that the customer belongs to.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"metadata","type":"`null` \\| `Record<string, unknown>`","description":"Customer metadata.","optional":true,"defaultValue":"","expandable":false,"children":[]}]},{"name":"is_return","type":"`boolean`","description":"Whether the tax lines are calculated for an order return.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"shipping_methods","type":"`object`[]","description":"The shipping method details.","optional":true,"defaultValue":"","expandable":false,"children":[{"name":"id","type":"`string`","description":"The associated shipping method's ID.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"name","type":"`string`","description":"The name of the shipping method.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"shipping_option_id","type":"`string`","description":"The associated shipping options's ID.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"amount","type":"`number`","description":"The amount of the shipping method.","optional":false,"defaultValue":"","expandable":false,"children":[]}]},{"name":"additional_context","type":"`Record<string, unknown>`","description":"Additional context to pass to the underlying tax provider.\n\nUse this to forward custom data that isn't part of the framework-provided\nfields but that your tax provider needs to calculate tax lines accurately.\nIn cart and order tax workflows, this is populated by the `setTaxLineContext` hook.","optional":true,"defaultValue":"","expandable":false,"children":[]}]}]},{"kind":"heading","level":4,"text":"Returns","id":"gettaxlines-returns"},{"kind":"typeList","sectionTitle":"getTaxLines","types":[{"name":"Promise","type":"Promise&#60;([ItemTaxLineDTO](/references/tax/interfaces/tax.ItemTaxLineDTO) \\| [ShippingTaxLineDTO](/references/tax/interfaces/tax.ShippingTaxLineDTO))[]&#62;","optional":false,"defaultValue":"","description":"The list of calculated line item and shipping tax lines.\nIf an item in the array has the `shipping_line_id` property, then it's a shipping tax line. Otherwise, if it has\nthe `line_item_id` property, then it's a line item tax line.","expandable":false,"children":[{"name":"(ItemTaxLineDTO \\| ShippingTaxLineDTO)[]","type":"([ItemTaxLineDTO](/references/tax/interfaces/tax.ItemTaxLineDTO) \\| [ShippingTaxLineDTO](/references/tax/interfaces/tax.ShippingTaxLineDTO))[]","optional":false,"defaultValue":"","description":"","expandable":false,"children":[{"name":"ItemTaxLineDTO \\| ShippingTaxLineDTO","type":"[ItemTaxLineDTO](/references/tax/interfaces/tax.ItemTaxLineDTO) \\| [ShippingTaxLineDTO](/references/tax/interfaces/tax.ShippingTaxLineDTO)","optional":false,"defaultValue":"","description":"","expandable":false,"children":[]}]}]}]},{"kind":"markdown","html":"---"},{"kind":"heading","level":2,"text":"3. Create Module Provider Definition File","id":"3-create-module-provider-definition-file"},{"kind":"markdown","html":"Create the file `src/modules/my-tax/index.ts` with the following content:\n\n```ts title=\"src/modules/my-tax/index.ts\"\nimport MyTaxProvider from \"./service\"\nimport { \n ModuleProvider, \n Modules\n} from \"@medusajs/framework/utils\"\n\nexport default ModuleProvider(Modules.TAX, {\n services: [MyTaxProvider],\n})\n```\n\nThis exports the module provider's definition, indicating that the `MyTaxProvider` is the module provider's service."},{"kind":"note","variant":"note","title":"Tip","html":"A tax module provider can have export multiple provider services, where each are registered as a separate tax provider."},{"kind":"markdown","html":"---"},{"kind":"heading","level":2,"text":"4. Use Module Provider","id":"4-use-module-provider"},{"kind":"markdown","html":"To use your Tax Module Provider, add it to the `providers` array of the Tax Module in `medusa-config.ts`:\n\n```ts title=\"medusa-config.ts\"\nmodule.exports = defineConfig({\n // ...\n modules: [\n {\n resolve: \"@medusajs/medusa/tax\",\n options: {\n providers: [\n {\n // if module provider is in a plugin, use `plugin-name/providers/my-tax`\n resolve: \"./src/modules/my-tax\",\n id: \"my-tax\",\n options: {\n // provider options...\n },\n },\n ],\n },\n },\n ]\n})\n```"}]}