Ejemplos / Rectificar, completar y anular
Factura Verifactu: abono o rectificativa por diferencias
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
Devolución parcial o total: envías solo la diferencia, en negativo. tipo_rectificativa: "I" (por diferencias, valor por defecto) y clave_rectificativa: "R1" (error fundado en derecho o devolución; por defecto). En rectificadas va la factura original.
Si la original se emitió con Ticketbai WS, basta con serie, número y fecha: la API recupera el resto.
{
"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": "R2026",
"numero": "0031",
"simplificada": false,
"rectificativa": true,
"clave_rectificativa": "R1",
"tipo_rectificativa": "I",
"tipo_operacion": "bienes",
"retencion": 0,
"rectificadas": [
{
"serie": "A2026",
"numero": "000154",
"fecha": "02/12/2026"
}
],
"lineas": [
{
"descripcion": "Devolución: auriculares",
"cantidad": -1,
"importe_unitario": 49.5868,
"tipo_iva": 21,
"tipo_req": 0,
"descuento": 10
}
],
"total_factura": -54.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' => 'R2026',
'numero' => '0031',
'simplificada' => false,
'rectificativa' => true,
'clave_rectificativa' => 'R1',
'tipo_rectificativa' => 'I',
'tipo_operacion' => 'bienes',
'retencion' => 0,
'rectificadas' => [
['serie' => 'A2026', 'numero' => '000154', 'fecha' => '02/12/2026'],
],
'lineas' => [
['descripcion' => 'Devolución: auriculares', 'cantidad' => -1, 'importe_unitario' => 49.5868, 'tipo_iva' => 21, 'tipo_req' => 0, 'descuento' => 10],
],
'total_factura' => -54.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": "R2026",
"numero": "0031",
"simplificada": false,
"rectificativa": true,
"clave_rectificativa": "R1",
"tipo_rectificativa": "I",
"tipo_operacion": "bienes",
"retencion": 0,
"rectificadas": [
{
"serie": "A2026",
"numero": "000154",
"fecha": "02/12/2026"
}
],
"lineas": [
{
"descripcion": "Devolución: auriculares",
"cantidad": -1,
"importe_unitario": 49.5868,
"tipo_iva": 21,
"tipo_req": 0,
"descuento": 10
}
],
"total_factura": -54.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": "R2026",
"numero": "0031",
"simplificada": false,
"rectificativa": true,
"clave_rectificativa": "R1",
"tipo_rectificativa": "I",
"tipo_operacion": "bienes",
"retencion": 0,
"rectificadas": [
{
"serie": "A2026",
"numero": "000154",
"fecha": "02/12/2026"
}
],
"lineas": [
{
"descripcion": "Devolución: auriculares",
"cantidad": -1,
"importe_unitario": 49.5868,
"tipo_iva": 21,
"tipo_req": 0,
"descuento": 10
}
],
"total_factura": -54.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": "R2026",
"numero": "0031",
"simplificada": false,
"rectificativa": true,
"clave_rectificativa": "R1",
"tipo_rectificativa": "I",
"tipo_operacion": "bienes",
"retencion": 0,
"rectificadas": [
{
"serie": "A2026",
"numero": "000154",
"fecha": "02/12/2026"
}
],
"lineas": [
{
"descripcion": "Devolución: auriculares",
"cantidad": -1,
"importe_unitario": 49.5868,
"tipo_iva": 21,
"tipo_req": 0,
"descuento": 10
}
],
"total_factura": -54.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": "R2026",
"numero": "0031",
"simplificada": false,
"rectificativa": true,
"clave_rectificativa": "R1",
"tipo_rectificativa": "I",
"tipo_operacion": "bienes",
"retencion": 0,
"rectificadas": [
{
"serie": "A2026",
"numero": "000154",
"fecha": "02/12/2026"
}
],
"lineas": [
{
"descripcion": "Devolución: auriculares",
"cantidad": -1,
"importe_unitario": 49.5868,
"tipo_iva": 21,
"tipo_req": 0,
"descuento": 10
}
],
"total_factura": -54.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": "6ADD71EB3E6BB97303530FA5C6501796246EB601C732F43218B8D15063356553",
"qr": "iVBORw0KGgo… (PNG en base64)",
"url": "https://www2.agenciatributaria.gob.es/wlpl/TIKE-CONT/ValidarQR?nif=B89670855&numserie=R20260031&fecha=03-12-2026&importe=-54.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».