# SES Bounce Handling: Hand-Rolled vs Wraps

Source: https://wraps.dev/compare/ses-bounce-handling-hand-rolled-vs-wraps

Every other page in this section compares Wraps to a company. This one compares it to the thing people actually choose instead: **writing it themselves in an afternoon**.

So let's be precise about what that costs. **It is 85 lines of SNS signature verification and 67 lines of handler** — and a competent developer, or a competent coding agent, writes them correctly. The full code is on this page.

Writing it is an afternoon. Running it is every week after that: the topic and the subscription, the queue behind the handler, the two rates AWS judges the account on, the suppression list, the history you go looking through when a customer says the mail never arrived. That second part is what Wraps is for.

## The Honest Line Count

These are real counts from the working implementation below, not an estimate. Nothing is padded to make the number look worse.

| File | Role | Lines | Excl. blanks & comments |
| --- | --- | --- | --- |
| `sns-verify.ts` | SNS signature verification | 85 | 67 |
| `route.ts` | Webhook endpoint + event handling | 67 | 51 |
| `Total` | Everything above | 152 | 118 |

For comparison, the Wraps version of the same handler is **39 lines**, because the signature is a shared secret rather than an asymmetric one. That is a real reduction, and it is also the least interesting thing on this page.

## Here Is the Code. It Is Correct.

This is the part most vendor comparison pages skip, or replace with a strawman. This implementation validates the certificate host, builds the canonical string with the exact key sets SNS signs, handles both signature versions, pins the topic ARN, and caches the certificate. It works.

Signature verification — 85 lines

sns-verify.ts

```
import crypto from "node:crypto";// SNS signs with a cert hosted on an AWS-controlled host. Anything else is// an attacker pointing you at a cert they control.const CERT_HOST = /^sns\.[a-zA-Z0-9-]{3,}\.amazonaws\.com(\.cn)?$/;const certCache = new Map<string, string>();async function fetchCert(url: string): Promise<string> {  const parsed = new URL(url);  if (parsed.protocol !== "https:" || !CERT_HOST.test(parsed.hostname)) {    throw new Error(`Untrusted SigningCertURL host: ${parsed.hostname}`);  }  const cached = certCache.get(url);  if (cached) return cached;  const res = await fetch(url);  if (!res.ok) throw new Error(`Cert fetch failed: ${res.status}`);  const pem = await res.text();  certCache.set(url, pem);  return pem;}// The signed string is a specific key subset, in this exact order, with the// key and value each followed by a newline. Order and membership are not// alphabetical-by-accident — they are fixed by SNS and differ per message type.const SIGNED_KEYS: Record<string, string[]> = {  Notification: [    "Message",    "MessageId",    "Subject",    "Timestamp",    "TopicArn",    "Type",  ],  SubscriptionConfirmation: [    "Message",    "MessageId",    "SubscribeURL",    "Timestamp",    "Token",    "TopicArn",    "Type",  ],  UnsubscribeConfirmation: [    "Message",    "MessageId",    "SubscribeURL",    "Timestamp",    "Token",    "TopicArn",    "Type",  ],};function canonicalString(msg: Record<string, unknown>): string {  const keys = SIGNED_KEYS[msg.Type as string];  if (!keys) throw new Error(`Unknown SNS message type: ${msg.Type}`);  let out = "";  for (const key of keys) {    // Subject is optional; it is omitted from the signed string when absent,    // not included as an empty value.    if (msg[key] === undefined || msg[key] === null) continue;    out += `${key}\n${msg[key]}\n`;  }  return out;}export async function verifySnsMessage(  msg: Record<string, unknown>,  expectedTopicArn: string): Promise<boolean> {  // Pin the topic. A valid signature only proves *some* SNS topic sent this.  if (msg.TopicArn !== expectedTopicArn) return false;  const version = msg.SignatureVersion;  if (version !== "1" && version !== "2") return false;  const algorithm = version === "1" ? "RSA-SHA1" : "RSA-SHA256";  const pem = await fetchCert(String(msg.SigningCertURL));  const verifier = crypto.createVerify(algorithm);  verifier.update(canonicalString(msg), "utf8");  return verifier.verify(pem, String(msg.Signature), "base64");}
```

The handler — 67 lines

app/api/ses-events/route.ts

