惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
博客园 - 司徒正美
I
InfoQ
宝玉的分享
宝玉的分享
G
Google Developers Blog
J
Java Code Geeks
Martin Fowler
Martin Fowler
The GitHub Blog
The GitHub Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
罗磊的独立博客
腾讯CDC
F
Fortinet All Blogs
A
About on SuperTechFans
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Recent Announcements
Recent Announcements
Last Week in AI
Last Week in AI
B
Blog RSS Feed
博客园 - 聂微东
D
DataBreaches.Net
Hugging Face - Blog
Hugging Face - Blog
The Cloudflare Blog
L
LangChain Blog
Microsoft Azure Blog
Microsoft Azure Blog
aimingoo的专栏
aimingoo的专栏

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
How to validate UK VAT numbers in Node.js (post-Brexit ed...
BitOwl · 2026-06-14 · via DEV Community

UK VAT numbers used to be validated through VIES like every other European country. After Brexit they moved to HMRC's own API, which is well-designed for what it does, but comes with some integration overhead that's worth knowing about upfront.

I had customers all over the world, storing thousands of VAT numbers in our SaaS solution, and sometimes a mess, manually managed. That brought me to finding a better way to validate them.

Here's a clean way to handle UK VAT validation, and how to extend it to cover EU and beyond with the same integration.

The HMRC API is solid, but not lightweight

HMRC's VAT validation endpoint lives inside their MTD ("Making Tax Digital") API platform. It's a proper, well-documented API. But it's built for accountancy software integrations, not quick checkout validations. Using it directly means:

  • Registering a developer account and creating an application
  • Implementing OAuth client credentials flow and handling token refresh
  • Going through HMRC's production credentials approval process before going live

The endpoint itself (GET /organisations/vat/check-vat-number/lookup/{targetVrn}) works well, once it's set up... The overhead is just higher than you might expect for a single-country number check.

The other consideration: if you're selling into EU countries as well, you'd need a separate VIES integration alongside it. Two auth systems, two error models, or more.

One endpoint for UK, EU, and more

I use TaxVett for this. It covers UK (HMRC), all 27 EU member states (VIES), Norway, Australia, and Singapore — one REST endpoint, one API key, consistent response format across all of them. For a checkout that sells into multiple regions, that's the main reason to reach for it.

For UK specifically, you just pass the number:

curl -X POST https://api.taxvett.com/v1/validate \
  -H "X-Api-Key: YOUR_KEY" \
  -H "Content-Type: application/json" \
  -d '{"number": "GB123456789"}'

UK numbers use GB prefix, auto-detected, no country field needed.

Full integration example

Here's a TypeScript module I for a Next.js API route:

// lib/vat.ts

interface ValidationResult {
  valid: boolean;
  companyName?: string;
  companyAddress?: string;
}

export async function validateUKVat(
  vatNumber: string
): Promise<ValidationResult> {
  // Normalise: strip spaces and ensure GB prefix
  const normalised = vatNumber.replace(/\s/g, "").toUpperCase();
  const withPrefix = normalised.startsWith("GB")
    ? normalised
    : `GB${normalised}`;

  const res = await fetch("https://api.taxvett.com/v1/validate", {
    method: "POST",
    headers: {
      "X-Api-Key": process.env.TAXVETT_API_KEY!,
      "Content-Type": "application/json",
    },
    body: JSON.stringify({ number: withPrefix, country: "GB" }),
  });

  if (res.status === 422) {
    // Format validation failed before hitting the registry
    return { valid: false };
  }

  if (!res.ok) {
    throw new Error(`TaxVett error: ${res.status}`);
  }

  const data = await res.json();
  return {
    valid: data.valid,
    companyName: data.company_name,
    companyAddress: data.company_address,
  };
}

And in the API route:

// pages/api/checkout/validate-vat.ts

import { validateUKVat } from "@/lib/vat";

export default async function handler(req, res) {
  const { vatNumber } = req.body;

  if (!vatNumber || typeof vatNumber !== "string") {
    return res.status(400).json({ error: "vatNumber required" });
  }

  try {
    const result = await validateUKVat(vatNumber);
    return res.status(200).json(result);
  } catch (err) {
    // Don't block checkout if validation service is down
    // Log the error, return valid: null to signal uncertainty
    console.error("VAT validation error", err);
    return res.status(200).json({ valid: null });
  }
}

Note: The valid: null pattern is intentional! I'd rather complete a sale and flag it for manual review than reject a legitimate customer because of a network timeout.

What format do UK VAT numbers have?

UK VAT numbers are 9 digits, optionally prefixed with GB. Branch traders use a 12-digit variant (GB + 9 digits + 3-digit branch code). A few formats the normalisation above handles:

Input Normalised
123 4567 89 GB123456789
gb123456789 GB123456789
GB 123 4567 89 GB123456789

HMRC's test number GB999999973 works in sandbox environments if you want to check the integration is wired up before going live.

Does this work for EU VAT too?

That same endpoint actually handles all 27 EU member states, Norway, Australia, and Singapore. If you're selling into multiple regions you validate with one integration and get a consistent response shape regardless of which registry handled the lookup. (I think there are minor differences for just one or a few rare countries, have not affected me in practice so far.)

When it makes sense, and when it doesn't

Good fit:

  • You're selling into more than one region
  • You're in early development and want to iterate fast
  • You want a consistent JSON response
  • You don't want to pay to get started

Worth considering:

  • If you only need UK and already have an OAuth setup, hitting HMRC directly is a valid option
  • VAT numbers pass through a third-party API. If your compliance requirements are strict about data routing, check you are ok with TaxVett's privacy policy before integrating
  • At higher volumes the paid plans kick in (Pro €9/month). That's still cheap, but it's not free