EU VAT numbers and a VIES check client
README.md

vies #

The VAT identification numbers of the EU member states and Northern Ireland,
and a client of VIES, the European Commission's VAT Information Exchange
System.

An invoice to a business in another member state carries the buyer's VAT
number, and the seller checks it against VIES. Vies.Vat_number checks a
number's form and check digit locally. Vies.check asks VIES's REST service
whether the number is registered, and returns the registered name and address
when the state gives them. A request that names the requester's own VAT number
is an approximate match request, and its answer carries a consultation number:
the proof an auditor accepts that the check was made. When VIES or the state
cannot answer, the result is an error that Vies.unavailable recognises, never
an invalid number.

Contents #

Installation #

$ opam install vies

The library is vies and its module is Vies; the command is vies.

Usage #

A VAT number is checked locally for its state's form and check digit:

# let n = Result.get_ok (Vies.Vat_number.of_string "DE 136 695 976");;
val n : Vies.Vat_number.t = <abstr>
# Vies.Vat_number.to_string n;;
- : string = "DE136695976"
# Vies.Vat_number.of_string "DE136695977"
  |> Result.iter_error (Fmt.pr "%a@." Vies.Vat_number.pp_error);;
"136695977" fails the check digit of a DE VAT number
- : unit = ()

A request names the number and, for a consultation number, the requester's own
VAT number:

# let requester = Result.get_ok (Vies.Vat_number.of_string "ATU13585627");;
val requester : Vies.Vat_number.t = <abstr>
# Vies.json_of_request (Vies.request ~requester n);;
- : string =
"{\"countryCode\":\"DE\",\"vatNumber\":\"136695976\",\"requesterMemberStateCode\":\"AT\",\"requesterNumber\":\"U13585627\"}"

Vies.check sends it over a Requests_eio.t session:

# let ask n =
    Eio_main.run @@ fun env ->
    Eio.Switch.run @@ fun sw ->
    let session =
      Requests_eio.v ~sw ~mono:env#mono_clock ~clock:env#clock env#net
    in
    match Vies.check (Vies.config session) (Vies.request ~requester n) with
    | Ok answer -> Fmt.pr "%a@." Vies.pp_answer answer
    | Error e when Vies.unavailable e ->
        Fmt.pr "ask again later: %a@." Vies.pp_error e
    | Error e -> Fmt.pr "%a@." Vies.pp_error e;;
val ask : Vies.Vat_number.t -> unit = <fun>

Vies.check_test_service asks VIES's test service instead, which answers by
the national number: 100 is valid, 200 is invalid, and 201 to 601 are
each of VIES's errors.

The command #

$ vies syntax DE136695976
DE136695976
$ vies check --requester ATU13585627 DE136695976
$ vies check --test DE100
$ vies status

vies check exits 0 when VIES has the number registered, 1 when it does not
or the number is malformed, 3 when VIES or the state could not answer and 4
when VIES refused the question.

What it covers #

  • The national form of each state's VAT number, as the Commission's VIES
    documentation tabulates it, and the state's check digit; the Vat_number
    interface lists each rule. A Latvian person's number, a Czech 9-digit
    number and a French number whose key has a letter have no check here.
  • check-vat-number and check-vat-test-service, with the trader details
    of an approximate match request and each detail's match, and
    check-status, VIES's view of each state's service.
  • Each VIES error code is a constructor of Vies.error, whatever HTTP status
    carries it, and so is an answer whose userError is one.

License #

ISC. See LICENSE.md.