How to Validate a VAT Number using Java

Vat numbers can be invalid for several reasons — most commonly due to improper formatting. Whatever the case, it’s important to ensure that VAT codes are entered correctly into any application or database, or you’ll have bigger problems to deal with down the line.

Thankfully, you can easily validate your VAT numbers through our VAT Validation API. The API response will provide a Boolean for the value IsValid, and can further identify BusinessName, BusinessAddress, and much more (only if this additional information is made available via the VAT code). The below example response shows a full list of possible values:

{
"CountryCode": "string",
"VatNumber": "string",
"IsValid": true,
"BusinessName": "string",
"BusinessAddress": "string",
"BusinessBuilding": "string",
"BusinessStreetNumber": "string",
"BusinessStreet": "string",
"BusinessCity": "string",
"BusinessStateOrProvince": "string",
"BusinessPostalCode": "string",
"BusinessCountry": "string"
}

You can easily take advantage of this API for free — all you need to do is register a free account on our website, which will provide you with an API key good for 800 API calls per month (with zero additional financial commitments; perfect for small-scale projects and startups).

First, we need to install the API client. Let’s add a reference to the repository in pom.xml:

<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

Next, let’s add a reference to the dependency in pom.xml:

<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v4.25</version>
</dependency>
</dependencies>

With installation complete, let’s shift focus over to the controller and add the following imports to the top of our file:

// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.ApiException;
//import com.cloudmersive.client.invoker.Configuration;
//import com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.VatApi;

Last but not least, let’s call the function & authenticate our API key on the fourth line down (indicated in the code comments):

ApiClient defaultClient = Configuration.getDefaultApiClient();// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//Apikey.setApiKeyPrefix("Token");
VatApi apiInstance = new VatApi();
VatLookupRequest input = new VatLookupRequest(); // VatLookupRequest | Input VAT code
try {
VatLookupResponse result = apiInstance.vatVatLookup(input);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling VatApi#vatVatLookup");
e.printStackTrace();
}

We’ll want to pass our VAT code through the function in the following format:

{
"VatCode": "string"
}

And that’s it — no more code required. Easy as can be!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Cloudmersive

Cloudmersive

437 Followers

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.