Authentication overview
VATlayer secures its API endpoints through a straightforward API key authentication mechanism. This method grants access to the VAT validation and rate lookup services by requiring a unique access_key with each API request. The API key functions as a secret token, identifying the calling application and authorizing its access to the requested resources. This approach is common for public APIs that prioritize ease of integration while maintaining a level of access control for usage tracking and security as described in API key management practices.
When an API request is made to VATlayer, the system validates the provided access_key. If the key is valid and associated with an active subscription, the API processes the request. Invalid or missing keys result in an authentication error, preventing access to the service. This system supports various operations, including validating VAT numbers, looking up VAT rates by country, and checking the validity of numbers against official EU VIES data.
The API is designed to be RESTful, sending requests over HTTP/HTTPS, and returning data in JSON format. All interactions with the API, from basic validation to more complex queries, necessitate the inclusion of the API key for successful operation. This ensures that all usage can be attributed to a specific account, facilitating accurate billing and usage monitoring as outlined in the VATlayer API documentation.
Supported authentication methods
VATlayer exclusively employs API key authentication for accessing its services. This method involves generating a unique alphanumeric string (the API key) that developers include directly in their API requests. This approach is widely adopted for its simplicity and effectiveness in controlling API access, as noted in general API security guidelines published by Google Cloud.
API Key Authentication
- Method Description: API key authentication requires an
access_keyto be passed as a query parameter in every API request. This key is unique to each user's account and acts as a credential for accessing the VATlayer API. - How it Works: When an application sends a request to a VATlayer endpoint, it appends the
access_keyto the URL. The VATlayer server then verifies this key against its records to confirm the user's identity and subscription status before processing the request. - Example Parameter:
?access_key=YOUR_ACCESS_KEY - Benefits: Simplicity of integration, easy to manage for individual users, and effective for rate limiting and usage tracking.
- Considerations: API keys should be treated as sensitive credentials and protected from unauthorized exposure.
The following table summarizes the authentication method supported by VATlayer:
| Method | When to Use | Security Level |
|---|---|---|
| API Key (Query Parameter) | Standard use for all API calls to VATlayer; suitable for server-side applications and controlled client-side environments where the key can be securely stored. | Moderate (requires secure handling of the key; vulnerable if exposed in client-side code without proper precautions). |
Getting your credentials
To obtain your VATlayer API key, you must first register for an account on the official VATlayer website. The process typically involves a few steps, leading to the generation of a unique access_key tied to your account.
- Sign Up/Log In: Navigate to the VATlayer homepage and either sign up for a new account or log in if you already have one. New accounts typically start with a free tier subscription, offering 100 API requests per month, as noted on their pricing page.
- Access Dashboard: After successful login, you will be directed to your personal dashboard. This is the central hub for managing your subscription, viewing usage statistics, and accessing your API key.
- Locate API Key: Within the dashboard, there will be a dedicated section, often labeled 'API Key' or 'Your API Access Key', where your unique key is displayed. This key is a long alphanumeric string that you will use to authenticate all your API requests.
- Copy and Store: Copy your API key and store it securely. It is crucial to treat this key as sensitive information, similar to a password.
VATlayer provides comprehensive documentation that includes detailed instructions for finding and using your API key within various programming languages and environments, such as PHP, Python, jQuery, Go, and Ruby.
Authenticated request example
This section provides examples of how to make an authenticated request to the VATlayer API using your obtained access_key. The key must be included as a query parameter named access_key in the request URL. These examples demonstrate validating a VAT number for a specific country.
Example: Validate a VAT Number
To validate a VAT number, you would typically use the /validate endpoint, providing the VAT number and, optionally, the country code.
Using cURL (Command Line)
The following cURL command demonstrates a basic GET request to validate a German VAT number. Replace YOUR_ACCESS_KEY with your actual VATlayer API key and DE123456789 with the VAT number you wish to validate.
curl "http://api.vatlayer.com/validate?access_key=YOUR_ACCESS_KEY&vat_number=DE123456789"
Using Python
This Python example uses the requests library to make an authenticated GET request. Ensure you have the requests library installed (pip install requests).
import requests
api_key = "YOUR_ACCESS_KEY"
vat_number = "DE123456789"
url = f"http://api.vatlayer.com/validate?access_key={api_key}&vat_number={vat_number}"
try:
response = requests.get(url)
response.raise_for_status() # Raise an exception for HTTP errors (4xx or 5xx)
data = response.json()
print(data)
except requests.exceptions.RequestException as e:
print(f"An error occurred: {e}")
Using PHP
This PHP example uses file_get_contents for simplicity, though for production environments, cURL is often preferred for more robust error handling and flexibility.
<?php
$apiKey = "YOUR_ACCESS_KEY";
$vatNumber = "DE123456789";
$url = "http://api.vatlayer.com/validate?access_key=" . $apiKey . "&vat_number=" . $vatNumber;
$response = @file_get_contents($url);
if ($response === FALSE) {
echo "Error fetching data.\n";
} else {
$data = json_decode($response, true);
print_r($data);
}
?>
In all examples, a successful response will return a JSON object containing validation results, including whether the VAT number is valid, the company name, and address information if available. An error response will typically include an error object with a code and type indicating the issue, such as an invalid or missing API key.
Security best practices
Securing your VATlayer API key is critical to prevent unauthorized access to your account and potential misuse, which could lead to exceeding your request limits or unauthorized data access. Implementing robust security practices is essential for any application that interacts with external APIs as recommended by Twilio's security guidelines.
1. Protect Your API Key
- Do not hardcode keys in client-side code: Never embed your API key directly into client-side code (e.g., JavaScript in a web browser). This makes the key easily discoverable by anyone inspecting your web page's source code.
- Use environment variables: For server-side applications, store your API key as an environment variable rather than directly in your codebase. This prevents the key from being committed to version control systems like Git.
- Configuration files (securely): If using configuration files, ensure they are external to your public repository and have restricted access permissions.
- Avoid public repositories: Do not commit your API key to any public code repositories (e.g., GitHub). Use
.gitignoreor similar mechanisms to exclude configuration files containing keys.
2. Transmit Securely (HTTPS)
- Always use HTTPS: Ensure all API requests to VATlayer are made over HTTPS. While VATlayer's documentation implies HTTP requests are possible, HTTPS encrypts the communication channel, protecting your API key and other sensitive data from interception during transit. This is a fundamental web security practice explained by MDN Web Docs.
3. Implement Server-Side Calls
- Proxy requests: If your client-side application needs to access VATlayer, route the requests through your own secure backend server. Your server can then append the API key before forwarding the request to VATlayer, and then relay the response back to the client. This keeps the API key hidden from the end-user's browser.
4. Monitor Usage
- Regularly check your dashboard: Periodically review your API usage statistics in the VATlayer dashboard. Unusual spikes in activity could indicate unauthorized use of your API key.
- Set up alerts: If VATlayer offers usage alerts, configure them to notify you if your request volume approaches your subscription limits.
5. Key Rotation and Revocation
- Rotate keys periodically: Although VATlayer may not directly support API key rotation from the dashboard, consider generating a new key and updating your applications periodically, especially if there's any suspicion of compromise.
- Revoke compromised keys: If you believe your API key has been compromised, immediately log into your VATlayer dashboard and look for options to regenerate or revoke the key. This will invalidate the old key, preventing further unauthorized use.
6. Least Privilege Principle
- Use dedicated keys (if available): While VATlayer's API key is generally a single access credential, if an API offered different types of keys with varying permissions, you would always use the key with the minimum necessary privileges for a given task.
By adhering to these security best practices, developers can significantly reduce the risk associated with using API keys and maintain the integrity and security of their applications when integrating with the VATlayer API.