Skip to content

Add optional baseUrl to ResendOptions for Resend-compatible APIs - #130

Closed
heracek wants to merge 2 commits into
convex:mainfrom
heracek:feat/base-url-option
Closed

heracek wants to merge 2 commits into
convex:mainfrom
heracek:feat/base-url-option

Conversation

@heracek

@heracek heracek commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

🤖 This PR was generated by Claude Opus 5.5 on behalf of @heracek.

Summary

Adds an optional baseUrl to ResendOptions, so queued batches can go to a Resend-compatible API other than https://api.resend.com. Unset, nothing changes.

 new Resend(components.resend, { apiKey, baseUrl })
   sendEmail                         # options stored in lastOptions, baseUrl included
     makeBatch
       callResendAPIWithBatch({ apiKey, baseUrl, emails })
-        fetch("https://api.resend.com/emails/batch")
+        fetch(`${baseUrl?.replace(/\/+$/, "") ?? "https://api.resend.com"}/emails/batch`)

baseUrl follows apiKey everywhere: vOptions, the makeBatch → callResendAPIWithBatch workpool args, and the generated ComponentApi. Docstring and README list it.

Why

We run browser end-to-end tests against a disposable local stack: a Convex local backend on loopback, our functions deployed to it, and @emulators/resend (on @emulators/core) as a fake Resend HTTP server. The tests read sign-up OTPs and notification emails from the emulator's GET /emails. Nothing leaves the machine, and CI needs no Resend credentials.

Both of our send paths have to reach the emulator:

  • sendEmailManually: covered already, since the resend SDK has a public baseUrl option.
  • sendEmail (queued batches): the component hard-codes https://api.resend.com/emails/batch. Because it ships bundled from dist/, an app can't redirect it. There's no bundler alias hook. Mounting the component conditionally breaks the committed _generated types. A MITM proxy or /etc/hosts change is far heavier than this option.

So we've carried this as a pnpm patch against dist/ since 0.2.8 and re-rolled it on every release. This PR is that patch, moved into src/.

Design choices

  • Explicit only, no environment fallback. The resend SDK falls back to RESEND_BASE_URL. This option deliberately doesn't, so a stray environment variable can't silently send production mail somewhere else. The app decides where mail goes. We, for example, accept only a loopback emulator URL, and only on a loopback Convex backend. Easy to add a fallback if you'd prefer parity with the SDK.
  • Stored with the other options. baseUrl lives in lastOptions and in the workpool action's args, like apiKey, so retries use the destination that was current when the batch was made.
  • Trailing slashes stripped. http://127.0.0.1:4100/ and http://127.0.0.1:4100 behave the same; otherwise the request goes to //emails/batch, which @emulators/resend answers with 404.
  • Batch endpoint only. It's the only Resend URL the component calls. Webhooks are unaffected.

I left CHANGELOG.md alone since versions look maintainer-managed. Happy to add an entry.

Evidence

New test in src/client/index.test.ts, sends batches to the configured baseUrl %s, run with and without a trailing slash:

