nsindocs Search the docs⌘K
  1. Docs
  2. API reference
  3. Email Routing

Mail arrival delivered to your webhook

postYour webhook URL

What NSIN sends to the mail webhook URL saved for a domain in the panel (Email Routing → More options → Webhook): one mail.stored when a message is kept in the inbox and one mail.forwarded when one is forwarded. test comes from the panel's Send test button. Metadata only — never the body or attachments. For stored mail, open it with GET /domains/{domain}/email-routing/inbox/{inbox_message_id} and a key that has email:read.

Verify every request. X-Nsin-Signature is sha256=<hex HMAC-SHA256> of <X-Nsin-Timestamp>.<raw body>, keyed with the webhook's signing secret (shown in the panel). Reject a timestamp older than a few minutes to stop replays, and use event_id to drop a repeat.

python
import hashlib, hmac, time

def verify(secret: str, headers, body: bytes) -> bool:
    ts = headers["X-Nsin-Timestamp"]
    if abs(time.time() - int(ts)) > 300:
        return False
    mac = hmac.new(secret.encode(), ts.encode() + b"." + body, hashlib.sha256)
    return hmac.compare_digest("sha256=" + mac.hexdigest(), headers["X-Nsin-Signature"])

Answer with any 2xx within 5 seconds. Anything else — another status, a timeout, a connection error — is retried four more times, after 30 seconds, 2 minutes, 10 minutes and 1 hour. A test is tried once. The URL must be https://; redirects are not followed, and a URL that resolves to a private, loopback, link-local or metadata address is refused without retrying. At most 60 events per domain per minute are sent; more are skipped and the panel says so. Delivery runs in the background, so events may arrive out of order and a few seconds late.

Header parameters

  • X-Nsin-Eventstringrequiredheader

    The event name, repeated from the body.

    One of: "mail.stored" "mail.forwarded" "test"
  • X-Nsin-Event-Idstringrequiredheader

    The event's id, repeated from the body; the same on every retry.

  • X-Nsin-Timestampstringrequiredheader

    When the request was signed, in unix seconds.

  • X-Nsin-Signaturestringrequiredheader

    sha256= followed by the hex HMAC-SHA256 of <timestamp>.<raw body>.

  • User-Agentstringheader

    Always Nsin-Mail-Webhook/1.

    One of: "Nsin-Mail-Webhook/1"

Request body

Content type: application/json · required

  • eventstringrequired
    One of: "mail.stored" "mail.forwarded" "test"
  • event_idstringrequired

    Unique per event; the same on every retry.

  • domainstringrequired
  • statusstringrequired
    One of: "stored" "forwarded" "test"
  • message_idstring

    The Message-ID header, <…> included.

  • fromstring

    The From header for stored mail; the envelope sender for forwarded mail.

  • mail_fromstring

    The envelope sender.

  • tostring

    The address on your domain the mail was sent to.

  • subjectstring

    Empty for forwarded mail while subject logging is off.

  • sizeinteger

    Size in bytes.

  • received_atstring (date-time)required
  • inbox_message_idinteger | null

    For mail.stored: the id to open with GET /domains/{domain}/email-routing/inbox/{id}. null otherwise.

  • folderstring

    For mail.stored: where it was put.

    One of: "inbox" "spam"
  • txnstring

    The mail transaction id

  • sent_atstring (date-time)required

    When this attempt was sent.

Responses

2XXDelivery accepted. Any other answer counts as a failure and is retried.
esc