# `POST /v1/generate/facturx` — exemples par langage

Requête : JSON de facture (voir le schéma `Invoice` dans `openapi.yaml`).
Réponse : `application/pdf` (200) ou une erreur RFC 7807 (`application/problem+json`).

## curl

```bash
curl -X POST "https://api.example/v1/generate/facturx?profile=EN16931" \
  -H "Authorization: Bearer $FACTURX_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "invoiceNumber": "F-2026-001",
    "issueDate": "2026-07-20",
    "currencyCode": "EUR",
    "seller": {"name": "Vendeur SARL"},
    "buyer": {"name": "Acheteur SAS"},
    "lines": [{"id": "1", "itemName": "Prestation", "quantity": 1, "unitPrice": 100, "vatRate": 20}],
    "vatBreakdown": [{"category": "S", "taxableAmount": 100, "vatAmount": 20}],
    "totals": {"totalWithoutVat": 100, "totalVat": 20, "totalWithVat": 120, "amountDue": 120}
  }' \
  -o facture.pdf
```

## PHP

```php
<?php
$ch = curl_init('https://api.example/v1/generate/facturx?profile=EN16931');
curl_setopt_array($ch, [
    CURLOPT_POST => true,
    CURLOPT_HTTPHEADER => [
        'Authorization: Bearer ' . getenv('FACTURX_API_KEY'),
        'Content-Type: application/json',
    ],
    CURLOPT_POSTFIELDS => json_encode([
        'invoiceNumber' => 'F-2026-001',
        'issueDate' => '2026-07-20',
        'currencyCode' => 'EUR',
        'seller' => ['name' => 'Vendeur SARL'],
        'buyer' => ['name' => 'Acheteur SAS'],
        'lines' => [['id' => '1', 'itemName' => 'Prestation', 'quantity' => 1, 'unitPrice' => 100, 'vatRate' => 20]],
        'vatBreakdown' => [['category' => 'S', 'taxableAmount' => 100, 'vatAmount' => 20]],
        'totals' => ['totalWithoutVat' => 100, 'totalVat' => 20, 'totalWithVat' => 120, 'amountDue' => 120],
    ]),
    CURLOPT_RETURNTRANSFER => true,
]);
$pdf = curl_exec($ch);
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) === 200) {
    file_put_contents('facture.pdf', $pdf);
}
```

## Python

```python
import requests
import os

resp = requests.post(
    "https://api.example/v1/generate/facturx",
    params={"profile": "EN16931"},
    headers={"Authorization": f"Bearer {os.environ['FACTURX_API_KEY']}"},
    json={
        "invoiceNumber": "F-2026-001",
        "issueDate": "2026-07-20",
        "currencyCode": "EUR",
        "seller": {"name": "Vendeur SARL"},
        "buyer": {"name": "Acheteur SAS"},
        "lines": [{"id": "1", "itemName": "Prestation", "quantity": 1, "unitPrice": 100, "vatRate": 20}],
        "vatBreakdown": [{"category": "S", "taxableAmount": 100, "vatAmount": 20}],
        "totals": {"totalWithoutVat": 100, "totalVat": 20, "totalWithVat": 120, "amountDue": 120},
    },
)
resp.raise_for_status()
with open("facture.pdf", "wb") as f:
    f.write(resp.content)
```

## JavaScript (Node.js, fetch)

```javascript
const resp = await fetch("https://api.example/v1/generate/facturx?profile=EN16931", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.FACTURX_API_KEY}`,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    invoiceNumber: "F-2026-001",
    issueDate: "2026-07-20",
    currencyCode: "EUR",
    seller: { name: "Vendeur SARL" },
    buyer: { name: "Acheteur SAS" },
    lines: [{ id: "1", itemName: "Prestation", quantity: 1, unitPrice: 100, vatRate: 20 }],
    vatBreakdown: [{ category: "S", taxableAmount: 100, vatAmount: 20 }],
    totals: { totalWithoutVat: 100, totalVat: 20, totalWithVat: 120, amountDue: 120 },
  }),
});
if (!resp.ok) throw new Error(`${resp.status}: ${await resp.text()}`);
const pdfBuffer = Buffer.from(await resp.arrayBuffer());
```

## Go

```go
package main

import (
	"bytes"
	"encoding/json"
	"io"
	"net/http"
	"os"
)

func main() {
	body, _ := json.Marshal(map[string]any{
		"invoiceNumber": "F-2026-001",
		"issueDate":     "2026-07-20",
		"currencyCode":  "EUR",
		"seller":        map[string]string{"name": "Vendeur SARL"},
		"buyer":         map[string]string{"name": "Acheteur SAS"},
		"lines": []map[string]any{
			{"id": "1", "itemName": "Prestation", "quantity": 1, "unitPrice": 100, "vatRate": 20},
		},
		"vatBreakdown": []map[string]any{{"category": "S", "taxableAmount": 100, "vatAmount": 20}},
		"totals":       map[string]any{"totalWithoutVat": 100, "totalVat": 20, "totalWithVat": 120, "amountDue": 120},
	})

	req, _ := http.NewRequest(http.MethodPost,
		"https://api.example/v1/generate/facturx?profile=EN16931", bytes.NewReader(body))
	req.Header.Set("Authorization", "Bearer "+os.Getenv("FACTURX_API_KEY"))
	req.Header.Set("Content-Type", "application/json")

	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()

	out, _ := os.Create("facture.pdf")
	defer out.Close()
	io.Copy(out, resp.Body)
}
```

## Erreur 422 (champs manquants)

```json
{
  "type": "incomplete-invoice",
  "title": "Champs requis manquants pour le profil demandé",
  "status": 422,
  "instance": "01K...",
  "missing": [{ "bt": "BT-1", "path": "invoiceNumber" }],
  "profile": "EN16931"
}
```
