Démarrage rapide
Quickstart : créer une facture en JavaScript
JavaScript pur (ESM ou CJS) avec @scell/sdk ou fetch
Le SDK `@scell/sdk` est utilisable en JavaScript pur (sans TypeScript) en mode ESM (`import`) ou CJS (`require`). Pour les contraintes spécifiques (Cloudflare Workers, Deno, Bun), utiliser `fetch` natif directement avec le header `X-API-Key`. Tous les payloads sont des JSON snake_case standards. Compatible Node 18+, Bun 1.0+, Deno 1.40+. Pour le client (navigateur), utiliser exclusivement la `pk_*` (publishable) — jamais la `sk_*` (secret).
À retenir
- Prérequis : Node.js 18+ / Bun / Deno / Cloudflare Workers
- Installation : `npm i @scell/sdk` (avec SDK) ou rien (fetch direct)
- Variable d'env : `process.env.SCELL_API_KEY` (Node) / `Deno.env` / `env.SCELL_API_KEY` (CF)
- HTTP : fetch natif
- Output attendu : objet JSON avec `id` et `status`
- Codes erreur : 4xx → throw, 5xx → retry recommandé
Exemple de code
// Approche fetch direct (zero-dep). Alternative: import { ScellApiClient } from '@scell/sdk'
const API = 'https://api.scell.io/api/v1';
const KEY = process.env.SCELL_API_KEY;
async function call(path, body) {
const r = await fetch(`${API}${path}`, {
method: 'POST',
headers: { 'X-API-Key': KEY, 'Content-Type': 'application/json', 'Idempotency-Key': crypto.randomUUID() },
body: JSON.stringify(body),
});
if (!r.ok) throw new Error(`${r.status} ${await r.text()}`);
return r.json();
}
const seller = {
siret: '12345678901234', name: 'Ma SAS', vat_number: 'FR12345678901',
address: { street: '1 rue Test', city: 'Paris', postal_code: '75001', country: 'FR' },
};
// 1. Facture B2B
const b2b = await call('/invoices', {
format: 'factur-x', profile: 'EN16931', currency: 'EUR', seller,
buyer: {
siret: '98765432109876', name: 'Client SARL',
address: { street: '2 avenue Demo', city: 'Lyon', postal_code: '69001', country: 'FR' },
},
lines: [{ description: 'Prestation conseil', quantity: 1, unit_price: 1000, vat_rate: 20 }],
});
console.log(`B2B: ${b2b.id} (${b2b.status})`);
// 2. Facture B2C (particulier)
const b2c = await call('/invoices', {
format: 'factur-x', profile: 'EN16931', currency: 'EUR', seller,
buyer: {
buyer_is_individual: true, name: 'Jean Dupont', email: 'jean@example.com',
address: { street: '3 bd Client', city: 'Marseille', postal_code: '13001', country: 'FR' },
},
lines: [{ description: 'Abonnement', quantity: 1, unit_price: 49.9, vat_rate: 20 }],
});
console.log(`B2C: ${b2c.id} (${b2c.status})`);
// 3. Signature simple eIDAS EU-SES
const sig = await call('/signatures', {
document_url: 'https://example.com/contrat.pdf',
signers: [{ name: 'Jean Dupont', email: 'jean@example.com', phone: '+33612345678' }],
signature_options: { signature_mode: 'drawn', timezone: 'Europe/Paris' },
});
console.log(`Signature: ${sig.id} → ${sig.signing_url}`);