Verifactu Development in Delphi

Help for developers to integrate Verifactu with Delphi: 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 Delphi

To make HTTP requests in Delphi to an API like Verifacti's, you can use the following code.

uses
  System.SysUtils, System.Classes, System.Net.HttpClient, System.Net.URLClient;

procedure CreateInvoice;
var
  HttpClient: TNetHTTPClient;
  Response: IHTTPResponse;
  JSONData: TStringStream;
  URL: string;
begin
  URL := 'https://api.verifacti.com/verifactu/create';
  HttpClient := TNetHTTPClient.Create(nil);
  try
    HttpClient.CustomHeaders['Authorization'] := 'Bearer <API_KEY>';
    HttpClient.ContentType := 'application/json';

    JSONData := TStringStream.Create(
      '{' +
      '"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"' +
      '}',
      TEncoding.UTF8
    );
    try
      Response := HttpClient.Post(URL, JSONData);
      if Response.StatusCode = 200 then
        WriteLn('Success: ' + Response.ContentAsString())
      else
        WriteLn('Error: ' + Response.StatusCode.ToString);
    finally
      JSONData.Free;
    end;
  finally
    HttpClient.Free;
  end;
end;

These units are part of Delphi's standard RTL and are available in Delphi versions that support TNetHTTPClient (typically Delphi XE8 and later)

Integration example with WinHttpRequest

If you prefer to use WinHttpRequest in Delphi, it can be done as follows:

uses
  ComObj, SysUtils, ActiveX;

procedure CreateInvoiceWithWinHttp;
var
  WinHttp: OleVariant;
  URL, APIKey, JSONData, Response: string;
begin
  CoInitialize(nil);
  try
    URL := 'https://api.verifacti.com/verifactu/create';
    APIKey := '<API_KEY>';
    JSONData := '{' +
      '"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"' +
      '}';
      
    WinHttp := CreateOleObject('WinHttp.WinHttpRequest.5.1');
    WinHttp.Open('POST', URL, False);
    WinHttp.SetRequestHeader('Authorization', 'Bearer ' + APIKey);
    WinHttp.SetRequestHeader('Content-Type', 'application/json');
    WinHttp.Send(JSONData);
    Response := WinHttp.ResponseText;
    
    WriteLn('Response: ' + Response);
  finally
    CoUninitialize;
  end;
end;

How to attach the QR to the invoice in Delphi

To embed a QR code in a PDF, you can use libraries such as:

Example with SynPDF:

uses
  SynPDF;

procedure AgregarQRAlPDF;
var
  Doc: TPdfDocumentGDI;
  Page: TPdfPage;
  QRBitmap: TBitmap;
begin
  Doc := TPdfDocumentGDI.Create;
  try
    Page := Doc.AddPage;
    QRBitmap := TBitmap.Create;
    try
      // Aquí se cargaría la imagen del QR en QRBitmap
      Page.RenderDraw(QRBitmap, 100, 100, 200, 200);
    finally
      QRBitmap.Free;
    end;
    Doc.SaveToFile('FacturaConQR.pdf');
  finally
    Doc.Free;
  end;
end;

How to generate a QR in Delphi

You can use the following libraries to generate QR codes in Delphi:

Example with DelphiZXingQRCode:

uses
  DelphiZXingQRCode, Vcl.Graphics;

procedure GenerarQR;
var
  QRCode: TDelphiZXingQRCode;
  Bitmap: TBitmap;
begin
  QRCode := TDelphiZXingQRCode.Create;
  try
    QRCode.Data := 'https://verifacti.com';
    QRCode.Encoding := TQRCodeEncoding.qrAuto;
    Bitmap := TBitmap.Create;
    try
      Bitmap.SetSize(QRCode.Rows, QRCode.Columns);
      QRCode.DrawBitmap(Bitmap);
      Bitmap.SaveToFile('QRFactura.bmp');
    finally
      Bitmap.Free;
    end;
  finally
    QRCode.Free;
  end;
end;

Related posts

Verifactu mandatory compliance: who must comply with Verifactu?

Verifactu mandatory compliance. Who is required to comply with VeriFactu. Mandatory compliance for computerized invoicing systems, legal entities and self-employed workers.

The VeriFactu Regulation: law and regulations for VeriFactu

Explanation of Royal Decree 1007/2023, known as the VeriFactu regulation. Summary and implications of the VeriFactu regulation.

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.