Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

fix(lib): build the Resend client lazily so a missing key is not fatal

Sidwebworks0d5a286b9f8f
diff --git a/packages/jobs/src/inngest/functions/tasks/onboard.ts b/packages/jobs/src/inngest/functions/tasks/onboard.ts
index e76288bac7..60b7bebe16 100644
--- a/packages/jobs/src/inngest/functions/tasks/onboard.ts
+++ b/packages/jobs/src/inngest/functions/tasks/onboard.ts
@@ -3,7 +3,7 @@ import { getCarbonServiceRole } from "@carbon/auth/client.server";
import type { Database } from "@carbon/database";
import { GetStartedEmail, WelcomeEmail } from "@carbon/documents/email";
import { RESEND_DOMAIN } from "@carbon/env";
-import { resend, sendEmail } from "@carbon/lib/resend.server";
+import { getResend, sendEmail } from "@carbon/lib/resend.server";
import { getSlackClient } from "@carbon/lib/slack.server";
import { getTwentyClient } from "@carbon/lib/twenty.server";
import { render } from "@react-email/components";
@@ -54,17 +54,27 @@ export const onboardFunction = inngest.createFunction(
companyId
);
- try {
- await resend.contacts.create({
- email: user.email,
- firstName: user.firstName,
- lastName: user.lastName,
- unsubscribed: false,
- audienceId: process.env.RESEND_AUDIENCE_ID!
- });
- logger.info("Successfully created resend contact for:", user.email);
- } catch (error) {
- logger.error("Error creating resend contact", error);
+ // Skipped rather than attempted when Resend is not configured:
+ // a self-hosted install has no marketing audience to add anyone
+ // to, and onboarding should not depend on someone else's SaaS.
+ const resend = getResend();
+ const audienceId = process.env.RESEND_AUDIENCE_ID;
+ if (resend && audienceId) {
+ try {
+ await resend.contacts.create({
+ email: user.email,
+ firstName: user.firstName,
+ lastName: user.lastName,
+ unsubscribed: false,
+ audienceId
+ });
+ logger.info(
+ "Successfully created resend contact for:",
+ user.email
+ );
+ } catch (error) {
+ logger.error("Error creating resend contact", error);
+ }
}
});
diff --git a/packages/lib/src/resend.server.ts b/packages/lib/src/resend.server.ts
index 0521ff189e..e3b580642b 100644
--- a/packages/lib/src/resend.server.ts
+++ b/packages/lib/src/resend.server.ts
@@ -8,12 +8,50 @@ import { Resend } from "resend";
const log = getLogger("lib", "resend");
-export const resend = new Resend(process.env.RESEND_API_KEY!);
+/**
+ * Built on first send, not at import.
+ *
+ * This was `export const resend = new Resend(process.env.RESEND_API_KEY!)`
+ * at module scope, and Resend's constructor throws when the key is
+ * missing. Every route bundle that imports `sendEmail` therefore evaluated
+ * it at boot, so a deployment with no RESEND_API_KEY did not lose email —
+ * it lost the whole server, crash-looping on "Missing API key" before it
+ * served a request. The `!` was the tell: it asserted a value that
+ * genuinely is absent in a self-hosted install.
+ *
+ * The DISABLE_RESEND guard below could never help, because the throw
+ * happened at import time, long before anything called sendEmail.
+ */
+let client: Resend | undefined;
+
+export const getResend = (): Resend | undefined => {
+ const key = process.env.RESEND_API_KEY;
+ if (!key) return undefined;
+ if (!client) client = new Resend(key);
+ return client;
+};
export const sendEmail = async (
payload: CreateEmailOptions,
options?: CreateEmailRequestOptions
): Promise<CreateEmailResponse> => {
+ const resend = getResend();
+
+ // No key configured is the same outcome as DISABLE_RESEND, and
+ // deliberately not an error: a self-hosted deployment that never set one
+ // has opted out of email, and inviting a user should not fail with a
+ // stack trace about someone else's SaaS.
+ if (!resend) {
+ log.debug("Email send skipped (no RESEND_API_KEY)", {
+ to: payload.to,
+ subject: payload.subject
+ });
+ return {
+ error: null,
+ data: null
+ };
+ }
+
if (process.env.DISABLE_RESEND) {
// Log only non-sensitive metadata — the full payload carries recipient PII
// and the rendered HTML body (which can include verification codes).