DEV Community

Alexander Nitrovich
Alexander Nitrovich

Posted on • Originally published at blog.eurovalidate.com

Validate EU VAT in Go Fiber

Introduction

Validating EU VAT numbers is crucial for businesses operating across Europe to ensure compliance with tax regulations and streamline transaction verifications. In this guide, we'll explore how to implement EU VAT validation using the Go Fiber framework, integrating with the EuroValidate API. This guide will provide a practical, step-by-step process for developers using Go and Fiber to build scalable, high-performance applications requiring accurate VAT validation.

What is EU VAT Validation?

The Value Added Tax (VAT) system is a consumption tax levied on goods and services in the EU. Each business registered for VAT is assigned a unique VAT number. Validating these numbers is paramount to avoid tax evasion and ensure regulatory compliance. The process, however, can be complex due to data variability across EU countries. Fortunately, APIs like EuroValidate simplify this task by providing reliable validation services.

Why Choose Go Fiber for VAT Validation?

Go Fiber is known for its performance, built on the fast Go language, offering an easy-to-use and expressive API. This makes it ideal for building web applications requiring seamless third-party integrations like VAT validation. Compared to other frameworks, Fiber's low latency and minimal overhead add substantial value to high-performance environments.

Setting Up Your Go Fiber Project

  1. Initialize a New Fiber Project:
   go mod init vat-validator
   go get github.com/gofiber/fiber/v2
Enter fullscreen mode Exit fullscreen mode
  1. Install Dependencies:

For HTTP requests, you’ll need an HTTP client:

   go get github.com/go-resty/resty/v2
Enter fullscreen mode Exit fullscreen mode
  1. Project Structure Tips:

Organize your project by creating directories for routes, handlers, and services to enhance maintainability.

Implementing VAT Validation in Go Fiber

Step 1: Create a VAT Validation Endpoint

The VAT validation involves receiving a VAT number, making an API call to the EuroValidate service, and returning the validation status to the client.

Step 2: Integrate EuroValidate API

To validate a VAT number, send a GET request to /v1/vat/{number}. The following example utilizes the Fiber framework:

package main

import (
    "github.com/gofiber/fiber/v2"
    "net/http"
    "encoding/json"
    "log"
)

type VatResponse struct {
    VatNumber      string  `json:"vat_number"`
    CountryCode    string  `json:"country_code"`
    Status         string  `json:"status"`
    CompanyName    string  `json:"company_name,omitempty"`
    CompanyAddress string  `json:"company_address,omitempty"`
    RequestId      string  `json:"request_id"`
    Meta           struct {
        Confidence float64 `json:"confidence"`
        Source     string  `json:"source"`
        Cached     bool    `json:"cached"`
        ResponseTime int   `json:"response_time_ms"`
    } `json:"meta"`
}

func ValidateVAT(c *fiber.Ctx) error {
    vatNumber := c.Params("vat")
    if vatNumber == "" {
        return c.Status(http.StatusBadRequest).JSON(fiber.Map{"error": "VAT number is required"})
    }
    apiURL := "https://api.eurovalidate.com/v1/vat/" + vatNumber
    // Respective HTTP client call
    resp, err := http.Get(apiURL)
    if err != nil {
        log.Println("API call error:", err)
        return c.Status(http.StatusInternalServerError).JSON(fiber.Map{"error": "API error"})
    }
    defer resp.Body.Close()

    var vatResp VatResponse
    if err := json.NewDecoder(resp.Body).Decode(&vatResp); err != nil {
        return c.Status(http.StatusInternalServerError).JSON(fiber.Map{"error": "response error"})
    }

    return c.JSON(vatResp)
}

func main() {
    app := fiber.New()
    app.Get("/validate-vat/:vat", ValidateVAT)
    log.Fatal(app.Listen(":3000"))
}
Enter fullscreen mode Exit fullscreen mode

Testing and Debugging Your Implementation

To test the endpoint, use tools like Postman or curl:

curl http://localhost:3000/validate-vat/NL820646660B01
Enter fullscreen mode Exit fullscreen mode

Valid Example:

{
  "vat_number": "NL820646660B01",
  "country_code": "NL",
  "status": "valid",
  "company_name": "Example Co.",
  "company_address": "123 Example St, Amsterdam",
  "request_id": "req-123456",
  "meta": {
    "confidence": 0.98,
    "source": "eu-portal",
    "cached": false,
    "response_time_ms": 150
  }
}
Enter fullscreen mode Exit fullscreen mode

Invalid Example:

{
  "vat_number": "FR40303265045",
  "country_code": "FR",
  "status": "invalid",
  "request_id": "req-654321",
  "meta": {
    "confidence": 0.50,
    "source": "eu-portal",
    "cached": true,
    "response_time_ms": 140
  }
}
Enter fullscreen mode Exit fullscreen mode

Debugging Tips

  1. Check API Key: Ensure your API key is correctly set in your requests.
  2. API Latency: Monitor average response times to spot performance bottlenecks.
  3. Error Handling: Verify error responses for clear user guidance.

Conclusion and Next Steps

This guide has shown you how to set up a Go Fiber application for EU VAT validation using the EuroValidate API. Remember to explore additional features of the API, such as batch validations, to further enhance your billing or compliance systems. Visit our documentation for more details.

Call to Action

Ready to streamline your EU VAT validation process? Sign up for our free API key today and integrate it effortlessly with your Go Fiber application! Don’t forget to subscribe to our updates and join the community forums for more developer guides.

Top comments (0)