{
  "openapi": "3.0.3",
  "info": {
    "title": "EU VAT API",
    "version": "1.0.0",
    "description": "EU VAT number validation (per-country format pre-check for EU-27 + XI, live VIES check with automatic cached fallback) and current EU VAT rates. Every validation response declares its source: vies-live, cache, or format-only. Rates last reviewed 2026-07."
  },
  "servers": [
    {
      "url": "https://eu-vat-api.pages.dev"
    }
  ],
  "tags": [
    {
      "name": "Validation",
      "description": "VAT number validation (VIES + format)."
    },
    {
      "name": "Rates",
      "description": "Current EU VAT rates."
    },
    {
      "name": "Meta",
      "description": "Health and specification."
    }
  ],
  "paths": {
    "/openapi.json": {
      "get": {
        "tags": [
          "Meta"
        ],
        "summary": "OpenAPI specification",
        "responses": {
          "200": {
            "description": "OpenAPI document"
          }
        }
      }
    },
    "/health": {
      "get": {
        "tags": [
          "Meta"
        ],
        "summary": "Health check",
        "responses": {
          "200": {
            "description": "Service status",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Health"
                }
              }
            }
          }
        }
      }
    },
    "/validate": {
      "get": {
        "tags": [
          "Validation"
        ],
        "summary": "Validate one EU VAT number",
        "description": "Format is checked first (invalid formats never hit VIES and return valid:false, reason:'format'). Valid formats are checked live against VIES; if VIES is down, the last known VIES answer is served from cache (source:'cache'). If both are unavailable you get 503 vies_unavailable with formatValid still reported.",
        "security": [
          {
            "ApiKeyHeader": []
          },
          {
            "BearerAuth": []
          },
          {
            "QueryApiKey": []
          }
        ],
        "parameters": [
          {
            "name": "vat",
            "in": "query",
            "required": true,
            "schema": {
              "type": "string",
              "example": "DE811907980"
            },
            "description": "Full VAT number: two-letter country code + national number. Spaces, dots and hyphens are ignored; GR is accepted for Greece (EL)."
          }
        ],
        "responses": {
          "200": {
            "description": "Validation result",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ValidationResult"
                }
              }
            }
          },
          "400": {
            "description": "Missing or malformed `vat` parameter",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "401": {
            "description": "Missing or invalid API key",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "503": {
            "description": "VIES unavailable and no cached result",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/rates": {
      "get": {
        "tags": [
          "Rates"
        ],
        "summary": "Current VAT rates (one country or all)",
        "security": [
          {
            "ApiKeyHeader": []
          },
          {
            "BearerAuth": []
          },
          {
            "QueryApiKey": []
          }
        ],
        "parameters": [
          {
            "name": "country",
            "in": "query",
            "required": false,
            "schema": {
              "type": "string",
              "example": "DE"
            },
            "description": "Two-letter country code (EU-27, EL or GR for Greece, GB/UK for reference). Omit for all countries."
          }
        ],
        "responses": {
          "200": {
            "description": "VAT rates with source citation and lastReviewed stamp",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/RatesResponse"
                }
              }
            }
          },
          "401": {
            "description": "Missing or invalid API key",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "Unknown country code",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/batch": {
      "post": {
        "tags": [
          "Validation"
        ],
        "summary": "Validate up to 50 VAT numbers in one call",
        "security": [
          {
            "ApiKeyHeader": []
          },
          {
            "BearerAuth": []
          },
          {
            "QueryApiKey": []
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "vats"
                ],
                "properties": {
                  "vats": {
                    "type": "array",
                    "maxItems": 50,
                    "items": {
                      "type": "string"
                    },
                    "example": [
                      "DE811907980",
                      "FR40303265045"
                    ]
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Per-item validation results",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/BatchResponse"
                }
              }
            }
          },
          "400": {
            "description": "Invalid body or more than 50 numbers",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "401": {
            "description": "Missing or invalid API key",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "ApiKeyHeader": {
        "type": "apiKey",
        "in": "header",
        "name": "X-API-Key"
      },
      "BearerAuth": {
        "type": "http",
        "scheme": "bearer"
      },
      "QueryApiKey": {
        "type": "apiKey",
        "in": "query",
        "name": "api_key"
      }
    },
    "schemas": {
      "Error": {
        "type": "object",
        "properties": {
          "error": {
            "type": "object",
            "properties": {
              "code": {
                "type": "string",
                "example": "vies_unavailable"
              },
              "message": {
                "type": "string"
              }
            }
          },
          "formatValid": {
            "type": "boolean",
            "description": "Present on 503 vies_unavailable."
          }
        }
      },
      "Health": {
        "type": "object",
        "properties": {
          "status": {
            "type": "string",
            "example": "ok"
          },
          "version": {
            "type": "string",
            "example": "1.0.0"
          },
          "lastReviewed": {
            "type": "string",
            "example": "2026-07"
          }
        }
      },
      "ValidationResult": {
        "type": "object",
        "properties": {
          "vat": {
            "type": "string",
            "example": "DE811907980"
          },
          "countryCode": {
            "type": "string",
            "example": "DE"
          },
          "vatNumber": {
            "type": "string",
            "example": "811907980"
          },
          "valid": {
            "type": "boolean"
          },
          "formatValid": {
            "type": "boolean"
          },
          "reason": {
            "type": "string",
            "example": "format",
            "description": "Present when decided without VIES."
          },
          "name": {
            "type": "string",
            "description": "Registered trader name when VIES returns it."
          },
          "address": {
            "type": "string",
            "description": "Registered trader address when VIES returns it."
          },
          "consultationNumber": {
            "type": "string",
            "description": "VIES consultation/request identifier when present."
          },
          "source": {
            "type": "string",
            "enum": [
              "vies-live",
              "cache",
              "format-only"
            ]
          },
          "checkedAt": {
            "type": "string",
            "format": "date-time"
          }
        }
      },
      "CountryRates": {
        "type": "object",
        "properties": {
          "country": {
            "type": "string",
            "example": "Germany"
          },
          "code": {
            "type": "string",
            "example": "DE"
          },
          "eu": {
            "type": "boolean",
            "description": "false for the UK reference entry."
          },
          "standard": {
            "type": "number",
            "example": 19
          },
          "reduced": {
            "type": "array",
            "items": {
              "type": "number"
            },
            "example": [
              7
            ]
          },
          "superReduced": {
            "type": "number"
          },
          "parking": {
            "type": "number"
          }
        }
      },
      "RatesResponse": {
        "type": "object",
        "description": "Single-country lookups return CountryRates fields at the top level instead of `rates`.",
        "properties": {
          "rates": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/CountryRates"
            }
          },
          "lastReviewed": {
            "type": "string",
            "example": "2026-07"
          },
          "source": {
            "type": "string",
            "description": "Citation for the rate values."
          }
        }
      },
      "BatchResponse": {
        "type": "object",
        "properties": {
          "results": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/ValidationResult"
            },
            "description": "One entry per input, same order. Items whose VIES check failed with no cache carry an `error` object instead of a validity verdict."
          }
        }
      }
    }
  }
}