Guide

Idempotenza (retry sicuri)

Ripeti qualsiasi invio senza mandare l'email due volte. Una key per email, riusata a ogni retry.

  • Idempotency-Key
  • idempotency_key
  • 24 h

Cosa risolve

La richiesta è andata in timeout. L'email è partita? Riprova con la stessa key e ottieni il risultato originale: niente viene inviato due volte.

Come usarla

Invia l'header Idempotency-Key: <la tua key> su POST /v3.0/send.

POST /v3.0/send
Authorization: <api_key>
Idempotency-Key: 3f1c9a52-6b1e-4f0e-9c2a-8d7e5b4a1f00
Content-Type: application/json

Regole

Formato della key

1–255 caratteri: lettere, numeri, _ - : . Un UUID è perfetto.

Valida 24 ore

Dopo 24 ore la stessa key conta come nuova.

Una key per email

Riusa una key solo per ripetere esattamente la stessa chiamata.

Stessa key, chiamata diversa

La stessa key con body o args diversi restituisce IDEMPOTENCY_KEY_REUSED (422 su /v3.0/send).

La prima chiamata è ancora in corso

Restituisce IDEMPOTENCY_IN_PROGRESS (409 con Retry-After: 1 su /v3.0/send). Attendi 1 secondo e riprova.

Si salvano solo i successi

Se la chiamata è fallita, un retry con la stessa key viene eseguito davvero di nuovo.

Come riconoscere un replay

Header Idempotent-Replayed: true su /v3.0/send, oppure idempotent_replayed: true in data nella webmail API.

Esempi di codice

Ogni esempio crea una key e la mantiene a ogni retry, così un timeout non invia mai l'email due volte.

POST /v3.0/send
KEY=$(uuidgen)   # one key per email, reused on every retry

for attempt in 1 2 3 4 5; do
  status=$(curl -s -o response.json -w '%{http_code}' --max-time 15 \
    https://mail.sendcorex.com/v3.0/send \
    -H "Authorization: $SENDCOREX_API_KEY" \
    -H "Idempotency-Key: $KEY" \
    -H 'Content-Type: application/json' \
    -d '{"to":"user@example.com","from":"noreply@yourdomain.com","senderName":"My App","subject":"Your receipt","body":"<p>Thanks for your order.</p>"}')

  case "$status" in
    000|409|429|5*) sleep $((2 ** (attempt - 1))) ;;   # timeout or busy: retry with the SAME key
    *) break ;;
  esac
done

cat response.json

Per gli agenti IA

Invia sempre una idempotency key. Se una chiamata va in timeout, riprova con la stessa key. L'email non viene mai inviata due volte.

Errori correlati