resend = new Resend(component, { apiKey, baseUrl: "http://127.0.0.1:4100" or "http://127.0.0.1:4100/" })
sendEmail(...); finishAllScheduledFunctions()
expect email status "sent"
expect fetch called once with "http://127.0.0.1:4100/emails/batch"
  • Before (test against main's src/): the option is silently dropped.
    FAIL  sends batches to the configured baseUrl
    -   "http://127.0.0.1:4100/emails/batch",
    +   "https://api.resend.com/emails/batch",
    
    After: passes. npm run build, typecheck, lint and test (35/35) are green. The existing test still asserts the default https://api.resend.com/emails/batch.
  • The same change, as a patch against 0.2.8 to 0.2.10, has been sending our e2e suite's batch mail to @emulators/resend.

src/component/_generated/component.ts was edited by hand: convex codegen --component-dir needs a deployment. The edit is a single baseUrl?: string; line, in codegen's alphabetical order.

Merge Danger

Door: two-way

Opt-in and additive. Existing lastOptions rows and queued workpool jobs have no baseUrl and keep using https://api.resend.com. One caveat: if a deployment has stored a baseUrl and then downgrades the component, its lastOptions row no longer matches the old schema until it's rewritten.

Blast Radius: opt-in

Only apps that pass baseUrl see different behaviour. The public type gains one optional field.

@heracek
heracek marked this pull request as ready for review November 6, 2026 16:02

@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 6 files

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

Re-trigger cubic

Comment thread src/component/lib.ts Resolved
@pkg-pr-new

pkg-pr-new Bot commented Nov 9, 2026

Copy link
Copy Markdown

Open in StackBlitz

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

commit: c3dcdfc

@convexwill

convexwill commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

We've been moving the component's config to component env vars (#165), so we took your change in that direction in #168: an optional RESEND_BASE_URL component env var instead of a baseUrl option. You're credited as co-author.

For your emulator setup, declare and bind it in convex/convex.config.ts:

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

Then set RESEND_BASE_URL only on your local deployment. On your concern about a stray env var redirecting production mail: the component only sees RESEND_BASE_URL if convex.config.ts binds it, and deployments that don't set it keep sending to https://api.resend.com. The resend SDK reads the same variable, so sendEmailManually goes to the emulator too. Once #168 ships, you should be able to drop the pnpm patch.

convexwill added a commit that referenced this pull request Dec 4, 2026
* Add optional RESEND_BASE_URL component env var

Lets the component send batches to a Resend-compatible API, such as a
local emulator in end-to-end tests, instead of https://api.resend.com.
Based on #130, which added a baseUrl client option; this reads it from
component env instead, so the app opts in by binding it in
convex.config.ts. Trailing slashes are stripped.

Co-authored-by: heracek <tomas.horacek@gmail.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Show the app env declaration in the RESEND_BASE_URL changeset

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

* Format the RESEND_BASE_URL changeset

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

---------

Co-authored-by: Will Stockwell <will@Wills-MacBook-Pro.local>
Co-authored-by: heracek <tomas.horacek@gmail.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
convexwill added a commit that referenced this pull request Dec 5, 2026
* Add optional RESEND_BASE_URL component env var

Lets the component send batches to a Resend-compatible API, such as a
local emulator in end-to-end tests, instead of https://api.resend.com.
Based on #130, which added a baseUrl client option; this reads it from
component env instead, so the app opts in by binding it in
convex.config.ts. Trailing slashes are stripped.

Co-authored-by: heracek <tomas.horacek@gmail.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Show the app env declaration in the RESEND_BASE_URL changeset

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

* Format the RESEND_BASE_URL changeset

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

* Declare RESEND_WEBHOOK_SECRET optional in the README

Deployments that don't receive webhooks shouldn't need the secret to
deploy. Matches the example app and the changeset.

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

* Bind RESEND_BASE_URL in the example app config

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

* Add PR numbers to the changesets

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

* Note that RESEND_BASE_URL should omit the trailing slash

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

* Revert "Note that RESEND_BASE_URL should omit the trailing slash"

The component supports a trailing slash. The resend SDK is the app's own
client, outside what this component controls.

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

* Drop the resend SDK note from the RESEND_BASE_URL docs

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

* Document that the resend SDK also reads RESEND_BASE_URL

The component strips a trailing slash, but the SDK appends paths to the
value as is, so manual sends need it without one.

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

* Regenerate example code for RESEND_BASE_URL

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

---------

Co-authored-by: Will Stockwell <will@Wills-MacBook-Pro.local>
Co-authored-by: heracek <tomas.horacek@gmail.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
@heracek

heracek commented Dec 5, 2026

Copy link
Copy Markdown
Contributor Author

Closing as #169 implemented this in a better way. Thanks!

@heracek heracek closed this 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