Skip to content

Read RESEND_API_KEY from component env - #164

Closed
convexwill wants to merge 1 commit into
mainfrom
api-223-component-env-vars
Closed

convexwill wants to merge 1 commit into
mainfrom
api-223-component-env-vars

Conversation

@convexwill

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 an optional RESEND_API_KEY env var. Apps bind it in convex/convex.config.ts:

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

callResendAPIWithBatch resolves the key in this order:

  1. The component's RESEND_API_KEY env var.
  2. The key forwarded by the client (apiKey option, or the app's process.env.RESEND_API_KEY). The component uses it and logs a deprecation warning.
  3. Neither set: sendEmail fails right away with RESEND_API_KEY is not set. This replaces the client-side "API key is not set" check, since the client can no longer tell whether the component has a key.

Compatibility

Nothing breaks on upgrade:

  • The env var is 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 is marked @deprecated and still works as a fallback.
  • apiKey is now optional in vOptions and in the callResendAPIWithBatch args. Existing lastOptions rows and queued workpool jobs still validate.

Until a later major version removes forwarding, the client still sends the key through sendEmail args and lastOptions. The component ignores that copy once its env var is set.

Out of scope: RESEND_WEBHOOK_SECRET (the app's HTTP action verifies webhooks) and baseUrl (API-89 / #130).

Testing

  • New tests in src/client/index.test.ts:
    • env only
    • env wins over the apiKey option
    • fallback with a warning
    • no key anywhere rejects sendEmail
  • The existing test now also asserts the Authorization header.
  • npm test (37 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.
  • Pushed the example to a local deployment with the env bound. Also pushed it with a plain app.use(resend) to check backward compatibility.

🤖 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>
@convexwill convexwill closed this Dec 4, 2026
@convexwill
convexwill deleted the api-223-component-env-vars branch December 4, 2026 00:28
@convexwill

Copy link
Copy Markdown
Contributor Author

Superseded by #165 (same commit, branch renamed to will/api-223-component-env-vars).

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.

1 participant