International sales tax calculator for Node (offline, but provides optional online VAT number fraud check). Tax rates are kept up-to-date.
You may use it to calculate VAT rates for countries in the European Union (VAT MOSS), GST in Canada, or get VAT for countries such as China, or even Hong Kong (which has no VAT).
International tax is hard (especially VAT). This library ensures rules are enforced in the code. If you see a rule that is missing or not correctly enforced, please open an issue. Also, when you use the library, make sure to specify your origin country; as it will return full international tax rates if you don't specify it (ie. the country you invoice your customers from).
You can find the raw sales tax rates JSON file here: sales_tax_rates.json
Up to date currencies, sales taxes
πΊπΈ Crafted in Portland, Maine, USA.
Crisp | Locize | TurnShift | Tally |
π You use sales-tax and you want to be listed there? Contact me.
The version history can be found in the CHANGELOG.md file.
As this library is SemVer-compatible, any breaking change would be released as a MAJOR version only. Non-breaking changes and features are released as MINOR. Tax rate updates and bug fixes are released as PATCH (note that tax rate updates may as well be bundled under a MINOR release, if it comes with new features or minor changes).
Include sales-tax
in your package.json
dependencies:
npm install --save sales-tax
If you are using TypeScript, type definitions are automatically imported.
This module may be used to acquire the billable VAT percentage for a given customer. You may also use it directly to process the total amount including VAT you should bill; and even to validate a customer's VAT number.
π΄ Important: in order to fetch the sales tax for a customer, you need to know their country (and sometimes state). The country (sometimes state) must be passed to all module methods, formatted as ISO ALPHA-2 (eg. France is FR, United States is US).
Import the module in your code:
var SalesTax = require("sales-tax");
Ensure that you specify your origin country before you use the library. This will affect how worldwide
, regional
and national
area taxes are handled from your point of view (regional
stands for the economic community, eg. the European Union).
Also, ensure that you consume correctly the charge
values that get returned. It tells you if the VAT charge should be directly invoiced to the customer via the direct
tag (you charge the VAT on your end), or if the customer should pay the VAT on their end via the reverse
tag (see VAT reverse charge). If the charge is not direct
, then the VAT rate will be 0.00
(it is up to the customer to apply their own VAT rate).
Prototype: SalesTax.setTaxOriginCountry(countryCode<string>, useRegionalTax<boolean?>)<undefined>
π«π· Charge customers from France if liable to VAT MOSS (thus worldwide
, regional
and national
VAT gets calculated from a French point of view):
SalesTax.setTaxOriginCountry("FR")
π«π· Charge customers from France if not liable to VAT MOSS (thus worldwide
, regional
and national
VAT gets calculated from a French point of view):
// Set the 'useRegionalTax' argument to false if not liable to VAT MOSS (eg. not enough turnover in another regional country)
SalesTax.setTaxOriginCountry("FR", false)
π© Unset your origin country (use default origin, full VAT rates will be applied for all countries worldwide β this is obviously not usable for your invoices):
SalesTax.setTaxOriginCountry(null)
Prototype: SalesTax.hasSalesTax(countryCode<string>)<boolean>
Notice: this method is origin-neutral. It means it return values regardless of your configured tax origin country.
Check some countries for sales tax (returns true
or false
):
var franceHasSalesTax = SalesTax.hasSalesTax("FR") // franceHasSalesTax === true
var brazilHasSalesTax = SalesTax.hasSalesTax("BR") // brazilHasSalesTax === true
var hongKongHasSalesTax = SalesTax.hasSalesTax("HK") // hongKongHasSalesTax === false
Prototype: SalesTax.hasStateSalesTax(countryCode<string>, stateCode<string>)<boolean>
Notice: this method is origin-neutral. It means it return values regardless of your configured tax origin country.
π¨π¦ Check some Canada states for sales tax (returns true
or false
):
var canadaQuebecHasSalesTax = SalesTax.hasStateSalesTax("CA", "QC") // canadaQuebecHasSalesTax === true
var canadaYukonHasSalesTax = SalesTax.hasStateSalesTax("CA", "YT") // canadaYukonHasSalesTax === false
πΊπΈ Check some US states for sales tax (returns true
or false
):
var unitedStatesCaliforniaHasSalesTax = SalesTax.hasStateSalesTax("US", "CA") // unitedStatesCaliforniaHasSalesTax === true
var unitedStatesDelawareHasSalesTax = SalesTax.hasStateSalesTax("US", "DE") // unitedStatesDelawareHasSalesTax === false
Prototype: SalesTax.getSalesTax(countryCode<string>, stateCode<string?>, taxNumber<string?>)<Promise<object>>
Notice: this method is origin-aware. It means it return values relative to your configured tax origin country.
π«π· Given a French customer VAT number (eg. here SARL CRISP IM
with VAT number FR 50833085806
):
SalesTax.getSalesTax("FR", null, "FR50833085806")
.then((tax) => {
// This customer is VAT-exempt (as it is a business)
/* tax ===
{
type : "vat",
rate : 0.00,
area : "worldwide",
exchange : "business",
charge : {
direct : false,
reverse : true
},
details : []
}
*/
});
Note: Crisp is a real living business from France, check their website there.
π«π· Given a French customer VAT number from a π«π· French tax origin (eg. here SARL CRISP IM
with VAT number FR 50833085806
):
// Set this once when initializing the library (to France)
SalesTax.setTaxOriginCountry("FR")
SalesTax.getSalesTax("FR", null, "FR50833085806")
.then((tax) => {
// This customer owes VAT in France (as it is a business, and billing is FR-to-FR)
// The `direct` tag is set to `true`, thus VAT should be charged
// The `area` tag is set to `national` as the exchange is done in France
/* tax ===
{
type : "vat",
rate : 0.20,
area : "national",
exchange : "business",
charge : {
direct : true,
reverse : false
},
details : [
{
type : "vat",
rate : 0.20
}
]
}
*/
});
π«π· Given a French customer VAT number from a π±π» Latvian tax origin (eg. here SARL CRISP IM
with VAT number FR 50833085806
):
// Set this once when initializing the library (to Latvia)
SalesTax.setTaxOriginCountry("LV")
SalesTax.getSalesTax("FR", null, "FR50833085806")
.then((tax) => {
// This customer owes a VAT reverse charge in their country (France), no VAT is due in Latvia
// The `reverse` tag is set to `true`, thus the customer should apply a reverse VAT charge in their country
// The `area` tag is set to `regional` as the exchange is done in the European Union
/* tax ===
{
type : "vat",
rate : 0.00,
area : "regional",
exchange : "business",
charge : {
direct : false,
reverse : true
},
details : []
}
*/
});
πΊπΈ Given an United States > California customer without any VAT number (eg. a consumer):
SalesTax.getSalesTax("US", "CA")
.then((tax) => {
// This customer has to pay 8.25% VAT (as it is a consumer)
/* tax ===
{
type : "vat",
rate : 0.0825,
area : "worldwide",
exchange : "consumer",
charge : {
direct : true,
reverse : false
},
details : [
{
type : "vat",
rate : 0.0825
}
]
}
*/
});
π¨π¦ Given a Canada > Ontario customer without any VAT number (eg. a consumer):
SalesTax.getSalesTax("CA", "ON")
.then((tax) => {
// This customer has to pay 5% GST + 8% HST (as it is a consumer)
/* tax ===
{
type : "gst+hst",
rate : 0.13,
area : "worldwide",
exchange : "consumer",
charge : {
direct : true,
reverse : false
},
details : [
{
type : "gst",
rate : 0.05
},
{
type : "hst",
rate : 0.08
}
]
}
*/
});
π±π» Given a Latvian customer without any VAT number (eg. a consumer):
SalesTax.getSalesTax("LV")
.then((tax) => {
// This customer has to pay 21% VAT (as it is a consumer)
/* tax ===
{
type : "vat",
rate : 0.21,
area : "worldwide",
exchange : "consumer",
charge : {
direct : true,
reverse : false
},
details : [
{
type : "vat",
rate : 0.21
}
]
}
*/
});
π±π» Given a Latvian customer without any VAT number from a π«π· French tax origin (eg. a consumer):
// Set this once when initializing the library (to France)
SalesTax.setTaxOriginCountry("FR")
SalesTax.getSalesTax("LV")
.then((tax) => {
// This customer owes VAT in Latvia (as it is a consumer, and billing is FR-to-LV)
// The `direct` tag is set to `true`, thus VAT should be charged
// The `area` tag is set to `regional` as the exchange is done in the European Union
/* tax ===
{
type : "vat",
rate : 0.21,
area : "regional",
exchange : "consumer",
charge : {
direct : true,
reverse : false
},
details : [
{
type : "vat",
rate : 0.21
}
]
}
*/
});
ππ° Given an Hong Kong-based customer (eg. a consumer):
SalesTax.getSalesTax("HK")
.then((tax) => {
// Hong Kong has no VAT
/* tax ===
{
type : "none",
rate : 0.00,
area : "worldwide",
exchange : "consumer",
charge : {
direct : false,
reverse : false
},
details : []
}
*/
});
πͺπΈ Given a Spanish customer who provided an invalid VAT number (eg. a rogue business):
SalesTax.getSalesTax("ES", null, "ESX12345523")
.then((tax) => {
// This customer has to pay 21% VAT (VAT number could not be authenticated against the VIES VAT API)
/* tax ===
{
type : "vat",
rate : 0.21,
area : "worldwide",
exchange : "consumer",
charge : {
direct : true,
reverse : false
},
details : [
{
type : "vat",
rate : 0.21
}
]
}
*/
});
Prototype: SalesTax.getAmountWithSalesTax(countryCode<string>, stateCode<string?>, amount<number?>, taxNumber<string?>)<Promise<object>>
Notice: this method is origin-aware. It means it return values relative to your configured tax origin country.
πͺπͺ Given an Estonian customer without any VAT number, buying 100.00β¬ of goods (eg. a consumer):
SalesTax.getAmountWithSalesTax("EE", null, 100.00)
.then((amountWithTax) => {
// This customer has to pay 20% VAT
/* amountWithTax ===
{
type : "vat",
rate : 0.20,
price : 100.00,
total : 120.00,
area : "worldwide",
exchange : "consumer",
charge : {
direct : true,
reverse : false
},
details : [
{
type : "vat",
rate : 0.20,
amount : 20.00
}
]
}
*/
});
Prototype: SalesTax.validateTaxNumber(countryCode<string>, taxNumber<string?>)<Promise<boolean>>
π«π· Given a French customer VAT number (eg. here SARL CRISP IM
with VAT number FR 50833085806
):
SalesTax.validateTaxNumber("FR", "FR50833085806")
.then((isValid) => {
// isValid === true
});
πΊπΈ Given an United States customer without any VAT number (eg. a consumer):
SalesTax.validateTaxNumber("US")
.then((isValid) => {
// isValid === false
});
π±π» Given a Latvian customer without any VAT number (eg. a consumer):
SalesTax.validateTaxNumber("LV")
.then((isValid) => {
// isValid === false
});
πͺπΈ Given a Spanish customer who provided an invalid VAT number (eg. a rogue business):
SalesTax.validateTaxNumber("ES", "ESX12345523")
.then((isValid) => {
// isValid === false
});
Prototype: SalesTax.getTaxExchangeStatus(countryCode<string>, stateCode<string?>, taxNumber<string?>)<Promise<object>>
Notice: this method is origin-aware. It means it return values relative to your configured tax origin country.
π«π· Given a French customer VAT number (eg. here SARL CRISP IM
with VAT number FR 50833085806
):
SalesTax.getTaxExchangeStatus("FR", null, "FR50833085806")
.then((exchangeStatus) => {
/* exchangeStatus ===
{
exchange : "business",
area : "worldwide",
exempt : true
}
*/
});
π²π¦ Given a Morocco-based customer:
SalesTax.getTaxExchangeStatus("MA")
.then((exchangeStatus) => {
/* exchangeStatus ===
{
exchange : "consumer",
area : "worldwide",
exempt : false
}
*/
});
πΊπΈ Given an United States > Delaware-based customer:
SalesTax.getTaxExchangeStatus("US", "DE")
.then((exchangeStatus) => {
/* exchangeStatus ===
{
exchange : "consumer",
area : "worldwide",
exempt : true
}
*/
});
ππ° Given an Hong Kong-based customer:
SalesTax.getTaxExchangeStatus("HK")
.then((exchangeStatus) => {
/* exchangeStatus ===
{
exchange : "consumer",
area : "worldwide",
exempt : true
}
*/
});
Prototype: SalesTax.toggleEnabledTaxNumberValidation(enabled<boolean>)<undefined>
π Enable tax number validation (enabled by default β use only if you disabled it previously):
SalesTax.toggleEnabledTaxNumberValidation(true)
π Disable tax number validation (do not check tax number syntax):
SalesTax.toggleEnabledTaxNumberValidation(false)
Prototype: SalesTax.toggleEnabledTaxNumberFraudCheck(enabled<boolean>)<undefined>
Notice: fraud check requires tax number validation to be enabled.
π Enable tax number fraud check (enable hitting against external APIs to verify tax numbers against fraud):
SalesTax.toggleEnabledTaxNumberFraudCheck(true)
π Disable tax number fraud check (disabled by default β use only if you enabled it previously):
SalesTax.toggleEnabledTaxNumberFraudCheck(false)
The offline tax data is pulled from Value-added tax (VAT) rates β PwC (last updated: 27th June 2023).
It is kept up-to-date year-by-year with tax changes worldwide.
Some countries have multiple sales tax, eg. Brazil. In those cases, the returned sales tax is the one on services. Indeed, I consider most users of this module use it for their SaaS business β in other words, service businesses.
As tax rate changes happen to some countries in the word on a yearly basis, sales-tax
automatically uses the current tax rate relative to current date and time, ie. whenever you call the library functions.
At a technical level, tax rate changes for a country can be easily scheduled from the tax rates JSON file by moving the current tax rate in a before
object, which then stores the country tax rate before enforcement date, and then the future tax rate is stored in the main object. Note that as a library user, you do not have to schedule tax rate changes, sales-tax
handles it for you automatically.
Please make sure you always keep sales-tax
up-to-date with the latest NPM version, as those tax rate changes are stored in an offline JSON file, which requires a manual library update.
For instance, Germany changed their VAT rate from 19% down to 16% as of 1st July 2020:
"DE": {
"type": "vat",
"rate": 0.16,
"before": {
"2020-06-30T22:00:00.000Z": {
"type": "vat",
"rate": 0.19
}
}
}
As international tax rules can be very complex depending on your business legal structure (eg. if you run a nexus in an US state, you may owe sales tax to this US state, even if you charge from the UK); sales-tax
does not void returned tax rate for worldwide
countries.
Thus, when the country is worldwide
relative to your billing origin country, you need to handle things your own way.
To make things easier for you, sales-tax
returns an area
parameter in the SalesTax.getSalesTax
, that is either worldwide
, regional
or national
(this depends on your configured origin country). For regional
and national
areas, you can trust the returned rate. However, you may need to override all worldwide
area rates and void them all to zero; for instance if you charge from France to the United States, and you know that you do not owe sales tax in the US as you do not run a nexus company in the US.
sales-tax
for such international charges (ie. worldwide
area). Therefore, such a worldwide
area VAT should be voided if you consider that this does not apply to you, while national
or regional
area VAT should be used as normal. Always check with your accountant when in doubt.
European VAT numbers can be fraud-checked against the official ec.europa.eu
VIES VAT API, which return whether a given VAT number exists or not. This helps you ensure a customer-provided VAT number really exists. This feature, as it may incur significant delays (while querying the VIES VAT API) is disabled by default. There's a switch to enable it.
In all cases, the syntax of the European VAT numbers get validated from offline rules. Although, it only checks number syntaxical correctness; thus it is not sufficient to tell if the number exists or not.
You can manually check a VAT number on VIES VAT number validation.
United States EIN (U.S. Employer Identification Number) are validated against EIN format rules.
Canada BN (Business Number) are validated against BN format rules.
If a country or economic community is not listed here, provided tax identification numbers are ignored for those countries (considered as invalid β so do not rely on validation methods as a source of truth).
If you need tax number validation for a missing country, feel free to submit a Pull Request.