Ejemplos / Rectificar, completar y anular
Factura Verifactu: abono de una factura con retención
Ejemplo de la API Verifactu: el JSON que envías a la AEAT con POST /tbai/, el mismo código en PHP y otros lenguajes, y la respuesta que recibes.
Actualizado el
POST /tbai/Verifactu
Abono total de la factura del ejemplo «Profesional con retención de IRPF». En un abono la retención va en negativo, igual que la base y el IVA: base −600, IVA −126, retención −90.
total_factura sigue siendo el líquido: −600 − 126 + 90 = −636 €. La API declara un importe total de −726 € (en el registro y en el QR) y una retención de −90 €. Si envías la retención en positivo, la API le pone el signo de la factura.
{
"fecha": "03/12/2026",
"hora": "10:30:00",
"nif": "B12345674",
"nombre": "Cliente Ejemplo S.L.",
"direccion": "Calle Mayor 1",
"cp": "01001",
"pais_cliente": "ES",
"serie": "RP2026",
"numero": "007",
"simplificada": false,
"rectificativa": true,
"clave_rectificativa": "R1",
"tipo_rectificativa": "I",
"tipo_operacion": "servicios",
"retencion": -90.0,
"rectificadas": [
{
"serie": "P2026",
"numero": "042",
"fecha": "02/12/2026"
}
],
"lineas": [
{
"descripcion": "Abono: dirección de obra",
"cantidad": -1,
"importe_unitario": 600.0,
"tipo_iva": 21,
"tipo_req": 0
}
],
"total_factura": -636.0
}
<?php
// composer require ticketbaiws/ticketbaiws-php
require 'vendor/autoload.php';
$tbai = new \TicketbaiWS\Client('TU_TOKEN_DE_TEST', 'B89670855', ['entorno' => 'test']);
$r = $tbai->crearFactura([
'fecha' => '03/12/2026',
'hora' => '10:30:00',
'nif' => 'B12345674',
'nombre' => 'Cliente Ejemplo S.L.',
'direccion' => 'Calle Mayor 1',
'cp' => '01001',
'pais_cliente' => 'ES',
'serie' => 'RP2026',
'numero' => '007',
'simplificada' => false,
'rectificativa' => true,
'clave_rectificativa' => 'R1',
'tipo_rectificativa' => 'I',
'tipo_operacion' => 'servicios',
'retencion' => -90.0,
'rectificadas' => [
['serie' => 'P2026', 'numero' => '042', 'fecha' => '02/12/2026'],
],
'lineas' => [
['descripcion' => 'Abono: dirección de obra', 'cantidad' => -1, 'importe_unitario' => 600.0, 'tipo_iva' => 21, 'tipo_req' => 0],
],
'total_factura' => -636.0,
]);
if ($r->isError()) {
throw new RuntimeException($r->getMessageText());
}
// $r->getData('qr') es el PNG del QR en base64 para imprimir en la factura
curl -X POST 'https://api-test.verifactu.ws/tbai/' \
-H 'Accept: application/json' \
-H 'Content-Type: application/json' \
-H 'Token: TU_TOKEN_DE_TEST' \
-H 'Nif: B89670855' \
--data-binary @- <<'JSON'
{
"fecha": "03/12/2026",
"hora": "10:30:00",
"nif": "B12345674",
"nombre": "Cliente Ejemplo S.L.",
"direccion": "Calle Mayor 1",
"cp": "01001",
"pais_cliente": "ES",
"serie": "RP2026",
"numero": "007",
"simplificada": false,
"rectificativa": true,
"clave_rectificativa": "R1",
"tipo_rectificativa": "I",
"tipo_operacion": "servicios",
"retencion": -90.0,
"rectificadas": [
{
"serie": "P2026",
"numero": "042",
"fecha": "02/12/2026"
}
],
"lineas": [
{
"descripcion": "Abono: dirección de obra",
"cantidad": -1,
"importe_unitario": 600.0,
"tipo_iva": 21,
"tipo_req": 0
}
],
"total_factura": -636.0
}
JSON
const datos = {
"fecha": "03/12/2026",
"hora": "10:30:00",
"nif": "B12345674",
"nombre": "Cliente Ejemplo S.L.",
"direccion": "Calle Mayor 1",
"cp": "01001",
"pais_cliente": "ES",
"serie": "RP2026",
"numero": "007",
"simplificada": false,
"rectificativa": true,
"clave_rectificativa": "R1",
"tipo_rectificativa": "I",
"tipo_operacion": "servicios",
"retencion": -90.0,
"rectificadas": [
{
"serie": "P2026",
"numero": "042",
"fecha": "02/12/2026"
}
],
"lineas": [
{
"descripcion": "Abono: dirección de obra",
"cantidad": -1,
"importe_unitario": 600.0,
"tipo_iva": 21,
"tipo_req": 0
}
],
"total_factura": -636.0
};
const r = await fetch("https://api-test.verifactu.ws/tbai/", {
method: "POST",
headers: {
"Accept": "application/json",
"Content-Type": "application/json",
"Token": "TU_TOKEN_DE_TEST",
"Nif": "B89670855",
},
body: JSON.stringify(datos),
});
const respuesta = await r.json();
if (respuesta.result === "ERROR") throw new Error(respuesta.msg);
console.log(respuesta.return);
import json
import requests
datos = json.loads(r"""
{
"fecha": "03/12/2026",
"hora": "10:30:00",
"nif": "B12345674",
"nombre": "Cliente Ejemplo S.L.",
"direccion": "Calle Mayor 1",
"cp": "01001",
"pais_cliente": "ES",
"serie": "RP2026",
"numero": "007",
"simplificada": false,
"rectificativa": true,
"clave_rectificativa": "R1",
"tipo_rectificativa": "I",
"tipo_operacion": "servicios",
"retencion": -90.0,
"rectificadas": [
{
"serie": "P2026",
"numero": "042",
"fecha": "02/12/2026"
}
],
"lineas": [
{
"descripcion": "Abono: dirección de obra",
"cantidad": -1,
"importe_unitario": 600.0,
"tipo_iva": 21,
"tipo_req": 0
}
],
"total_factura": -636.0
}
""")
r = requests.request(
"POST",
"https://api-test.verifactu.ws/tbai/",
headers={"Token": "TU_TOKEN_DE_TEST", "Nif": "B89670855", "Accept": "application/json"},
json=datos,
timeout=30,
)
respuesta = r.json()
if respuesta["result"] == "ERROR":
raise Exception(respuesta["msg"])
print(respuesta["return"])
using System.Net.Http;
using System.Text;
var json = """
{
"fecha": "03/12/2026",
"hora": "10:30:00",
"nif": "B12345674",
"nombre": "Cliente Ejemplo S.L.",
"direccion": "Calle Mayor 1",
"cp": "01001",
"pais_cliente": "ES",
"serie": "RP2026",
"numero": "007",
"simplificada": false,
"rectificativa": true,
"clave_rectificativa": "R1",
"tipo_rectificativa": "I",
"tipo_operacion": "servicios",
"retencion": -90.0,
"rectificadas": [
{
"serie": "P2026",
"numero": "042",
"fecha": "02/12/2026"
}
],
"lineas": [
{
"descripcion": "Abono: dirección de obra",
"cantidad": -1,
"importe_unitario": 600.0,
"tipo_iva": 21,
"tipo_req": 0
}
],
"total_factura": -636.0
}
""";
using var http = new HttpClient();
var peticion = new HttpRequestMessage(new HttpMethod("POST"), "https://api-test.verifactu.ws/tbai/")
{
Content = new StringContent(json, Encoding.UTF8, "application/json")
};
peticion.Headers.Add("Token", "TU_TOKEN_DE_TEST");
peticion.Headers.Add("Nif", "B89670855");
var respuesta = await http.SendAsync(peticion);
Console.WriteLine(await respuesta.Content.ReadAsStringAsync());
Respuesta de ejemplo
{
"result": "PENDING",
"return": {
"huella": "B2222C4136C1841400AB6B62497EB545FC868B2D8F7CA66C01337A213677BFC2",
"qr": "iVBORw0KGgo… (PNG en base64)",
"url": "https://www2.agenciatributaria.gob.es/wlpl/TIKE-CONT/ValidarQR?nif=B89670855&numserie=RP2026007&fecha=03-12-2026&importe=-726.00"
},
"msg": null
}- Los importes de las líneas van sin IVA. La API responde siempre con HTTP 200: comprueba
result(OK,PENDINGoERROR). - Sin
"sincrono": truela respuesta esPENDINGy la factura se envía a la AEAT en segundo plano. El resultado llega por webhook o conGET /tbai/. - Imprime en la factura el QR (
return.qr, PNG en base64) y la leyenda «VERI*FACTU».