Ejemplos / Regímenes y casos especiales
Factura Verifactu: inversión del sujeto pasivo
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
Operaciones en las que el IVA lo autoliquida el cliente (ejecuciones de obra, chatarra, oro de inversión…). Marca inversion_sujeto_pasivo: true y envía las líneas a IVA 0.
{
"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": "A2026",
"numero": "000160",
"simplificada": false,
"rectificativa": false,
"tipo_operacion": "servicios",
"inversion_sujeto_pasivo": true,
"retencion": 0,
"lineas": [
{
"descripcion": "Rehabilitación de fachada — certificación 2",
"cantidad": 1,
"importe_unitario": 12500,
"tipo_iva": 0,
"tipo_req": 0
}
],
"total_factura": 12500.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' => 'A2026',
'numero' => '000160',
'simplificada' => false,
'rectificativa' => false,
'tipo_operacion' => 'servicios',
'inversion_sujeto_pasivo' => true,
'retencion' => 0,
'lineas' => [
['descripcion' => 'Rehabilitación de fachada — certificación 2', 'cantidad' => 1, 'importe_unitario' => 12500, 'tipo_iva' => 0, 'tipo_req' => 0],
],
'total_factura' => 12500.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": "A2026",
"numero": "000160",
"simplificada": false,
"rectificativa": false,
"tipo_operacion": "servicios",
"inversion_sujeto_pasivo": true,
"retencion": 0,
"lineas": [
{
"descripcion": "Rehabilitación de fachada — certificación 2",
"cantidad": 1,
"importe_unitario": 12500,
"tipo_iva": 0,
"tipo_req": 0
}
],
"total_factura": 12500.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": "A2026",
"numero": "000160",
"simplificada": false,
"rectificativa": false,
"tipo_operacion": "servicios",
"inversion_sujeto_pasivo": true,
"retencion": 0,
"lineas": [
{
"descripcion": "Rehabilitación de fachada — certificación 2",
"cantidad": 1,
"importe_unitario": 12500,
"tipo_iva": 0,
"tipo_req": 0
}
],
"total_factura": 12500.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": "A2026",
"numero": "000160",
"simplificada": false,
"rectificativa": false,
"tipo_operacion": "servicios",
"inversion_sujeto_pasivo": true,
"retencion": 0,
"lineas": [
{
"descripcion": "Rehabilitación de fachada — certificación 2",
"cantidad": 1,
"importe_unitario": 12500,
"tipo_iva": 0,
"tipo_req": 0
}
],
"total_factura": 12500.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": "A2026",
"numero": "000160",
"simplificada": false,
"rectificativa": false,
"tipo_operacion": "servicios",
"inversion_sujeto_pasivo": true,
"retencion": 0,
"lineas": [
{
"descripcion": "Rehabilitación de fachada — certificación 2",
"cantidad": 1,
"importe_unitario": 12500,
"tipo_iva": 0,
"tipo_req": 0
}
],
"total_factura": 12500.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": "2AEC72460A111C1CAB0811EA0B0DD21348DC7CA79983DC6B46AB7CDB840E4917",
"qr": "iVBORw0KGgo… (PNG en base64)",
"url": "https://www2.agenciatributaria.gob.es/wlpl/TIKE-CONT/ValidarQR?nif=B89670855&numserie=A2026000160&fecha=03-12-2026&importe=12500.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».