## New TicketBAI / Verifactu invoice

`POST`  https://{entorno}.ticketbaiws.eus/tbai/

This method sends an invoice to the corresponding tax authority and returns the fingerprint, the QR code image in base64 and the invoice verification URL contained in the QR. The test environment submits to the test environment of the corresponding tax authority. It works with the three Basque provincial tax authorities and with the AEAT (Spanish Tax Agency) for Verifactu

**Invoice resubmission:**  To correct or modify invoices that were reported with warnings or errors, make a new submission (with the corrected data) setting the **ZUZENDU or SUBSANAR parameter to true** .
**(*) The series, number and total amount of the invoice cannot be modified; they must be the same as in the original invoice. (In Bizkaia, for invoices rejected by Batuz, the correction is sent through LROE subchapter 1.2 and keeps the fingerprint and QR of the original invoice; to change an invoice that has already been accepted, issue a corrective invoice)**

**Resending:**  invoice creation is idempotent. If an invoice that already exists (same date, series and number) is sent again, no new one is created: the original is returned and **msg**  says so ("Previously sent on ..."). If it is resent with different data and without ZUZENDU, it is not duplicated either: the original is returned with the warning "Previously sent with different values ...". This makes it safe to retry an invoice creation after a timeout, a network error or a 429 response (honoring the **Retry-After**  header).

**PARAMETERS**

- `fecha` (string, required): Invoice issue date in dd/mm/yyyy format. Ejemplo: `13/11/2021`
- `hora` (string, required): Invoice issue time in HH:MM:SS format. Ejemplo: `12:14:00`
- `fecha_operacion` (string, optional): Transaction date in dd/mm/yyyy format. Ejemplo: `12/11/2021`
- `nif` (string, required *): NIF of the customer the invoice is issued to
  (*) Required only if **simplificada**  is false. Optional for simplified invoices. Ejemplo: `B01000012`
- `tipo_documento` (string, optional): Type of identification document for foreign customers. Defaults to 02 for intra-EU invoices or 06 for the rest.
  Possible values:
  **02**  => NIF-IVA (EU VAT number)
  **03**  => Passport
  **04**  => Official identity document issued by the country or territory of residence,
  **05**  => Certificate of residence
  **06**  => Other supporting document. Ejemplo: `02`
- `simplificada` (boolean, required): The invoice is a simplified invoice. If it is set as simplified, customer details are not required. Ejemplo: `true`
- `pais_cliente` (string, optional): 2-letter country code of the customer if the customer's NIF is foreign (ISO-3166 Alpha-2 code format) Ejemplo: `ES`
- `nombre` (string, required *): Legal name of the customer the invoice is issued to
  (*) Required only if **simplificada**  is false. Optional for simplified invoices. Ejemplo: `Example Company S.L.`
- `direccion` (string, required *): Billing address of the customer the invoice is issued to
  (*) Required only in Álava and Gipuzkoa. Ejemplo: `123 Example Street`
- `cp` (string, required *): Postal code of the customer the invoice is issued to
  (*) Required only in Álava and Gipuzkoa. Ejemplo: `01013`
- `poblacion` (string, recommended): Town/municipality of the customer the invoice is issued to
  (*) It will be required for invoices to companies and professionals established in Spain that are sent to the AEAT's SPFE (Spanish public B2B e-invoicing platform) under the Crea y Crece law. If it is missing, the response includes a warning in **msg** . Ejemplo: `Vitoria-Gasteiz`
