• Stars
    star
    298
  • Rank 139,663 (Top 3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 7 years ago
  • Updated 6 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

💰 International sales tax calculator for Node (offline, but provides optional online VAT number fraud check). Tax rates are kept up-to-date.

node-sales-tax

Test and Build Build and Release NPM Downloads Buy Me A Coffee

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

🇺🇸 Crafted in Portland, Maine, USA.

Who uses it?

Crisp Locize TurnShift Tally

👋 You use sales-tax and you want to be listed there? Contact me.

Last changes

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).

How to install?

Include sales-tax in your package.json dependencies:

npm install --save sales-tax

If you are using TypeScript, type definitions are automatically imported.

How to use?

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

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).

Specify the country you charge from

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)

✅ Check if a country has sales tax

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

Check if a state has sales tax (in a country)

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

✅ Get the sales tax for a customer

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
          }
        ]
      }
     */
  });

Process the price including sales tax for a customer

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
          }
        ]
      }
     */
  });

Validate tax number for a customer

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
  });

Get tax exchange status for a customer (exempt + area + exchange)

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
      }
     */
  });

Disable / enable tax number validation

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)

Disable / enable tax number fraud check

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)

Where is the offline tax data pulled from?

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.

What happens if a country or state schedules a tax rate change?

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
    }
  }
}

I bill from the EU, but sales tax is still being returned for non-EU countries!

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.

⚠️ Note that this would also apply to non-EU businesses charging customers outside of their jurisdiction. For instance, an Australian business would not charge VAT for customers outside of the country, yet it would charge VAT for all Australian customers. Yet, VAT might be returned by 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.

How are tax numbers validated?

🇪🇺 Europe

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

United States EIN (U.S. Employer Identification Number) are validated against EIN format rules.

🇨🇦 Canada

Canada BN (Business Number) are validated against BN format rules.

🏴 Rest of the world

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.

More Repositories

1

sonic

🦔 Fast, lightweight & schema-less search backend. An alternative to Elasticsearch that runs on a few MBs of RAM.
Rust
19,769
star
2

vigil

🚦 Microservices Status Page. Monitors a distributed infrastructure and sends alerts (Slack, SMS, etc.).
Rust
1,690
star
3

bloom

🌸 HTTP REST API caching middleware, to be used between load balancers and REST API workers.
Rust
711
star
4

node-sonic-channel

🦉 Sonic Channel integration for Node. Used in pair with Sonic, the fast, lightweight and schema-less search backend.
JavaScript
145
star
5

raider

🐎 Affiliates dashboard. Used by affiliates to generate tracking codes and review their balance.
Rust
144
star
6

constellation

🌌 Pluggable authoritative DNS server. Entries can be added & removed from an HTTP REST API.
Rust
127
star
7

node-fast-ratelimit

☔ Fast and efficient in-memory rate-limit for Node, used to alleviate most common DOS attacks.
JavaScript
107
star
8

jquery.clipboard

✂️ jQuery Clipboard plugin (newest version) - Copy any text to the user's clipboard. Implements ZeroClipboard over the jQuery plugin layer.
JavaScript
70
star
9

giggle

📞 Giggle Jingle library for XMPP, implementation of XEP-0166.
JavaScript
57
star
10

vigil-local

🕯 Vigil Local daemon. Used as a slave service to monitor hosts behind a firewall and report their status to Vigil.
Rust
21
star
11

boulder-dash

:godmode: Boulder Dash game remake, done in Java.
Java
14
star
12

django-gitlab-logging

🍷 A logging handler for Django that opens GitLab issues on server error.
Python
13
star
13

plotters-conrod

📈 Conrod backend for Plotters. This is more efficient than using the default Bitmap backend when plotting in Conrod.
Rust
13
star
14

callisto

💫 Yet another Solar System simulator, written in Go.
Go
11
star
15

go-vigil-reporter

🚧 Vigil Reporter for Golang. Used in pair with Vigil, the Microservices Status Page.
Go
11
star
16

node-vigil-reporter

🚧 Vigil Reporter for Node. Used in pair with Vigil, the Microservices Status Page.
JavaScript
10
star
17

gulp-remove-logging

🚿 Removes console logging statements.
JavaScript
8
star
18

rs-vigil-reporter

🚧 Vigil Reporter for Rust. Used in pair with Vigil, the Microservices Status Page.
Rust
7
star
19

progressio

🎈 Beautiful & stylish asynchronous page loader. Makes a static website dynamic in a breeze.
JavaScript
7
star
20

node-gitlab-logging

🍺 A logging handler for NodeJS that opens GitLab issues on provided exception.
JavaScript
7
star
21

node-bloom-control

💐 Bloom Control integration for Node. Used in pair with Bloom, the HTTP REST API caching middleware.
JavaScript
7
star
22

grunt-blurred-images

🔮 Produce blurred versions of images. Used to reproduce Medium blur-on-scroll effect.
JavaScript
5
star
23

django-request-mock

🙈 Create a Django request object that mocks a real one. Useful in case a real request object is not available, but is needed (delayed Celery tasks for instance)
Python
5
star
24

waaave-bootstrap

🏄 The Waaave Bootstrap. Efficient by design.
JavaScript
4
star
25

waaave-web

🏄 Waaave, The Developer Sharing Network. Tutorials, Shots and more.
Python
4
star
26

lab-iot-homekit

💡 HomeKit-powered home automation IoT projects, running on ESP32.
C
3
star
27

node-spamassassin-client

🔪 SpamAssassin client for Node. Lets you check if an email is spam or ham.
JavaScript
3
star
28

grunt-contrib-lualint

💊 Grunt task for validating Lua code.
Lua
2
star
29

server-workflow-scripts

🐹 Server workflow scripts for fast project deployment and execution - used with valeriansaliou/gitlab-deploy-hooks
Shell
2
star
30

datastore.js

🐘 A complete Web storage wrapper (sessionStorage/localStorage). Provides a fallback when not supported.
JavaScript
2
star
31

jquery.hasparent

💡 jQuery hasParent helper. Checks if the selected element has a defined parent element.
JavaScript
2
star
32

medius

🍊 Learn how the immune system works with Medius, a real-time fight game.
Python
2
star
33

gulp-jade-client

🐰 Compiles Jade templates from the browser.
JavaScript
1
star
34

waaave-hitcount

🏄 Basic app that allows you to track the number of hits/views for a particular object.
Python
1
star
35

valeriansaliou

👱🏻‍♂️ My GitHub profile.
1
star
36

backlinks-manager

🎃 BackLinks.com ads manager. Easily deploy a BackLinks.com ad code, with a fast cache system.
PHP
1
star
37

lab-eigenfaces

💡 Face recognition algorithm implementation, using the eigenfaces technique.
MATLAB
1
star
38

grunt-contrib-rubylint

💊 Grunt task for validating Ruby code.
JavaScript
1
star
39

dns-deploy-utilities

🐬 Utilities to deploy DNS configurations for the BIND9 nameserver.
1
star