Validate EU VAT numbers with FastAPI and auto-generated Swagger docs. Uses dependency injection for clean, testable code.
- Get your API key at eurovalidate.com
- Clone and install
git clone https://github.com/eurovalidate/fastapi-vat-example.git cd fastapi-vat-example python -m venv venv && source venv/bin/activate pip install -r requirements.txt
- Configure environment
cp .env.example .env # Edit .env with your EuroValidate API key - Run
python main.py # Open http://localhost:8000/docs for Swagger UI
- GET /validate?vat_number=NL820646660B01 -- validate a single VAT number
- POST /validate/batch -- validate up to 10 VAT numbers in one request
- Auto-generated Swagger UI at
/docswith example values and response models - Dependency injection keeps the EuroValidate client clean and testable
EuroValidate as a FastAPI dependency:
from eurovalidate import EuroValidate
def get_eurovalidate() -> EuroValidate:
return EuroValidate(api_key=os.environ["EUROVALIDATE_API_KEY"])
EuroValidateClient = Annotated[EuroValidate, Depends(get_eurovalidate)]
@app.get("/validate")
async def validate_vat(
vat_number: str,
client: EuroValidateClient,
):
result = client.vat.validate(vat_number)
return result# Single validation
curl "http://localhost:8000/validate?vat_number=NL820646660B01"
# Batch validation
curl -X POST http://localhost:8000/validate/batch \
-H "Content-Type: application/json" \
-d '{"items": [{"vat_number": "NL820646660B01"}, {"vat_number": "FR40303265045"}]}'{
"success": true,
"data": {
"valid": true,
"vat_number": "NL820646660B01",
"country_code": "NL",
"company_name": "COOLBLUE B.V.",
"company_address": "WEENA 00664 3012CN ROTTERDAM"
},
"meta": {
"confidence": "HIGH",
"source": "vies_live",
"cached": false,
"response_time_ms": 47
},
"request_id": "req_abc123"
}| VAT Number | Country | Company |
|---|---|---|
NL820646660B01 |
Netherlands | Coolblue B.V. |
FR40303265045 |
France | Google France |
MIT