- `provincia` (string, optional): Customer's province. If not provided, it is derived from the postal code for invoices to customers in Spain. Ejemplo: `Álava`
- `serie` (string, required): Invoice series. Ejemplo: `A`
- `numero` (string, required): Invoice number. Ejemplo: `2021000123`
- `rectificativa` (boolean, required): The invoice is a corrective invoice. Ejemplo: `false`
- `clave_rectificativa` (string, optional): Corrective invoice code (R1, R2, R3, R4, R5). If not set, R1 is used, or R5 if it is a simplified invoice. Ejemplo: `R1`
- `tipo_rectificativa` (string, optional): Corrective invoice type: (S) by substitution (I) by differences. If not set, it is interpreted as**I (by differences).** The value "SN" is available for corrective invoices by substitution without providing the ImporteRectificacionSustitutiva block. Ejemplo: `I`
- `rectificadas` (object array, required if rectificativa)
  - `serie` (string, required): Series of the invoice being corrected. Ejemplo: `A`
  - `numero` (string, required): Number of the invoice being corrected. Ejemplo: `2021000100`
  - `fecha` (string, required): Date of the invoice being corrected in dd/mm/yyyy format. Ejemplo: `13/11/2021`
  - `base` (float, optional): Tax base of the invoice being corrected. Only needed if the invoice was not previously sent through Ticketbai WS. Ejemplo: `100.00`
  - `cuota` (float, optional): VAT amount of the invoice being corrected. Only needed if the invoice was not previously sent through Ticketbai WS. Ejemplo: `21.00`
  - `recargo` (float, optional): Equivalence surcharge of the invoice being corrected. Only needed if the invoice was not previously sent through Ticketbai WS. Ejemplo: `5.20`
- `tipo_operacion` (string, optional): Transaction type: servicios (services) or bienes (goods). If not set, the transaction is treated as servicios. Ejemplo: `servicios`
- `intracomunitaria` (boolean, optional): The invoice is an intra-EU invoice. Ejemplo: `false`
- `exportacion` (boolean, optional): The invoice is an export outside the EU. Ejemplo: `false`
- `retencion` (float, required): Withholding amount applied. Ejemplo: `0.00`
- `lineas` (object array. MAX 1000 lines, required *): (*) Verifactu and Bizkaia do not require the detail of each invoice line, but they do require
  the overall tax bases
  - `descripcion` (string, Invoice line description (optional)) Ejemplo: `Standard product`
  - `cantidad` (float, required): Number of units in the line. Ejemplo: `1`
  - `importe_unitario` (float, required): Line amount EXCLUDING VAT. Ejemplo: `100.00`
  - `tipo_iva` (float, required): VAT percentage applied to the line. Ejemplo: `21.00`
  - `tipo_req` (float, required): Equivalence surcharge percentage applied to the line. Ejemplo: `0.00`
  - `descuento` (float, optional): Discount percentage applied to the line. Ejemplo: `10.00`
  - `regimen_general` (boolean, optional): Indicates that the item falls under the general VAT scheme when the rest of the invoice is under the simplified or equivalence surcharge scheme. Only taken into account if the modo_recargo_equivalencia or modo_regimen_simplificado flag is true. Ejemplo: `true`
  - `epigrafe` (integer, optional, only for individuals (natural persons) in Bizkaia (LROE 140)): Indicates that the item falls under a specific IAE activity code, different from the general one defined in the company profile or from the one defined on the invoice. Ejemplo: `197210`
- `total_factura` (float, required): Invoice total amount. Ejemplo: `132.00`
- `zuzendu` (boolean, optional): Modification or resubmission of an invoice that previously returned an error. In Bizkaia it applies to invoices rejected by Batuz: it is sent through LROE subchapter 1.2 and keeps the original fingerprint and QR. To change an invoice that has already been accepted, issue a corrective invoice. Ejemplo: `false`

**OTHER OPTIONAL PARAMETERS**

- `regimen_iva` (integer, optional): Special VAT regime code. Ejemplo: `17`
- `causa_exencion` (string, optional): VAT exemption reason code. If not set, the application will determine the most appropriate one
  Options:
  **E1** : Domestic transactions (Article 20 of the VAT Law)
  **E2** : Exports of goods (Article 21 of the VAT Law)
  **E3** : Exports (ships, fishing...) (Article 22 of the VAT Law)
  **E4** : Free zones, free warehouses and customs regimes (Articles 23 and 24 of the VAT Law)
  **E5** : Intra-EU transactions (Article 25 of the VAT Law)
  **E6** : Exempt for another reason
  **RL:**  Not subject under place-of-supply rules
  **OT:**  Other reasons for not being subject (Article 7 of the VAT Law)
  **IE:**  Not subject under place-of-supply rules but foreign tax, IPSI/IGIC or VAT is charged
  **VT:**  Not subject, sales made on behalf of third parties (amount not counted for VAT or IRPF purposes)
  **(*) Exemption reasons not available for Verifactu**. Ejemplo: `E6`