```
import { type NextRequest, NextResponse } from "next/server";import { verifySnsMessage } from "./sns-verify";const TOPIC_ARN = process.env.SES_EVENTS_TOPIC_ARN!;export async function POST(request: NextRequest) {  // SNS sends text/plain, so request.json() is not guaranteed to work.  const raw = await request.text();  let msg: Record<string, unknown>;  try {    msg = JSON.parse(raw);  } catch {    return new NextResponse("Bad JSON", { status: 400 });  }  if (!(await verifySnsMessage(msg, TOPIC_ARN))) {    return new NextResponse("Invalid signature", { status: 403 });  }  // A new subscription is dead until you GET the SubscribeURL. Miss this and  // the endpoint silently receives nothing.  if (msg.Type === "SubscriptionConfirmation") {    await fetch(String(msg.SubscribeURL));    return new NextResponse("OK", { status: 200 });  }  if (msg.Type !== "Notification") {    return new NextResponse("OK", { status: 200 });  }  // The SES event is a JSON string nested inside the SNS envelope.  const event = JSON.parse(String(msg.Message));  switch (event.eventType ?? event.notificationType) {    case "Bounce": {      const { bounceType, bouncedRecipients } = event.bounce;      for (const r of bouncedRecipients) {        if (bounceType === "Permanent") {          await db.contacts.update({            where: { email: r.emailAddress },            data: { bounced: true, bouncedAt: new Date() },          });        }      }      break;    }    case "Complaint": {      for (const r of event.complaint.complainedRecipients) {        await db.contacts.update({          where: { email: r.emailAddress },          data: { unsubscribed: true, unsubscribedAt: new Date() },        });      }      break;    }    default:      break;  }  return new NextResponse("OK", { status: 200 });}
```

**One caveat, in fairness to both sides.** Node's `crypto.verify()` checks the signature against the certificate you hand it — it does not validate that certificate's chain of trust. The host check above is what carries that weight, which is why it matters more than it looks. AWS's own guidance is to confirm the chain as well, and the official [aws-js-sns-message-validator](https://github.com/aws/aws-js-sns-message-validator) is the safer default if you go this route. It is AWS's own library, and at that one job it is better than the code above.

## What the 152 Lines Don't Cover

The argument for Wraps was never "your signature verification will be wrong." It's that signature verification is the part of this problem with a clean, well-documented answer — and it's roughly the only part. Everything below has no snippet to copy.

The code assumes infrastructure that doesn't exist yet

`SES_EVENTS_TOPIC_ARN` implies an SNS topic, a configuration set with an event destination pointed at it, an HTTPS subscription, and an IAM policy letting SES publish. That's console clicks you'll forget, or another 60-odd lines of Terraform you now maintain. The handler is the visible tip of the work.

When your endpoint is down, the events are gone

SNS retries an HTTPS endpoint on its own schedule and then stops. There is no queue in front of your handler and no dead-letter queue behind it, so a deploy window or a database blip drops events with no record that they arrived. Nothing reconciles afterwards, so an address you were told about stays on your list.

Somebody still has to watch the account

The handler records events. It never reads them back. AWS can place an account under review above a 5% bounce rate and can pause sending at 10%; for complaints the lines are 0.1% and 0.5%. Both are account-wide averages that take real volume to pull back down, so someone has to open the SES console and look. On Wraps that is the control plane's job. A sweep runs every hour, reads both rates against AWS's own review and pause lines, and notifies the organization's owners and admins when an account crosses one — once a day per account, not once an hour.

The same deploy can also put CloudWatch alarms in your own account, set below AWS's lines on purpose so there is lead time: bounce warning at 2% and critical at 4%, complaint warning at 0.05% and critical at 0.08%, and one on any dead-lettered message. Those are on by default for the Production and Enterprise presets and off for Starter, so a starter deploy is not alarmed. They are also the half you keep — alarms are resources in your account, not a dashboard you log into.

The event model has sharp edges the happy path hides

All of these are documented AWS behavior, and all of them break code that looks correct:

-   A Delivery event can be followed by a Bounce for the same message — status is a log, not a state machine.
-   Permanent bounces with subtype `OnAccountSuppressionList` mean SES never tried. They don't count toward your bounce rate, and the handler above treats them as fresh news.
-   Transient bounces need a counter and a threshold, not a suppression. The handler above ignores them entirely.
-   SES gives no ordering or batching guarantees. One notification may cover many recipients, or many notifications may cover one.

## The Same Handler, on Wraps

Wraps delivers events through EventBridge to your endpoint with a shared secret header, so there is no envelope to unwrap, no certificate to fetch, and no subscription handshake. The queue, the dead-letter queue, and the event history sit behind it, all deployed into your AWS account by one command, and the control plane watches the account from there.

Hand-rolled — 152 lines across 2 files

sns-verify.ts + route.ts

