Home / Use cases / Vercel Edge

Vercel Edge

EU VAT validation on Vercel Edge

Add EU VAT validation to your Next.js app using Vercel Edge Functions. The edge runtime runs your validation globally with sub-50ms cold starts and built-in Next.js caching via next: { revalidate }.

Vercel Edge Functions run on the V8 isolate runtime in Vercel's global network, executing your Next.js API routes at the edge closest to the end user. Declaring export const runtime = 'edge' on an API route opts it into this runtime, which has sub-50ms cold starts and no Node.js-specific APIs (no fs, no child_process) — but it has full access to the global fetch, which is all that is needed to call the TaxID REST API.

Next.js's built-in data cache understands the next: { revalidate } option in fetch calls, so passing next: { revalidate: 3600 } to the TaxID API request caches the response at the Vercel edge for one hour. For active VAT numbers, you can extend this to 86400 (24 hours) to match TaxID's own cache window. The cached: true field in the TaxID response tells you whether TaxID itself served the result from cache, providing two layers of caching transparency.

Vercel Edge Functions compose naturally with Next.js Middleware, allowing you to intercept requests and validate VAT numbers before they reach your page handlers. However, for checkout flows where VAT validation is user-initiated, an API route with export const runtime = 'edge' called from a React component is the more appropriate pattern — it keeps the validation result in the component's state and allows for the optimistic UI patterns described in the React use case.

Implementation steps

  1. 1

    Create an API route with export const runtime = 'edge'

    Create app/api/validate-vat/route.ts and export export const runtime = 'edge' alongside the GET handler. The edge runtime restricts you to Web API-compatible code (no Node.js built-ins), but fetch, URL, Request, and Response are all available. Destructure country and vat from the request URL using new URL(request.url).searchParams to parse the incoming query parameters safely.

  2. 2

    Store TAXID_API_KEY in your Vercel environment variables

    Add TAXID_API_KEY to your Vercel project's environment variables via the Vercel dashboard (Settings > Environment Variables) for Production, Preview, and Development environments. Access it in the edge function with process.env.TAXID_API_KEY — Vercel injects environment variables into the edge runtime the same way as the Node.js runtime. Never use next.config.js env for secrets, as those values are bundled into the client-side JavaScript.

  3. 3

    Call the TaxID API with next: { revalidate: 3600 } for edge caching

    Inside your GET handler, call: const res = await fetch(`https://api.taxid.pro/v1/validate/${country}/${vat}`, { headers: { Authorization: `Bearer ${process.env.TAXID_API_KEY}` }, next: { revalidate: 86400 } }). The next.revalidate: 86400 option instructs Next.js's data cache to store this response for 24 hours at the edge, matching TaxID's cache window for active numbers and eliminating redundant origin requests for frequently-queried VAT numbers.

  4. 4

    Return NextResponse.json to your React frontend or webhook handler

    Parse the TaxID response with const data = await res.json() and return NextResponse.json(data, { headers: { 'Cache-Control': data.status === 'active' ? 'public, max-age=86400' : 'no-store' } }). Setting Cache-Control on the edge function response tells Vercel's CDN layer how long to serve the cached response to subsequent requests for the same country+vat combination, providing a second caching layer on top of Next.js's data cache.

Code example

Vercel Edge

// app/api/validate-vat/route.ts
// Runs at the Vercel edge — global, sub-50ms cold starts

import { NextRequest, NextResponse } from 'next/server';

export const runtime = 'edge';

export async function GET(request: NextRequest) {
  const { searchParams } = new URL(request.url);
  const country = searchParams.get('country');
  const vat     = searchParams.get('vat');

  if (!country || !vat) {
    return NextResponse.json(
      { error: 'country and vat params required' },
      { status: 400 }
    );
  }

  const res = await fetch(
    `http://localhost:3000/api/v1/validate/${country}/${vat}`,
    {
      headers: { 'Authorization': `Bearer ${process.env.TAXID_API_KEY}` },
      next: { revalidate: 3600 },  // cache validated results for 1 hour
    }
  );

  const result = await res.json();
  return NextResponse.json(result);
}

cURL

# Test locally:
# curl "http://localhost:3000/api/validate-vat?country=DE&vat=DE123456789"

# Or call the TaxID API directly:
curl "http://localhost:3000/api/v1/validate/DE/DE123456789" \
  -H "Authorization: Bearer $TAXID_API_KEY"

# {
#   "valid": true,
#   "status": "active",
#   "company_name": "Example GmbH",
#   "cached": false
# }

API response

The TaxID API returns a consistent JSON response for every validation request:

200 OK (active)valid
{
  "valid": true,
  "status": "active",
  "country_code": "DE",
  "vat_number": "123456789",
  "company_name": "Example GmbH",
  "company_address": "Musterstraße 1, 10115 Berlin",
  "request_date": "2026-05-10T00:00:00.000Z",
  "cached": false,
  "request_id": "req_01j..."
}

Error handling

The API uses a consistent Stripe-style error format. Always handle service_unavailable separately — VIES has occasional downtime and you should not reject valid customers during outages.

active

VAT number is valid and the business is registered

invalid

VAT number format is wrong or not registered in VIES

service_unavailable

VIES or the national system is temporarily down — retry later

Further reading

Evaluating EU VAT APIs? Compare TaxID with:

Ready to implement?

Get your free API key and start validating EU VAT numbers today.

Get free API key

Related use cases

Stripe EU VAT: Validate Tax IDs Before Charging Customers

Stripe EU VAT integration guide: validate EU VAT numbers server-side before applying zero-rate B2B e...

UK VAT validation in Shopify B2B

Validate UK VAT numbers for B2B customers in Shopify. Required under UK Making Tax Digital rules for...

WooCommerce Spain NIF/CIF validation

Validate Spanish NIF and CIF numbers in WooCommerce checkout. Automatically apply B2B tax exemptions...