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.

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)
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;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;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;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.