Code Examples
PHP (cURL)
Validate VAT Number
Find below a simple way of using PHP (cURL) for validating a EU VAT number:
// set API Endpoint and Access Key
$endpoint = 'validate';
$access_key = 'YOUR_ACCESS_KEY';
// set VAT number
$vat_number = 'LU26375245';
// Initialize CURL:
$ch = curl_init('https://api.apilayer.net/vatlayer/'.$endpoint.'?access_key='.$access_key.'&vat_number='.$vat_number.'');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Store the data:
$json = curl_exec($ch);
curl_close($ch);
// Decode JSON response:
$validationResult = json_decode($json, true);
// Access and use your preferred validation result objects
$validationResult['valid'];
$validationResult['query'];
$validationResult['company_name'];
$validationResult['company_address'];JavaScript (jQuery.ajax)
Validate VAT Number
Find below a simple way of using JavaScript (jQuery.ajax) for validating a EU VAT number:
// set endpoint and your access key
var endpoint = 'validate'
var access_key = 'YOUR_ACCESS_KEY';
var vat_number = 'LU26375245';
// validate VAT number via AJAX call
$.ajax({
url: 'https://api.apilayer.net/vatlayer/' + endpoint + '?access_key=' + access_key + '&vat_number=' + vat_number,
dataType: 'jsonp',
success: function(json) {
// Access and use your preferred validation result objects
alert(json.valid);
alert(json.query);
alert(json.company_name);
alert(json.company_address);
}
});Please note: In this example, the use of JSONP is entirely optional.