nextauthjs/next-auth · error · Error

Sendgrid error: + (await res.text())

Error message

Sendgrid error: + (await res.text())

What it means

The SendGrid email provider in Auth.js throws this error when the SendGrid mail-send API responds with a non-OK status during sendVerificationRequest (the source even carries a 'REVIEW: Clean up error handling' comment). The raw response text — SendGrid's JSON errors array describing each rejection — is appended to the message. It means SendGrid refused the send request.

Source

Thrown at packages/core/src/providers/sendgrid.ts:32

      const { host } = new URL(url)
      const res = await fetch("https://api.sendgrid.com/v3/mail/send", {
        method: "POST",
        headers: {
          Authorization: `Bearer ${provider.apiKey}`,
          "Content-Type": "application/json",
        },
        body: JSON.stringify({
          personalizations: [{ to: [{ email: to }] }],
          from: { email: provider.from },
          subject: `Sign in to ${host}`,
          content: [
            { type: "text/plain", value: text({ url, host }) },
            { type: "text/html", value: html({ url, host, theme }) },
          ],
        }),
      })
      // REVIEW: Clean up error handling
      if (!res.ok) throw new Error("Sendgrid error: " + (await res.text()))
    },
    options: config,
  }
}

View on GitHub (pinned to a1a16a5a77)

Solutions

  1. Read the response text in the error — SendGrid includes a JSON errors array pinpointing the problem.
  2. Verify SENDGRID_API_KEY is set and has Mail Send permission enabled in the SendGrid dashboard.
  3. Verify the 'from' address as a Single Sender or via domain authentication (DNS records).
  4. Check for 403 (scope) vs 401 (bad key) vs 429 (rate limit) codes in the embedded response to pick the right fix.

Example fix

// before
Sendgrid({ apiKey: process.env.SENDGRID_API_KEY, from: "me@personalmail.com" }) // unverified sender
// after
// Verify sender identity in SendGrid first, then:
Sendgrid({ apiKey: process.env.SENDGRID_API_KEY, from: "no-reply@verified-domain.com" })
Defensive patterns

Strategy: validation

Validate before calling

if (!process.env.SENDGRID_API_KEY) throw new Error("SENDGRID_API_KEY is not set")
// Confirm the API key has "Mail Send" scope enabled in the SendGrid dashboard.

Type guard

function isSendGridErrorBody(text: string): boolean {
  try {
    const parsed = JSON.parse(text)
    return Array.isArray(parsed.errors)
  } catch {
    return false
  }
}

Try / catch

try {
  await signIn("email", { email })
} catch (err) {
  const text = (err as Error).message.replace("Sendgrid error: ", "")
  console.error("SendGrid API response:", text) // JSON errors array
}

Prevention

When it happens

Trigger: The fetch to https://api.sendgrid.com/v3/mail/send returns res.ok === false: invalid/missing SENDGRID_API_KEY, 'from' address not a verified sender, request body rejected (validation error), or HTTP 429 for exceeding mail quota. Note SendGrid returns 202 on success with an empty body, so any body text accompanies an error.

Common situations: Developers use an API key without 'Mail Send' permission scope, send from an unverified Single Sender or domain, or run on a free tier hitting daily limits. Deployed environments often miss the SENDGRID_API_KEY env var entirely.

Related errors


AI-assisted analysis of nextauthjs/next-auth@a1a16a5a77 (2026-08-28). Data as JSON: /api/errors/3c269899a796366b. Report an issue: GitHub.