- `inversion_sujeto_pasivo` (boolean, optional): Invoice with reverse charge. Ejemplo: `false`
- `emitida_terceros` (string, optional): Invoice issued by a third party or by the recipient. Possible values: **N** (no), **T** (third party), **D** (recipient) Ejemplo: `N`
- `modo_recargo_equivalencia` (boolean, optional): Transaction under the equivalence surcharge scheme. Ejemplo: `false`
- `modo_regimen_simplificado` (boolean, optional): Transaction under the simplified VAT scheme. Ejemplo: `false`
- `epigrafe` (string, optional): IAE activity code for the invoice. If not provided, the activity code set in the company profile will be sent. This parameter is only valid for individuals (natural persons) who must send TicketBAI to Bizkaia Batuz and work under more than one activity code. Ejemplo: `165960`

**POST** `https://{entorno}.ticketbaiws.eus/tbai/`

```bash
$ curl --request POST \
     --url https://api-test.ticketbaiws.eus/tbai/ \
     --header 'Accept: application/json' \
     --header 'Content-Type: application/json' \
     --header 'Token: xxx' \
     --header 'Nif: 00000014Z'
	 --data '
{
    "fecha": "13\/11\/2021",
    "hora": "12:14:00",
	"fecha_operacion": "12\/11\/2021",
    "pais_cliente": "ES",
	"nif": "B00000011",
    "nombre": "Empresa de ejemplo S.L.",
    "direccion": "Calle Falsa 123",
    "cp": "28080",
    "poblacion": "Madrid",
    "provincia": "Madrid",
    "serie": "A",
    "numero": "2021000123",
    "simplificada": false,
    "rectificativa": false,
    "clave_rectificativa": "R1",
    "tipo_rectificativa": "I",
    "rectificadas" : [
        {
            "serie": "A"
            "numero": "20210000075",
            "fecha": "10\/11\/2021"
        }
    ],
    "tipo_operacion": "servicios",
    "intracomunitaria": false,
	"exportacion": false,
    "retencion": 0,
    "lineas": [
        {
            "descripcion": "Producto normal",
            "cantidad": 1,
            "importe_unitario": 100.00,
            "tipo_iva": 21.00,
            "tipo_req": 0
        },
        {
            "descripcion": "Producto alimentario",
            "cantidad": 1,
            "importe_unitario": 10,
            "tipo_iva": 10.00,
            "tipo_req": 0
        }
    ],
    "total_factura": 132.00,
	"zuzendu" : false
}'
```

Response for Ticketbai:

```json
HTTP/1.1 200 OK
{
	"result": "OK",
	"return": {
		"huella_tbai" : "TBAI-B01000012-131121-zG3wtCW3WlOSN-108",
		"qr" : "iVBORw0KGgoAAAANSUhEUgAAAfQAAAH0CAIAAABEtEjdAAAABmJ...",
		"url" : "https://batuz.eus/QRTBAI/?id=TBAI-..."
	},
	"msg": null
}
```

Response for Verifactu:

```json
HTTP/1.1 200 OK
{
	"result": "OK",
	"return": {
		"huella" : "8FA696A615A750A9EC04A273013F05EB1B092076FFD9A66EF6",
		"qr" : "iVBORw0KGgoAAAANSUhEUgAAAfQAAAH0CAIAAABEtEjdAAAABmJ...",
		"url" : "https://prewww2.aeat.es/wlpl/TIKE-CONT/ValidarQR?..."
	},
	"msg": null
}
```

Response with notices:

```json
HTTP/1.1 200 OK
{
	"result": "OK",
	"return": {
		"huella" : "8FA696A615A750A9EC04A273013F05EB1B092076FFD9A66EF6",
		"qr" : "iVBORw0KGgoAAAANSUhEUgAAAfQAAAH0CAIAAABEtEjdAAAABmJ...",
		"url" : "https://prewww2.aeat.es/wlpl/TIKE-CONT/ValidarQR?..."
	},
	"msg": "poblacion: customer town/city is missing. It will be mandatory for B2B invoices sent to the AEAT e-invoicing public solution (SPFE, Ley Crea y Crece)"
}
```

Fuente: https://ticketbaiws.eus/en/ticketbai-post/