```
import crypto from "node:crypto";// SNS signs with a cert hosted on an AWS-controlled host. Anything else is// an attacker pointing you at a cert they control.const CERT_HOST = /^sns\.[a-zA-Z0-9-]{3,}\.amazonaws\.com(\.cn)?$/;const certCache = new Map<string, string>();async function fetchCert(url: string): Promise<string> {  const parsed = new URL(url);  if (parsed.protocol !== "https:" || !CERT_HOST.test(parsed.hostname)) {    throw new Error(`Untrusted SigningCertURL host: ${parsed.hostname}`);  }  const cached = certCache.get(url);  if (cached) return cached;  const res = await fetch(url);  if (!res.ok) throw new Error(`Cert fetch failed: ${res.status}`);  const pem = await res.text();  certCache.set(url, pem);  return pem;}// The signed string is a specific key subset, in this exact order, with the// key and value each followed by a newline. Order and membership are not// alphabetical-by-accident — they are fixed by SNS and differ per message type.const SIGNED_KEYS: Record<string, string[]> = {  Notification: [    "Message",    "MessageId",    "Subject",    "Timestamp",    "TopicArn",    "Type",  ],  SubscriptionConfirmation: [    "Message",    "MessageId",    "SubscribeURL",    "Timestamp",    "Token",    "TopicArn",    "Type",  ],  UnsubscribeConfirmation: [    "Message",    "MessageId",    "SubscribeURL",    "Timestamp",    "Token",    "TopicArn",    "Type",  ],};function canonicalString(msg: Record<string, unknown>): string {  const keys = SIGNED_KEYS[msg.Type as string];  if (!keys) throw new Error(`Unknown SNS message type: ${msg.Type}`);  let out = "";  for (const key of keys) {    // Subject is optional; it is omitted from the signed string when absent,    // not included as an empty value.    if (msg[key] === undefined || msg[key] === null) continue;    out += `${key}\n${msg[key]}\n`;  }  return out;}export async function verifySnsMessage(  msg: Record<string, unknown>,  expectedTopicArn: string): Promise<boolean> {  // Pin the topic. A valid signature only proves *some* SNS topic sent this.  if (msg.TopicArn !== expectedTopicArn) return false;  const version = msg.SignatureVersion;  if (version !== "1" && version !== "2") return false;  const algorithm = version === "1" ? "RSA-SHA1" : "RSA-SHA256";  const pem = await fetchCert(String(msg.SigningCertURL));  const verifier = crypto.createVerify(algorithm);  verifier.update(canonicalString(msg), "utf8");  return verifier.verify(pem, String(msg.Signature), "base64");}// ...plus the 67-line route handler shown above.
```

With Wraps — 39 lines

app/api/webhooks/email/route.ts

```
import crypto from "node:crypto";import { type NextRequest, NextResponse } from "next/server";const SECRET = process.env.WRAPS_WEBHOOK_SECRET!;export async function POST(request: NextRequest) {  const sent = Buffer.from(request.headers.get("x-wraps-signature") ?? "");  const expected = Buffer.from(SECRET);  // timingSafeEqual throws when the buffers differ in length — check first,  // or a short header is a 500 instead of a 401.  if (    sent.length !== expected.length ||    !crypto.timingSafeEqual(sent, expected)  ) {    return NextResponse.json({ error: "Unauthorized" }, { status: 401 });  }  const { event, detail } = await request.json();  if (event === "Bounce" && detail.bounce.bounceType === "Permanent") {    for (const r of detail.bounce.bouncedRecipients) {      await db.contacts.update({        where: { email: r.emailAddress },        data: { bounced: true, bouncedAt: new Date() },      });    }  }  if (event === "Complaint") {    for (const r of detail.complaint.complainedRecipients) {      await db.contacts.update({        where: { email: r.emailAddress },        data: { unsubscribed: true, unsubscribedAt: new Date() },      });    }  }  return NextResponse.json({ received: true });}
```

**Worth naming: the hand-rolled version's signature check is asymmetric, and ours is a shared secret.** SNS proves the message came from AWS using a certificate you can verify. Wraps proves it came from your EventBridge destination using a secret both sides hold. Both are sound over HTTPS, and the shared secret is what makes the handler short — but if asymmetric verification is a hard requirement in your threat model, the SNS path is the one to take.

## Line by Line, Then Everything Else

### The part the code covers

| Capability | Hand-rolled | Wraps |
| --- | --- | --- |
| SNS signature verification | 
85 lines, correct

 | 

EventBridge + shared secret

 |
| Subscription confirmation | 

One fetch call

 | 

No subscription handshake at all

 |
| Parse bounce/complaint payloads | 

Straightforward JSON

 | 

Same payload, flattened

 |

### The part the code doesn't cover

| Capability | Hand-rolled | Wraps |
| --- | --- | --- |
| SNS topic, subscription, IAM policy | 
Console clicks or ~60 lines of IaC

 | 

