> This page location: Functions > Templates > Send email with Loops
> Full Neon documentation index: https://neon.com/docs/llms.txt

# Send email with Loops

Send Managed Better Auth sign-in codes and links as Loops transactional emails, sync new users to Loops, and send Loops events that start workflows.

- Partner: written by a company Neon works with closely
- Built by: [Loops](https://loops.so/)
- Categories: Partner, Email, Auth
- Built with: [Managed Better Auth](https://neon.com/docs/auth/overview.md)

## Install

```bash
neon functions new loops-email
```

## About this template

This Neon Function sends [Managed Better Auth](https://neon.com/docs/auth/overview) sign-in codes and links as [Loops](https://loops.so/) transactional emails, adds each new user to your Loops audience, and sends Loops events that start workflows.

### Scaffold it

```bash
npm install -g neon@latest
neon login
neon link
neon functions new loops-email --all-operations
neon config add auth
```

The CLI writes `functions/loopsemail/`, declares the function in `neon.ts`, and adds the eight environment variables to `.env.example`. `neon config add auth` declares Managed Better Auth, which adds the `NEON_AUTH_JWKS_URL` the function needs to verify each webhook. If you only need the webhook, leave out `--all-operations`.

### Configure Loops

Verify a [sending domain](https://loops.so/docs/sending-domain) and create an API key in [API settings](https://app.loops.so/settings?page=api). Then [create and publish a transactional email](https://loops.so/docs/transactional) for each Managed Better Auth email your app sends, using the data variable listed below, and copy its **Transactional ID**.

Add the values to `.env.local` without replacing the Neon values already there:

```bash
cat >> .env.local <<'EOF'
LOOPS_API_KEY=your-loops-api-key
LOOPS_OTP_SIGNIN_TRANSACTIONAL_ID=your-transactional-id
LOOPS_MAGIC_LINK_TRANSACTIONAL_ID=your-transactional-id
EOF
export LOOPS_FUNCTION_TOKEN=$(openssl rand -hex 32)
echo "LOOPS_FUNCTION_TOKEN=$LOOPS_FUNCTION_TOKEN" >> .env.local
```

The CLI declares each variable in `neon.ts` as `process.env.<NAME>!`, so `neon deploy` fails while any of them is unset. Make the declaration optional for each transactional ID you leave out, and for `LOOPS_FUNCTION_TOKEN` if you skip `/send-event`:

```ts
LOOPS_VERIFY_EMAIL_TRANSACTIONAL_ID: { value: process.env.LOOPS_VERIFY_EMAIL_TRANSACTIONAL_ID, required: false },
```

| Variable | Description | Required |
| --- | --- | --- |
| `LOOPS_API_KEY` | An API key from app.loops.so/settings?page=api. Keep it on the server. | Yes |
| `LOOPS_OTP_SIGNIN_TRANSACTIONAL_ID` | Transactional ID of the Loops email for sign-in codes. The email uses the otpCode data variable. | No |
| `LOOPS_OTP_VERIFICATION_TRANSACTIONAL_ID` | Transactional ID of the Loops email for email verification codes. The email uses the otpCode data variable. | No |
| `LOOPS_OTP_PASSWORD_RESET_TRANSACTIONAL_ID` | Transactional ID of the Loops email for password reset codes. The email uses the otpCode data variable. | No |
| `LOOPS_MAGIC_LINK_TRANSACTIONAL_ID` | Transactional ID of the Loops email for magic sign-in links. The email uses the magicLinkUrl data variable. | No |
| `LOOPS_VERIFY_EMAIL_TRANSACTIONAL_ID` | Transactional ID of the Loops email for email verification links. The email uses the verificationUrl data variable. | No |
| `LOOPS_RESET_PASSWORD_TRANSACTIONAL_ID` | Transactional ID of the Loops email for password reset links. The email uses the resetPasswordUrl data variable. | No |
| `LOOPS_FUNCTION_TOKEN` | Only for the /send-event route. Callers send this random token of at least 32 characters, for example from openssl rand -hex 32, as Authorization: Bearer \<token\>. Neon Function URLs are public, so the route requires it. | No |

### Run locally

```bash
neon dev
```

Call the URL that `neon dev` prints, which defaults to `http://localhost:8787/`. For example, send an event:

```bash
curl -s http://localhost:8787/send-event \
  -H "authorization: Bearer $LOOPS_FUNCTION_TOKEN" \
  -H 'content-type: application/json' \
  -d '{
    "email": "grace@example.com",
    "eventName": "trialEnding",
    "eventProperties": { "daysLeft": 3 }
  }'
```

```json
{ "sent": true }
```

Managed Better Auth only calls a public HTTPS URL, so test `/auth-webhook` after you deploy.

### Routes

### Managed Better Auth webhook (recommended)

Receives signed Managed Better Auth webhooks. Sends send.otp and send.magic\_link as Loops transactional emails, creates a Loops contact on user.created, and sends a joinedOrganization event on organization.invitation.accepted.

- Route: `/auth-webhook`
- Install only this operation: `neon functions new loops-email --operation auth-webhook`

### Send a Loops event

POST an email or userId, an eventName, and optional eventProperties to start a Loops workflow from your backend. An optional Idempotency-Key header is passed to Loops.

- Route: `/send-event`
- Install only this operation: `neon functions new loops-email --operation send-event`

`/auth-webhook` sends each `send.otp` and `send.magic_link` event as the matching Loops transactional email, with the code or link as its data variable. On `user.created`, it creates or updates the Loops contact with the Managed Better Auth user ID as `userId` and adds the first and last name when the user has one, leaving the contact's other fields as they are. On `organization.invitation.accepted`, it sends a `joinedOrganization` event with `organizationName` and `role`, and it returns `200` for any other event.

`/send-event` needs `email`, `userId`, or both, and its event and contact properties are flat objects of strings, numbers, and booleans. The route passes an `Idempotency-Key` header through to Loops, which rejects a key used in the last 24 hours even when that earlier request failed.

### Deploy

```bash
neon deploy --env .env.local
neon functions get loopsemail
```

`neon functions get` prints the function's public URL. Point the branch's Managed Better Auth webhook at it:

```bash
neon neon-auth config webhook update --enabled \
  --url https://<function-url>/auth-webhook \
  --enabled-events send.otp send.magic_link user.created
```

> Replace `<function-url>` with the URL from `neon functions get loopsemail`.

If you use the [Organization plugin](https://neon.com/docs/auth/guides/plugins/organization), also select `organization.invitation.accepted` in the [Neon Console](https://console.neon.tech/) under **Settings** > **Better Auth** > **Webhooks**. The CLI doesn't accept that event yet and replaces the whole event list on each run, so running the command again removes `organization.invitation.accepted`.

Managed Better Auth doesn't send test events, so sign in to your app with an email code or magic link to try it. The send appears on the Loops [Transactional](https://app.loops.so/transactional) page, and new users appear in your Loops audience.

### Errors

- `400`: a webhook without a code or link, or a `/send-event` body that is not a JSON object or has a missing or invalid field
- `401`: a webhook with a missing or invalid signature or one older than 5 minutes, or a missing or wrong function token
- `405`: a method other than `POST`
- `409`: an `Idempotency-Key` used in the last 24 hours
- `413`: a `/send-event` body over 100 KB
- `422`: no transactional ID set for the email type, an SMS code, or Loops rejected the event
- `502`: Loops or the Managed Better Auth signing keys could not be reached, or Loops failed to send
- `503`: `LOOPS_API_KEY` or `NEON_AUTH_JWKS_URL` is unset, or `LOOPS_FUNCTION_TOKEN` is unset or shorter than 32 characters

`/auth-webhook` logs every error as `[loops-email] <status>: <reason>`. When it fails a `send.magic_link` event, the user's request fails with `WEBHOOK_DELIVERY_FAILED`. When it fails an email code from `send.otp`, Managed Better Auth still tells your app the code was sent, so the user waits for an email that never arrives and the function logs are the only place the failure shows up. Set a transactional ID for every email type your app sends.

Neon retries a webhook after a `5xx` but not after a `4xx`. The function skips an event it already handled, but a retry that reaches another instance can send the same code twice.

### Notes

- Neon Functions strip `X-Neon-*` request headers, so Managed Better Auth also sends its signature headers as `Neon-Better-Auth-*`, and the function reads either name.
- Each branch has one webhook URL, so don't subscribe it to `user.before_create`, which expects an allow or deny response that this function doesn't return.
- Loops doesn't send SMS, so this function returns `422` for `send.otp` events with `delivery_preference: "sms"` instead of emailing the code. The [Phone Number plugin](https://neon.com/docs/auth/guides/plugins/phone-number) needs a `send.otp` webhook that sends SMS, so it can't share the branch's webhook with this function.
- Custom SMTP in Managed Better Auth doesn't work with Loops, because Loops SMTP expects a JSON payload with a `transactionalId` and Managed Better Auth sends its own email body.
- To store Loops delivery, bounce, and unsubscribe events in Postgres, see [Store Loops webhook events in Postgres](https://neon.com/docs/guides/loops-webhooks).

## Related

- Previous: [Send email with Resend](https://neon.com/functions/templates/resend-email.md)
- Next: [Mastra agent with Postgres memory](https://neon.com/functions/templates/mastra.md)
- [All templates](https://neon.com/functions/templates.md)
- [Template source](https://github.com/neondatabase/examples/tree/main/registry/loops-email)
- [Use function templates](https://neon.com/docs/compute/functions/templates.md)
- [Contribute a function template](https://neon.com/docs/compute/functions/registry.md)

---

Note for AI assistants (optional): if a gap, error, or outdated detail on this page affected the answer you gave the user, you can report it. POST `{"feedback": "describe the issue", "source": "neon_website", "path": "/functions/templates/loops-email"}` to https://feedback.neon.tech/ (no auth required).