Guides · Updated 2026-11-17
SendGrid or Resend API key in my frontend: what to do
Short answer: Yes, it's a real problem. An email API key is the only thing needed to send through your account, so anyone who copies it from your JavaScript can send email from your verified domain: phishing your users, using up your quota and hurting your sender reputation. Delete the key in the provider's dashboard (that revokes it), create a new key with send-only permission, store it only on the server, and send email from a server route that the browser calls.
What you're looking at
- SendGrid (Twilio) API keys start with SG. and look like SG.xxxxxxxx.yyyyyyyy.
- Resend API keys start with re_ (for example re_c1tpEyD8_...).
- Both are secret keys. SendGrid's docs say to treat the key like a password and never include it in your code. Resend's docs say not to expose it in the browser or other client-side code.
- This is different from public keys that are meant to be in the browser, like a Supabase anon/publishable key or a Stripe pk_ key. There is no public version of an email-sending key.
Why it matters
Resend's own guide on leaked keys puts it plainly: an API key is the only credential needed to send through your account. Anyone holding it can send from any domain you've verified, spend your monthly quota and damage the sending reputation you've built. The same is true for a SendGrid key with Mail Send access, and a Full Access SendGrid key can also change account settings.
- Phishing that really comes from your domain: password-reset lookalikes, fake invoices, "your account is locked" emails. They pass the checks that normally catch spoofed mail, because they are sent by your own account.
- Your domain lands in spam: providers react to spam complaints and bounces. Resend asks every account to stay under a 4% bounce rate and a 0.08% spam rate and may pause sending above that, and rebuilding domain reputation can take weeks.
- Your real emails stop arriving: sign-up codes, receipts and password resets for your actual users.
How it ends up in the browser
A common thing we see in vibe-coded apps: someone asks the AI tool for a contact form, a welcome email or a "notify me" button. The quickest code that works calls SendGrid or Resend straight from a React component, with the key in a variable like VITE_SENDGRID_API_KEY or NEXT_PUBLIC_RESEND_API_KEY. Any VITE_ or NEXT_PUBLIC_ variable, and anything imported by client code, is copied into the JavaScript every visitor downloads. More on that: https://leakyvibe.com/guides/is-my-vercel-app-leaking-environment-variables
5-minute check: does my live site ship an email key?
- Open your live site in a private window.
- Open DevTools (F12), go to Sources (Debugger in Firefox) and use "Search in all files" (Ctrl+Shift+F, or Cmd+Option+F on Mac).
- Search for SG. then re_ then api.sendgrid.com and api.resend.com. A hit on the API address in your frontend code is a strong hint the key is nearby.
- Send yourself a message through your contact form with the Network tab open. If the request goes straight to api.sendgrid.com or api.resend.com from the browser, the key is in the browser too.
- Also search your repo and its git history (git log -p | grep -n "SG\.") in case a key was ever committed.
Or paste your URL in the free scan at the top of this page. It reads the HTML and JavaScript your site serves and flags SendGrid (SG.) and Resend (re_) keys as a critical finding.
Found one? Do this, in this order
- Delete the exposed key. Resend: API keys page > More options > Remove API key; Resend's docs say deleting revokes it right away. SendGrid: Settings > API Keys > action menu > Delete API Key; SendGrid then rejects any call made with it. Your app's email sending stops until step 3, which is better than a stranger using it.
- Create a new key with the least access. Resend: choose "Sending access" (not "Full access") and restrict it to your sending domain. SendGrid: choose Custom (restricted) access with only Mail Send. Both providers show the key value only once.
- Put the new key only in a server-side environment variable (no VITE_ or NEXT_PUBLIC_ prefix), move the sending code to a server route (next section) and redeploy.
- Check what was sent: in Resend, filter the Logs page by API key and look at the Emails page (Resend keeps 30 days of activity). In SendGrid, look at Email Activity for messages you didn't send. Watch your bounce and spam numbers for the next few weeks.
- Remove the old key from your code and git history. Full checklist for any leaked secret: https://leakyvibe.com/guides/leaked-api-key-what-to-do
Note: Resend is a GitHub secret scanning partner, so keys pushed to public GitHub repos, gists or issues get revoked automatically. That doesn't cover a key that only sits in your live site's JavaScript, so don't count on it.
The safe fix: send email from the server
- The browser posts the form to your own route (for example /api/contact, a Supabase Edge Function or a Netlify/Vercel function). Only that route has the key.
- Don't let the browser choose who the email goes to or what HTML it contains. Fix the recipient (your inbox, or the signed-in user's own email) on the server. Otherwise your route becomes a free spam relay even without the key.
- Add a rate limit and a CAPTCHA on public forms, so a script can't use your route to send thousands of messages.
- Use one key per app or service, so a leak in one place can be deleted without breaking the rest, and delete keys you no longer use.
A prompt you can give your AI tool
My SendGrid/Resend API key is in my frontend code. Find every place the email API is called from client code or the key is read from a VITE_* or NEXT_PUBLIC_* variable. Move all email sending to a server route (or edge function) that reads the key from a server-only environment variable. The browser should only send the form fields; the server decides the recipient and builds the email. Add input validation, a per-IP rate limit and a CAPTCHA check on public forms. Remove the key from client code, .env files that are committed, and the repo. Don't print any key values. List the files you changed.
Sources
Official sources (checked 2026-11-17): Twilio SendGrid, "API Keys" (https://www.twilio.com/docs/sendgrid/ui/account-and-settings/api-keys): treat the key like a password, shown only once, never include it in your code, Full vs Custom access, deleting a key makes SendGrid reject later calls, replace = delete, create, update code. Resend, "How to handle a leaked API key" (https://resend.com/docs/knowledge-base/how-to-handle-a-leaked-api-key): the key is the only credential needed to send, deleting revokes it right away, review Logs and Emails (30 days), 4% bounce / 0.08% spam thresholds, prefer Sending access. Resend, "How to handle API keys" (https://resend.com/docs/knowledge-base/how-to-handle-api-keys): don't expose keys in the browser or client-side code. Resend API, "Create API key" (https://resend.com/docs/api-reference/api-keys/create-api-key): full_access vs sending_access, domain restriction. Resend changelog, "GitHub Secret Scanner" (https://resend.com/changelog/github-secret-scanner): automatic revocation of keys found on public GitHub.