Deployed by wraps email init

 |
| Retry + dead-letter queue on handler failure | 

SNS retries, then drops silently

 | 

SQS + DLQ in your AWS account

 |
| Bounce and complaint rates watched | 

Nothing reads the events back

 | 

Hourly sweep; owners and admins notified

 |
| CloudWatch alarms below AWS's thresholds | 

Build and tune them yourself

 | 

On for the Production and Enterprise presets; off for Starter

 |
| Queryable event history | 

Your DB, your schema, your retention

 | 

DynamoDB in your account + events API

 |
| Suppression list you can act on | 

Raw SESv2 API calls

 | 

Browse and clear from the dashboard

 |
| Deliverability and blacklist audit | 

Assemble the DNS and blacklist checks yourself

 | 

wraps email check, on demand

 |
| Open/click tracking over HTTPS | 

Config set + ACM + CloudFront

 | 

Deployed with the stack

 |
| Clean teardown | 

Manual resource deletion

 | 

wraps email destroy

 |

\= built-in or included, \= possible but requires manual setup, \= not available

## Still Your Job, Even on Wraps

Wraps does not make bounce handling disappear. These stay yours either way:

-   Deciding your soft-bounce threshold and writing the counter behind it
-   Keeping your own unsubscribe list in sync with the SES suppression list
-   Making your handler idempotent — events can arrive more than once
-   Requesting SES production access. Wraps detects the sandbox, explains it, and links the request; the approval is AWS's call

The [bounce handling guide](https://wraps.dev/docs/guides/bounce-handling) covers each of these, and applies whether or not you use Wraps.

## When to Hand-Roll It

There is a real case for it.

-   You only need bounce and complaint events, and you already have an SNS topic wired up
-   You have an existing queue, retry, and alerting stack that new events can plug into
-   Your team already owns CloudWatch alarms and a metrics pipeline you trust
-   You want zero additional vendors in the path, and are willing to own the operational surface
-   The 152 lines are genuinely the whole job for your use case — some apps are that simple

## When to Use Wraps

-   You want the surrounding infrastructure — queue, dead-letter queue, event history — deployed rather than assembled
-   You want the two rates AWS judges you on read against its own lines every hour, with your owners and admins told
-   You want CloudWatch alarms at 2% and 4% bounce sitting in your own account — the Production and Enterprise presets deploy them, Starter does not
-   You want per-message event history you can query without designing a schema for it
-   You would rather your team's next stretch of work go to product than to email plumbing
-   You still want to own everything: it all deploys into your AWS account and can be torn down

## Try It Against Your Own Numbers

$ npx @wraps.dev/cli email init

Everything deploys into your AWS account, namespaced `wraps-email-*`, and `wraps email destroy` removes exactly what was created. If you decide the 152 lines were the better trade, you can leave without unpicking anything.

## Also Compare

[

Resend vs Wraps

Same DX, different economics

](https://wraps.dev/compare/resend-vs-wraps)[

Amazon SES vs Wraps

Same infrastructure, better DX

](https://wraps.dev/compare/amazon-ses-vs-wraps)[

SendGrid vs Wraps

Escape the legacy tax

](https://wraps.dev/compare/sendgrid-vs-wraps)[

Customer.io vs Wraps

Unlimited contacts, no surprise bills

](https://wraps.dev/compare/customer-io-vs-wraps)[

Postmark vs Wraps

Beyond transactional sending

](https://wraps.dev/compare/postmark-vs-wraps)[

Klaviyo vs Wraps

Up to 5.6x cheaper at scale

](https://wraps.dev/compare/klaviyo-vs-wraps)[

Mailgun vs Wraps

Your infra, AWS pricing, no suspensions

](https://wraps.dev/compare/mailgun-vs-wraps)

## The code is the easy part

Deploy the queue, the dead-letter queue, and the event history in one command, into your own AWS account, and let the control plane run it from there.

[Get Started](https://wraps.dev/docs/quickstart/email)[Read the Bounce Guide](https://wraps.dev/docs/guides/bounce-handling)

**Last updated:** September 2026. Line counts measured with `wc -l` on the code shown above. SES bounce types, subtypes, reputation thresholds, and mailbox simulator behavior verified against the [Amazon SES Developer Guide](https://docs.aws.amazon.com/ses/latest/dg/notification-contents.html). SNS signing behavior verified against the [Amazon SNS Developer Guide](https://docs.aws.amazon.com/sns/latest/dg/sns-verify-signature-of-message.html).

If anything here is wrong — especially the code — tell us at [support@wraps.dev](mailto:support@wraps.dev) and we'll fix it on this page.
