Verifactu Visual Basic: Complying with Verifactu regulations

Help for developers to integrate Verifactu with Visual Basic: learn to make REST requests, generate invoices with QR, sign with digital certificate and create XML efficiently.

screenshot
Verifacti

Verifacti

Mar 18, 2025

How to make a request to a REST API from Visual Basic

To make a request to a REST API, such as the Verifacti API, from Visual Basic, we can use HttpClient. Here is an example of how to make a POST request:

Imports System.Net.Http
Imports System.Net.Http.Headers
Imports System.Text
Imports System.Threading.Tasks

Module Module1
    Sub Main()
        ' Call the asynchronous method and wait for completion
        CreateInvoiceAsync().Wait()
    End Sub

    Private Async Function CreateInvoiceAsync() As Task
        Dim apiKey As String = "<API_KEY>"
        Dim url As String = "https://api.verifacti.com/verifactu/create"

        ' Build the JSON payload as a string
        Dim jsonPayload As String = "{" & _
            """serie"": ""A""," & _
            """numero"": ""234634""," & _
            """fecha_expedicion"": ""02-12-2024""," & _
            """tipo_factura"": ""F1""," & _
            """descripcion"": ""Descripcion de la operacion""," & _
            """nif"": ""A15022510""," & _
            """nombre"": ""Nombre cliente""," & _
            """lineas"": [{" & _
                """base_imponible"": ""200""," & _
                """tipo_impositivo"": ""21""," & _
                """cuota_repercutida"": ""42""" & _
            "}]," & _
            """importe_total"": ""242""" & _
            "}"

        Using client As New HttpClient()
            ' Set the authorization header
            client.DefaultRequestHeaders.Authorization = New AuthenticationHeaderValue("Bearer", apiKey)
            ' Set the Content-Type header
            client.DefaultRequestHeaders.Accept.Add(New MediaTypeWithQualityHeaderValue("application/json"))

            ' Create the StringContent with the JSON payload
            Dim content As New StringContent(jsonPayload, Encoding.UTF8, "application/json")

            ' Send the POST request asynchronously
            Dim response As HttpResponseMessage = Await client.PostAsync(url, content)

            If response.IsSuccessStatusCode Then
                Dim result As String = Await response.Content.ReadAsStringAsync()
                Console.WriteLine("Success: " & result)
            Else
                Console.WriteLine("Error: " & response.StatusCode.ToString())
            End If
        End Using
    End Function
End Module

How to attach the QR to the invoice in Visual Basic

To embed a QR code in an invoice in PDF format in Visual Basic, you can use libraries such as:

Example of how to insert a base64 image into a PDF with iTextSharp:

Imports System.IO
Imports iTextSharp.text
Imports iTextSharp.text.pdf
  
Sub InsertarQR(ByVal pdfPath As String, ByVal qrBase64 As String)
    Dim pdfReader As New PdfReader(pdfPath)
    Dim pdfStamper As New PdfStamper(pdfReader, New FileStream("FacturaConQR.pdf", FileMode.Create))
    Dim content As PdfContentByte = pdfStamper.GetOverContent(1)

    Dim qrBytes As Byte() = Convert.FromBase64String(qrBase64)
    Dim qrImage As Image = Image.GetInstance(qrBytes)
    qrImage.SetAbsolutePosition(50, 50)
    content.AddImage(qrImage)

    pdfStamper.Close()
    pdfReader.Close()
End Sub

How to generate a QR in Visual Basic

To generate a QR code in Visual Basic, you can use these libraries:

Example of generating a QR code with ZXing.Net:

Imports ZXing
Imports System.Drawing

Sub GenerarQR(ByVal texto As String)
    Dim qrWriter As New BarcodeWriter()
    qrWriter.Format = BarcodeFormat.QR_CODE

    Dim bitmap As Bitmap = qrWriter.Write(texto)
    bitmap.Save("QR.png", Imaging.ImageFormat.Png)
End Sub

The content provided by Bilbabit SL in any of its sections (Blog, guides, landing pages, FAQ section, commercial or customer support emails) is for informational purposes only and has no legal validity. BILBABIT is not a tax consulting firm, but a fiscal software developer, so the information it provides is of an indicative nature. In any case, any activity related to invoicing and taxation should always and without exception be consulted with a tax advisor who understands the specific implications of each regulation for each company or professional in particular.