How to determine if a tax ID is valid and get business details
Here is a TypeScript
code example that shows how to make a request to the API to determine if a tax ID is valid and to retrieve business details. This example includes error handling and the use of environment variables for the API key.
Before using your API key, we recommend storing it in a .env
file to avoid exposing it in the source code.
VERIFYVAT_SECRET_KEY=sk_demo_00000000000000000000000000000000
You can use any HTTP request library. In this example, we use fetch
, which is available in both Node.js and modern browsers.
/** * Verify if a tax ID is valid and retrieve business details * @param taxId The tax ID to verify * @return business details if the tax ID is valid, false if not, null in case of error */export async function getTaxIdDetails(taxId: string) { // Change this to the appropriate tax type const taxType = 'auto' // Sanitize the tax ID const sanitizedTaxId = taxId.replace(/[^a-zA-Z0-9]/g, '') try { const endpoint = `https://verifyvat.com/api/${taxType}/${sanitizedTaxId}` const query = await fetch(endpoint, { method: 'POST', headers: { 'Content-Type': 'application/json', 'x-api-key': process.env.VERIFYVAT_SECRET_KEY!, }, })
if (!query.ok) { // If we encounter an error at this stage, // it means there is a connection issue with the API throw new Error('Network response was not ok') }
const response = await query.json() if (response.error) { // If the API returns an error, handle it here // For example, we might want to check the error code // and provide a more detailed message throw new Error(response.message) } if (!response.data) { // If the response does not contain data, even if the request was successful // it means there has been an issue with our request throw new Error('Unexpected error occured during the request') }
if (!response.data.isValid) { // The tax ID is not valid return false }
// The tax ID is valid, return the business details const { businessName = '', fullAddress = '', countryCode = '' } = response.data return { businessName, fullAddress, countryCode, } } catch (error) { console.error('Error fetching tax ID data:', error) return null }}
We can now use this function in our main code to verify a tax ID dynamically, displaying a different message depending on the status.
import { getTaxIdDetails } from './api'
export async function main() { // Change this to the tax ID you want to verify const taxId = 'NO 995 525 828 MVA' const taxIdDetails = await getTaxIdDetails(taxId) if (taxIdDetails) { console.log(`Tax ID "${taxId}" is valid.`) console.log(`Business Name: ${taxIdDetails.businessName}`) console.log(`Full Address: ${taxIdDetails.fullAddress}`) console.log(`Country Code: ${taxIdDetails.countryCode}`) } else if (taxIdDetails === false) { console.log(`Tax ID "${taxId}" is not valid.`) } else { console.log(`Could not retrieve tax ID status for "${taxId}".`) }}main()
To explore more features of the API, check the full documentation, or visit the playground to test API calls interactively.
For reference, here is the shape of the response returned by the API as a TypeScript
type using zod
for validation:
import { z } from 'zod'
export const ResponseDataShape = z.object({ id: z.string(), type: z.string(), formattedId: z.string(), typeName: z.string(), isValid: z.boolean(), isVerified: z.boolean(), isPatternOnly: z.boolean(), isVerifiable: z.boolean(), manualDataSource: z.string().optional(), dataSource: z.string().optional(), firstName: z.string().optional(), lastName: z.string().optional(), fullName: z.string().optional(), birthDate: z.string().optional(), gender: z.string().optional(), birthPlace: z.string().optional(), birthCountryCode: z.string().optional(), businessName: z.string().optional(), alternativeName: z.string().optional(), businessType: z.string().optional(), businessStatus: z.string().optional(), registrationDate: z.string().optional(), dissolutionDate: z.string().optional(), isVatRegistered: z.boolean().optional(), address: z.string().optional(), postalCode: z.string().optional(), town: z.string().optional(), region: z.string().optional(), countryCode: z.string().optional(), fullAddress: z.string().optional(), alternativeIdentifiers: z .array( z.object({ country: z.string(), type: z.string().optional(), id: z.string(), }), ) .optional(),})
export const ResponseShape = z.object({ error: z.boolean(), message: z.string(), status: z.number(), data: ResponseDataShape.nullable(), timestamp: z.number(), eventId: z.string().nullable(), code: z.string().nullable(), overQuota: z.boolean(),})
export type VerifyVATData = z.infer<typeof ResponseDataShape>export type VerifyVATResponse = z.infer<typeof ResponseShape>