1import { ScellApiClient } from '@scell/sdk';
2
3const scell = new ScellApiClient('sk_live_your_api_key');
4
5// ── B2B Invoice (Factur-X) ──────────────────────────────────────────
6const { data: invoice } = await scell.invoices.create({
7 direction: 'outgoing',
8 output_format: 'facturx', // 'facturx' | 'ubl' | 'cii'
9 invoice_date: '2026-06-01',
10 due_date: '2026-07-01',
11 currency: 'EUR',
12
13 // Seller (flat fields — server validates per country)
14 seller_name: 'QR Communication SAS',
15 seller_siret: '90347842500015', // required if seller_country=FR
16 seller_vat_number: 'FR42903478425',
17 seller_country: 'FR',
18 seller_address: {
19 line1: '123 Avenue de la République',
20 postal_code: '75011',
21 city: 'Paris',
22 country: 'FR',
23 },
24
25 // Buyer — option 1: flat fields
26 buyer_name: 'GN IMMO',
27 buyer_siret: '49438068600076', // required if buyer_country=FR && !buyer_is_individual
28 buyer_vat_number: 'FR42494380686',
29 buyer_country: 'FR',
30 buyer_address: {
31 line1: '453 Route Nationale 7',
32 postal_code: '13670',
33 city: 'Verquières',
34 country: 'FR',
35 },
36
37 // Amounts (HT + TVA = TTC)
38 total_ht: 2500.00,
39 total_tva: 500.00,
40 total_ttc: 3000.00,
41
42 // Lines (min 1)
43 lines: [
44 {
45 description: 'Prestation conseil transformation digitale',
46 quantity: 10,
47 unit: 'HUR', // UN/ECE Rec 20 (optional)
48 unit_price_ht: 250.00,
49 tva_rate: 20.0, // 0 | 5.5 | 10 | 20
50 total_ht: 2500.00,
51 total_ttc: 3000.00,
52 },
53 ],
54});
55
56console.log(invoice.invoice_number); // 'FAC-2026-000042'
57console.log(invoice.status); // 'draft'
58console.log(invoice.download_url); // presigned S3 URL
59
60// ── B2C Invoice (individual — no SIRET/VAT required) ────────────────
61const { data: b2cInvoice } = await scell.invoices.create({
62 direction: 'outgoing',
63 output_format: 'facturx',
64 invoice_date: '2026-06-01',
65 due_date: '2026-06-15',
66 currency: 'EUR',
67 seller_name: 'QR Communication SAS',
68 seller_siret: '90347842500015',
69 seller_country: 'FR',
70 seller_address: { line1: '123 Avenue de la République', postal_code: '75011', city: 'Paris', country: 'FR' },
71 buyer_name: 'Marie Dupont',
72 buyer_is_individual: true, // BT-46/47/48 omitted, no L441-10
73 buyer_country: 'FR',
74 buyer_address: { line1: '12 Rue des Lilas', postal_code: '75011', city: 'Paris', country: 'FR' },
75 total_ht: 150.00,
76 total_tva: 30.00,
77 total_ttc: 180.00,
78 lines: [{ description: 'Formation individuelle', quantity: 1, unit_price_ht: 150.00, tva_rate: 20.0, total_ht: 150.00, total_ttc: 180.00 }],
79});
80
81// ── With buyer_id (registry shortcut) ───────────────────────────────
82const { data: fromRegistry } = await scell.invoices.create({
83 direction: 'outgoing',
84 output_format: 'facturx',
85 invoice_date: '2026-06-01',
86 due_date: '2026-07-01',
87 currency: 'EUR',
88 seller_name: 'QR Communication SAS',
89 seller_siret: '90347842500015',
90 seller_country: 'FR',
91 seller_address: { line1: '123 Avenue de la République', postal_code: '75011', city: 'Paris', country: 'FR' },
92 buyer_id: '019e2dbe-362a-7105-87a5-f45fad1382ed', // auto-snapshot
93 total_ht: 2500.00,
94 total_tva: 500.00,
95 total_ttc: 3000.00,
96 lines: [{ description: 'Consulting', quantity: 10, unit_price_ht: 250.00, tva_rate: 20.0, total_ht: 2500.00, total_ttc: 3000.00 }],
97});
98
99// ── Sub-tenant scoping ──────────────────────────────────────────────
100const { data: subInvoice } = await scell.invoices.create({
101 sub_tenant_id: '019d5ea8-0000-0000-0000-000000000000',
102 direction: 'outgoing',
103 output_format: 'facturx',
104 invoice_date: '2026-06-01',
105 due_date: '2026-07-01',
106 currency: 'EUR',
107 buyer_id: '019e2dbe-362a-7105-87a5-f45fad1382ed',
108 total_ht: 1000.00,
109 total_tva: 200.00,
110 total_ttc: 1200.00,
111 lines: [{ description: 'Service', quantity: 1, unit_price_ht: 1000.00, tva_rate: 20.0, total_ht: 1000.00, total_ttc: 1200.00 }],
112});
113
114// ── Standard invoice from a quote (parent_quote_id, since v2.21.0) ──
115// Lien soft vers un devis source pour tracabilite (devis -> facture).
116// IMPORTANT : parent_quote_id est accepte UNIQUEMENT pour invoice_type='standard'
117// (ou champ omis). Pour une facture d'acompte ou de solde, utiliser les
118// endpoints dedies POST /quotes/{id}/convert-to-deposit / convert-to-balance.
119// Backend : 404 PARENT_QUOTE_NOT_FOUND si le devis n'appartient pas au tenant,
120// 422 si invoice_type='deposit' ou 'balance' est combine avec parent_quote_id.
121const { data: fromQuote } = await scell.invoices.create({
122 direction: 'outgoing',
123 output_format: 'facturx',
124 invoice_date: '2026-06-01',
125 due_date: '2026-07-01',
126 currency: 'EUR',
127 parent_quote_id: '019e3f00-7c8d-7000-9000-000000000001', // UUID du devis source
128 buyer_id: '019e2dbe-362a-7105-87a5-f45fad1382ed',
129 total_ht: 2500.00,
130 total_tva: 500.00,
131 total_ttc: 3000.00,
132 lines: [{ description: 'Consulting (devis DEV-2026-0042)', quantity: 10, unit_price_ht: 250.00, tva_rate: 20.0, total_ht: 2500.00, total_ttc: 3000.00 }],
133});
134