# Groundmail + Lovable

Lovable’s install surface is **Secrets + a chat prompt**: store your `gm_` key, paste the prompt, and let Lovable wire server-side sends (often via Supabase Auth).

There is no separate Lovable “connector package.” The agent writes the Edge Function / hook against `POST /emails` (or [SMTP](https://docs.groundmail.dev/smtp) if you prefer Supabase’s custom SMTP UI).

## When to use Groundmail

- Password reset, verification, receipts, and other **transactional** mail
- Branded `from` on your own domain after DNS verify
- Supabase Auth via a Send Email hook / Edge Function, or [custom SMTP](https://docs.groundmail.dev/smtp) with a Groundmail credential

Lovable projects often use Supabase — see [Supabase Auth email](https://docs.groundmail.dev/guides/supabase) for the hook path and the SMTP alternative.

## 1. Add your API key (Secrets)

1. Open [app.groundmail.dev](https://app.groundmail.dev) → **API Keys** → create a key (`gm_…`).
2. In Lovable, open your project **Settings** → **Secrets** (or the env/secrets panel Lovable exposes for backend code).
3. Add:

```text
GROUNDMAIL_API_KEY=gm_your_key_here
```

Use the secret name exactly — server routes and Edge Functions should read `process.env.GROUNDMAIL_API_KEY`. Never put the key in client-side React components.

## 2. Paste this prompt in Lovable chat

```text
Integrate Groundmail transactional email into this app.

Docs (read first):
- https://docs.groundmail.dev/llms.txt
- https://docs.groundmail.dev/ai/safe-defaults
- https://docs.groundmail.dev/guides/supabase
- https://docs.groundmail.dev/smtp
- https://docs.groundmail.dev/builders/lovable

Rules:
1. GROUNDMAIL_API_KEY is in Lovable Secrets. Use it only in a Supabase Edge Function or other server code, never in React components, and never log it.
2. Send with POST https://api.groundmail.dev/emails (header Authorization: Bearer $GROUNDMAIL_API_KEY; JSON fields from, to, subject, html, text, reply_to), or the "groundmail" npm package on Node (it throws GroundmailError on failure).
3. Give every send an Idempotency-Key header (SDK option: idempotencyKey) that is unique to the message, for example "password-reset-<userId>-<tokenId>", and reuse it if you retry.
4. Test first: send from "noreply@mail.groundmail.dev" to my Groundmail account email only. Do not email anyone else until I have verified a domain and added a payment method in Groundmail (the Free plan is not charged).
5. On an error, read error.code and error.hint from the JSON response and follow the hint (https://docs.groundmail.dev/errors). Retry only on 429, 500, 502 or 503, with backoff and the same Idempotency-Key.
6. Add a server-side send path for at least one real flow: password reset, email verification or a receipt.
7. If I prefer Supabase custom SMTP instead of the API: host smtp.groundmail.dev, port 587, username groundmail, password from an SMTP credential in the Groundmail dashboard.

When my domain is verified, switch from to an address on it, for example noreply@ourdomain.com.
```

If the agent asks for your account email, give it the email you sign in to Groundmail with.

## 3. Minimal server send (Node / Edge)

```ts
const res = await fetch('https://api.groundmail.dev/emails', {
  method: 'POST',
  headers: {
    Authorization: `Bearer ${process.env.GROUNDMAIL_API_KEY}`,
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    from: 'noreply@yourdomain.com',
    to: 'user@example.com',
    subject: 'Verify your email',
    html: '<p>Click the link to verify.</p>',
  }),
})
```

Or install `groundmail` and use the SDK — see [Next.js](https://docs.groundmail.dev/frameworks/nextjs).

## Sandbox self-test

Send to your Groundmail account email only, with `from: "noreply@mail.groundmail.dev"`, until your domain is verified and Free production is unlocked (payment method on file):

```json
{
  "from": "noreply@mail.groundmail.dev",
  "to": "YOUR_ACCOUNT_EMAIL",
  "subject": "Groundmail sandbox check",
  "html": "<p>It works.</p>"
}
```

## Production

1. Add your domain in the Groundmail dashboard.
2. Apply DNS (Cloudflare integration or registrar credentials).
3. Verify, add a payment method to unlock Free production (Free is never charged; you're only billed if you turn on pay as you go), then update `from` to an address on that domain.

## Optional: MCP while you build

If you use Cursor (or another MCP client) alongside Lovable, connect hosted MCP at `https://mcp.groundmail.dev/mcp` with OAuth — see [MCP server](https://docs.groundmail.dev/ai/mcp). That helps the coding agent; your Lovable app still needs Secrets (or SMTP) for runtime sends.

## Links

- [Quickstart](https://docs.groundmail.dev/quickstart)
- [Domains API](https://docs.groundmail.dev/domains)
- [SMTP relay](https://docs.groundmail.dev/smtp)
- [MCP server](https://docs.groundmail.dev/ai/mcp)
- [Password reset guide](https://docs.groundmail.dev/guides/password-reset-emails)
- [Supabase Auth email](https://docs.groundmail.dev/guides/supabase)
