Servolution developers

Tutoriel 3 — Pousser une facture Comptentra → cabinet d'expert-comptable

Objectif : envoyer automatiquement chaque mois le FEC (Fichier des Écritures Comptables) et les factures Factur-X au cabinet d'expert-comptable du dirigeant.

Pré-requis

1. Exporter le FEC du mois

curl "https://app.comptentra.servolution.fr/api/v1/fec?date_from=2026-04-01&date_to=2026-04-30" \
  -H "X-API-Key: $COMPTENTRA_KEY" \
  -o FEC-2026-04.txt

# Format conforme art. A.47 A-1 du LPF (BOI-CF-IOR-60-40)
# Encodage : ISO-8859-1, séparateur tabulation, header sur 1ère ligne

2. Exporter les factures Factur-X (PDF/A-3 + XML CII embedded)

curl "https://app.comptentra.servolution.fr/api/v1/factures?date_from=2026-04-01&date_to=2026-04-30&format=facturx" \
  -H "X-API-Key: $COMPTENTRA_KEY" \
  -o factures-2026-04.zip

3. Envoyer par email (script Python)

import smtplib, ssl
from email.message import EmailMessage
from pathlib import Path

msg = EmailMessage()
msg["From"] = "compta@monentreprise.fr"
msg["To"] = "cabinet@expert-comptable.fr"
msg["Subject"] = "FEC + factures avril 2026 — MonEntreprise SAS"
msg.set_content("Bonjour,\n\nVeuillez trouver ci-joint :\n- FEC avril 2026\n- Factures Factur-X (zip)\n\nCordialement.")

for f in ["FEC-2026-04.txt", "factures-2026-04.zip"]:
    msg.add_attachment(Path(f).read_bytes(),
                       maintype="application",
                       subtype="octet-stream",
                       filename=f)

with smtplib.SMTP_SSL("smtp.ionos.fr", 465, context=ssl.create_default_context()) as s:
    s.login("compta@monentreprise.fr", os.environ["SMTP_PASSWORD"])
    s.send_message(msg)

4. Variante SFTP (cabinets iSuite Expert / MyUnisoft / ACD)

import paramiko
t = paramiko.Transport(("sftp.cabinet.fr", 22))
t.connect(username="dossier-monentreprise", password=os.environ["SFTP_PWD"])
sftp = paramiko.SFTPClient.from_transport(t)
sftp.put("FEC-2026-04.txt", "/inbound/FEC-2026-04.txt")
sftp.put("factures-2026-04.zip", "/inbound/factures-2026-04.zip")
t.close()

5. Cron mensuel n8n

Schedule trigger : 1er du mois à 06h00 → HTTP Request GET FEC → HTTP Request GET factures → Email node OU SFTP node. Idempotent (ne renvoie pas le mois précédent si déjà envoyé : utilisez la table interne cabinet_envois).

Ressources