Node.js: how to validate an European VAT number

Node.js: how to validate an European VAT number

In this article we're going to learn how to validate an European VAT number with Node.js.

In this article we're going to learn how to validate an European VAT number with Node.js.

The European Community provides a public service called VIES. This service makes use of SOAP to validate a given VAT number if such number has been inserted in their database. Not all European countries automatically push VAT numbers to the VIES database upon creation, so when you get an error that tells you that the provided number is invalid, it's likely that your number is not present in the EU database.

The relevant API method is called checkVat and it requires two parameters:

  1. countryCode: it must be the two-letters code of a member state of the European Community, e.g. DE for Germany.
  2. vatNumber: the VAT number to be validated.

Our implementation requires the soap package:


npm install soap --save

Then we can write:


'use strict';

const express = require('express');
const bodyParser = require('body-parser');
const port = process.env.PORT || 8080;
const app = express();
const endpoint = 'http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl';
const soap = require('soap');

app.post('/validate', (req, res) => {
   let country = req.body.country;
   let vat = req.body.vat;
   let params = {
       countryCode: country,
       vatNumber: vat
   };
    soap.createClient(endpoint, (err, client) => {
       client.checkVat(params, (err, result) => {
          res.send(result);
       });
    });
});

Our request will return a JSON object whose valid boolean property will contain the result of the validation process.

Complete code

Node.js-European-VAT-validation