Skip to content

Read RESEND_API_KEY and RESEND_WEBHOOK_SECRET from component env - #165

Merged
convexwill merged 7 commits into
mainfrom
will/api-223-component-env-vars
Dec 4, 2026
Merged

convexwill merged 7 commits into
mainfrom
will/api-223-component-env-vars

Conversation

@convexwill

@convexwill convexwill commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

Part of API-223: move first-party component credentials to component env vars, following expo-push-notifications#89. Resend goes first so we can get feedback on the pattern before Twilio, R2, Polar, LaunchDarkly and WorkOS.

What changes

The component declares two optional env vars, RESEND_API_KEY and RESEND_WEBHOOK_SECRET. Apps bind them in convex/convex.config.ts:

const app = defineApp({
  env: { RESEND_API_KEY: v.string(), RESEND_WEBHOOK_SECRET: v.string() },
});
app.use(resend, {
  env: {
    RESEND_API_KEY: app.env.RESEND_API_KEY,
    RESEND_WEBHOOK_SECRET: app.env.RESEND_WEBHOOK_SECRET,
  },
});

For each value, the component uses:

  1. Its own env var.
  2. Otherwise, the value forwarded by the client: the apiKey / webhookSecret option, or the app's process.env. It logs a deprecation warning when it does this.
  3. If neither is set, it throws RESEND_API_KEY is not set / RESEND_WEBHOOK_SECRET is not set. These replace the client's "API key is not set" and "Webhook secret is not set" checks, since the client can no longer tell whether the component has a value.

API key

callResendAPIWithBatch resolves the key when it sends. sendEmail fails right away if no key is available.

Webhook verification moves into the component

The webhook HTTP route stays in the app. Moving the endpoint into the component is blocked on function-handle support in component env. handleResendEventWebhook no longer verifies the signature itself. It passes the raw body and svix headers to a new component mutation, lib.handleEmailEventWebhook. That mutation verifies with svix and then runs the same logic as lib.handleEmailEvent. svix verifies with a pure-JS HMAC, so it runs inside a mutation. The secret is only a mutation arg; it isn't stored or scheduled.

Compatibility

Nothing breaks on upgrade:

  • Both env vars are optional, so app.use(resend) without env still type-checks and deploys. convex 1.43.0 made env optional in app.use when all of a component's env vars are optional, and the existing ^1.43.0 peer range covers that.
  • ResendOptions.apiKey and ResendOptions.webhookSecret are marked @deprecated and still work as fallbacks. config.webhookSecret keeps its string type.
  • apiKey is now optional in vOptions and in the callResendAPIWithBatch args. Existing lastOptions rows and queued workpool jobs still validate.
  • lib.handleEmailEvent is unchanged.

Until a later major version removes forwarding, the client still sends both values. The component ignores them once its env vars are set.

Out of scope: baseUrl (API-89 / #130).

Testing

  • src/client/index.test.ts, for both the API key and the webhook secret:
    • env only
    • env wins over the client option, with no deprecation warning
    • fallback to the option, with a warning
    • upgrade without binding the env: the client reads the app's process.env and forwards the value, and the component uses it with a warning
    • nothing set rejects
    • plus: a webhook signed with the wrong secret is rejected and the email status is unchanged
  • src/component/upgrade.test.ts covers the data older versions stored, with and without the env bound:
    • an old-shape lastOptions row that includes apiKey
    • a queued callResendAPIWithBatch job with { apiKey, emails } args
    • sends that go through that row and the workpool
  • I checked that these tests fail when the code is broken in each likely way:
    • apiKey dropped from the validators
    • forwarded values ignored
    • a warning logged even when the env wins
  • npm test (50 passing), npm run lint, npm run typecheck, npm run build.
  • _generated files were regenerated with convex codegen and convex dev --once against a local deployment.

Upgrade on a real (local) deployment

  1. Deployed main, then enqueued an email through the old code.
    • The old code wrote lastOptions with apiKey.
    • Its first batch attempt failed. To force this, I pointed the batch URL at a closed port, so the workpool kept a retry with the old { apiKey, emails } args.
  2. Pushed this branch over that data without changing convex.config.ts.
    • The push succeeded.
    • The old retry ran under the new code, fell back to the forwarded key with the warning, and reached api.resend.com. It got a 401 for the fake key, as expected.
  3. Bound both env vars in convex.config.ts.
    • A send with no forwarded key reached Resend using the component env, with no warning.
    • A real signed POST to /resend-webhook returned 201.
    • One signed with the wrong secret returned 500.

🤖 Generated with Claude Code

The component declares an optional RESEND_API_KEY env var and prefers it
over the key forwarded by the client. The client's apiKey option is
deprecated but keeps working as a fallback, with a warning.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@pkg-pr-new

pkg-pr-new Bot commented Dec 4, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/@convex-dev/resend@165

commit: 2541d9a

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 11 files

Reply to a comment to ask cubic a question or push back. It learns from your replies.

Re-trigger cubic

Comment thread src/client/index.test.ts Resolved
Comment thread example/convex/convex.config.ts Resolved
Will Stockwell and others added 2 commits December 3, 2026 16:41
The example declares RESEND_API_KEY as a required app env var, and
example.ts already built a Resend SDK client from it at module load, so
`npm run dev` failed on a fresh deployment before the key was set.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The client's handleResendEventWebhook now passes the raw body and svix
headers to a new component mutation, handleEmailEventWebhook, which
verifies the signature with the component's RESEND_WEBHOOK_SECRET env
var. The client's webhookSecret option is deprecated but keeps working
as a fallback, with a warning.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@convexwill convexwill changed the title Read RESEND_API_KEY from component env Read RESEND_API_KEY and RESEND_WEBHOOK_SECRET from component env Dec 4, 2026

@ianmacartney ianmacartney left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Approving here, with the understanding that you'll respond to the comments I led. If anything seems controversial or you want eyes on another pass, feel free to re-request review. We generally go with a high-trust mode for reviews, where the trust is that you're personally vouching for the code, and the reviewer isn't responsible for testing everything. And we do the eager approve when any follow-on work seems straightforward & doesn't need a performative stamp.
And assume that cubic feedback is ~always worth following up on. if you don't think the bot's feedback is correct, respond to it and explain why. I think it will get smarter over time. I usually try to get to 0 outstanding cubic comments before landing

Comment thread src/component/lib.ts Resolved
Comment thread src/client/index.ts Resolved
Comment thread example/convex/convex.config.ts Resolved
Comment thread README.md Resolved
Comment thread README.md Resolved
- Inline the env var hints in the component's error and warning messages.
- Drop the deprecated option bullets and the migration section from the
  README; the migration notes now live in the changeset.
- Construct the client before stubbing the env in the env-only tests, so
  the client has nothing to forward and the tests check the component env.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 4 files (changes from recent commits).

Tip: Get a Slack DM when cubic finishes reviewing your PRs. Connect Slack

Re-trigger cubic

convexwill and others added 3 commits December 4, 2026 10:02
Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com>
- New src/component/upgrade.test.ts covers data older versions stored:
  a lastOptions row and a queued callResendAPIWithBatch job that carry
  apiKey, with and without the component env bound.
- Client tests cover the old process.env default (no option, env not
  bound) for both the API key and the webhook secret, and assert no
  deprecation warning once the component env wins.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
It's backward compatible; the minor bump (0.3.0) is for removing the
deprecated apiKey and webhookSecret options.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@convexwill
convexwill merged commit eeab711 into main Dec 4, 2026
3 checks passed
@github-actions github-actions Bot mentioned this pull request Dec 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants