Späw Documentation
Ressources

Webhooks

Un webhook est un appel de Späw vers votre serveur, dès qu'un événement se produit. C'est le moyen fiable de savoir qu'un prélèvement est payé, rejeté ou contesté.

Ajouter un point de terminaison

POST/v1/webhook_endpoints

Le secret de signature n'est renvoyé qu'à la création. Jusqu'à 10 points de terminaison actifs.

Paramètres

urltexteobligatoire
Adresse https:// de votre serveur.
eventsliste
Types d'événements à recevoir. Tous si absent.
curl -X POST https://xn--spw-rla.com/v1/webhook_endpoints \
  -H "Authorization: Bearer spaw_sk_VOTRE_CLE" \
  -H "Content-Type: application/json" \
  -d '{
    "url": "https://votre-site.fr/webhooks/spaw",
    "events": [
      "payment.paid",
      "payment.failed"
    ]
  }'
const res = await fetch('https://xn--spw-rla.com/v1/webhook_endpoints', {
  method: 'POST',
  headers: {
    Authorization: `Bearer ${process.env.SPAW_SECRET_KEY}`,
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    "url": "https://votre-site.fr/webhooks/spaw",
    "events": [
      "payment.paid",
      "payment.failed"
    ]
  }),
});
const data = await res.json();
Réponse · 201
{
  "id": "we_2",
  "object": "webhook_endpoint",
  "url": "https://votre-site.fr/webhooks/spaw",
  "events": [
    "payment.paid",
    "payment.failed"
  ],
  "created": "2026-11-16T09:20:00.000Z",
  "secret": "spaw_whsec_…"
}

Recevoir un événement

Späw envoie une requête POST en JSON : l'événement, avec l'objet concerné dans data.object. Répondez par un code 2xx en moins de 10 secondes. Sinon l'envoi est renouvelé après 1 min, 5 min, 30 min, 2 h, 6 h puis 24 h, avant d'être abandonné.

Un même événement peut arriver deux fois, ou dans le désordre : appuyez-vous sur son id pour ignorer les doublons, et relisez l'objet par l'API en cas de doute.

Réponse
{
  "id": "evt_911",
  "object": "event",
  "type": "payment.paid",
  "created": "2026-11-19T06:02:10.000Z",
  "data": {
    "object": {
      "id": "pay_208",
      "object": "payment",
      "customer": "cus_42",
      "mandate": "mdt_17",
      "subscription": "sub_31",
      "amount": 4900,
      "fee": 74,
      "net": 4826,
      "currency": "eur",
      "status": "paid",
      "description": "Abonnement Pro — échéance du 16 nov. 2026",
      "failure_reason": null,
      "period_date": "2026-11-16",
      "provider": "gocardless",
      "created": "2026-11-16T09:13:05.000Z",
      "updated": "2026-11-16T09:13:05.000Z"
    }
  }
}

Vérifier la signature

Chaque envoi porte un en-tête Spaw-Signature: t=<horodatage>,v1=<signature>. La signature est le HMAC-SHA256, en hexadécimal, de horodatage + "." + corps brut, calculé avec le secret du point de terminaison. Recalculez-la sur le corps brut (avant tout décodage JSON), comparez-la en temps constant, et refusez un horodatage vieux de plus de 5 minutes.

const crypto = require('crypto');
const express = require('express');
const app = express();

app.post('/webhooks/spaw', express.raw({ type: 'application/json' }), (req, res) => {
  const header = req.get('Spaw-Signature') || '';
  const match = /^t=(\d+),v1=([0-9a-f]{64})$/.exec(header);
  if (!match) return res.status(400).end();
  const [, timestamp, signature] = match;

  const expected = crypto
    .createHmac('sha256', process.env.SPAW_WEBHOOK_SECRET)
    .update(`${timestamp}.${req.body}`)
    .digest('hex');
  const valid = crypto.timingSafeEqual(Buffer.from(expected), Buffer.from(signature));
  const fresh = Math.abs(Date.now() / 1000 - Number(timestamp)) < 300;
  if (!valid || !fresh) return res.status(400).end();

  const event = JSON.parse(req.body);
  if (event.type === 'payment.paid') {
    // marquer la facture comme réglée…
  }
  res.status(204).end();
});

Lister les points de terminaison

GET/v1/webhook_endpoints
curl https://xn--spw-rla.com/v1/webhook_endpoints \
  -H "Authorization: Bearer spaw_sk_VOTRE_CLE"
const res = await fetch('https://xn--spw-rla.com/v1/webhook_endpoints', {
  method: 'GET',
  headers: {
    Authorization: `Bearer ${process.env.SPAW_SECRET_KEY}`,
  },
});
const data = await res.json();
Réponse · 200
{
  "object": "list",
  "data": [
    {
      "id": "we_2",
      "object": "webhook_endpoint",
      "url": "https://votre-site.fr/webhooks/spaw",
      "events": [
        "payment.paid",
        "payment.failed"
      ],
      "created": "2026-11-16T09:20:00.000Z"
    }
  ],
  "has_more": false
}

Supprimer un point de terminaison

DELETE/v1/webhook_endpoints/:id
curl -X DELETE https://xn--spw-rla.com/v1/webhook_endpoints/we_2 \
  -H "Authorization: Bearer spaw_sk_VOTRE_CLE"
const res = await fetch('https://xn--spw-rla.com/v1/webhook_endpoints/we_2', {
  method: 'DELETE',
  headers: {
    Authorization: `Bearer ${process.env.SPAW_SECRET_KEY}`,
  },
});
const data = await res.json();
Réponse · 200
{
  "id": "we_2",
  "object": "webhook_endpoint",
  "deleted": true
}