# Set up Sendtern in a repository

Use this guide from your coding agent or terminal. Select Codex, Claude Code, or OpenCode on the documentation page to copy a prompt for the same workflow.

## 1. Identify the integration

Find the server-side code that sends mail, its configuration schema, and the existing tests. Use the repository's runtime and conventions. Ask only for requirements still missing, such as the email event or production sender. Keep existing email templates and unrelated features intact.

## 2. Configure access

- Create a project and a sending-only key in the dashboard. Put the key in the application's secret manager as `SENDTERN_API_KEY`.
- Add placeholders to the checked-in configuration example. Keep actual keys out of chat, source files, client bundles, and request logs.
- Use an owner session or a separate full-access key for resource administration. The application's key only needs sending permissions.

## 3. Implement and test with Email Playground

Write or update a server-side function. Keep a stable `Idempotency-Key` for each business event so transient HTTP failures can be retried without creating a new message. Check the SDK or HTTP result before treating the email as queued.

```bash
curl --fail-with-body https://sendtern.com/test/emails \
  -H "Authorization: Bearer $SENDTERN_API_KEY" \
  -H 'Content-Type: application/json' \
  -H 'Idempotency-Key: integration-check-001' \
  -d '{"from":"sandbox@sendtern.test","to":"recipient@example.com","subject":"Integration test","text":"Captured by Sendtern."}'
```

A successful capture returns an ID. Inspect the message in Email Playground. A sending key restricted to a verified domain must use that domain in `from`, including when testing. You can also use the Resend Node.js SDK with the test base URL `https://sendtern.com/test` for `emails.send`. Run the existing automated tests. If a key is unavailable, finish the code and mock the integration, then report the step still needed.

## 4. Move to production when approved

Add the exact sender domain in Sendtern, publish the DNS records shown in the dashboard, and verify it. If DNS changes require access you do not have, give the operator the records and continue with the test integration. Use the approved sender address and use the production API path `https://sendtern.com/emails` or the Resend Node.js SDK base `https://sendtern.com/resend`. Replace any sample recipient with an approved address before making a real send.

```js
import { Resend } from 'resend';

const mail = new Resend(process.env.SENDTERN_API_KEY, {
  baseUrl: 'https://sendtern.com/resend',
});

export async function sendReceipt({ eventId, recipient, text }) {
  const { data, error } = await mail.emails.send({
    from: process.env.SENDTERN_FROM,
    to: recipient,
    subject: 'Payment receipt',
    text,
  }, { idempotencyKey: 'receipt-' + eventId });
  if (error) throw new Error(error.message);
  return data.id;
}
```

When migrating an existing Resend integration, reuse its mail calls wherever the [compatibility contract](https://sendtern.com/agent.md#migrate-from-resend) covers them. Create new Sendtern keys and verify the sender domain in Sendtern. Use the delivery timeline to distinguish app acceptance, SMTP acceptance, delay, and bounce.

Report the changes, tests, actual capture ID when available, and remaining configuration. For DNS and spam checks see the [deliverability guide](https://sendtern.com/deliverability.md). For machine-readable endpoints use [OpenAPI](https://sendtern.com/openapi.json).
