VIES API Client for JavaScript

1.3.3 · active · verified Tue Apr 21

The `viesapi-client` is the official JavaScript library for interacting with the VIES API, which provides services for querying the European Union's VAT Information Exchange System. This client enables developers to validate EU VAT numbers, retrieve company details, and check the registration status of businesses across all EU member states. Currently at version 1.3.3, the library is actively maintained with regular updates and ships with full TypeScript type definitions. It supports both Node.js and browser environments, offering built-in authentication, request signing, and comprehensive error handling. Key differentiators include its ability to perform both synchronous single-number lookups and asynchronous batch processing, along with utility functions for local validation of EU VAT and Polish NIP numbers prior to making API requests. The library handles the intricacies of communicating with the VIES system, simplifying the process for developers.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to instantiate the `VIESAPIClient` for both test and production environments, perform local VAT number validation, and then query the VIES API to check the status of an EU VAT number, including basic error handling.

import { VIESAPIClient, EUVAT } from 'viesapi-client';

const API_ID = process.env.VIESAPI_ID ?? '';
const API_KEY = process.env.VIESAPI_KEY ?? '';

async function validateVatNumber(vatNumber: string) {
  let client: VIESAPIClient;

  // Instantiate client for test mode if no credentials are provided
  // The test environment has specific VAT numbers it will respond to successfully.
  if (!API_ID || !API_KEY) {
    console.warn('VIESAPI_ID or VIESAPI_KEY not set. Using test environment.');
    client = new VIESAPIClient(); // Test mode constructor
  } else {
    client = new VIESAPIClient(API_ID, API_KEY); // Production mode
  }

  // Optionally set a custom application identifier for tracking
  client.setApp('MyAwesomeApp/1.0');

  // Local validation first for basic format checks
  if (!EUVAT.isValid(vatNumber)) {
    console.error(`Error: Invalid EU VAT number format for ${vatNumber}.`)
    return;
  }

  try {
    console.log(`Checking VAT number: ${vatNumber}...`);
    const viesData = await client.getVIESData(vatNumber);

    if (viesData.valid) {
      console.log(`VAT number ${vatNumber} is VALID.`);
      console.log(`Company Name: ${viesData.traderName}`);
      console.log(`Address: ${viesData.traderAddress}`);
    } else {
      console.log(`VAT number ${vatNumber} is INVALID or not found.`);
      console.log(`UID: ${viesData.uid}`); // Unique ID of the query
    }
  } catch (error: any) {
    console.error(`Failed to validate VAT number ${vatNumber}:`, error.message);
    if (error.status === 401) {
      console.error('Check your API ID and Key. They might be incorrect or missing for production environment.');
    }
  }
}

// Example usage with a test VAT number for the VIES API test environment
validateVatNumber('PL7171642051');
// Example with an invalid VAT number (will fail local validation)
// validateVatNumber('DE12345');
// Example for a production key (replace with actual values in .env or similar)
// validateVatNumber('FR10402571889');

view raw JSON →