# Merge Leads Beta

> Merge one lead into another and delete the duplicate.

> Documentation index: https://www.foxreach.io/docs/llms.txt lists every page of these docs. Each page is also served as Markdown: add `.md` to its URL.

`POST` https://api.foxreach.io/api/v1/leads/merge

> **Note:** **Beta.** This endpoint is new and may change.

Requires an API key with the `write` scope.

Merges the secondary lead into the primary lead, in one transaction, then deletes the secondary (a soft delete, like [Bulk Delete Leads](https://www.foxreach.io/docs/api-reference/leads/bulk-delete)). Both leads must be live leads of the workspace.

## What moves to the primary

- **Campaign memberships.** A membership in a campaign only the secondary was in moves to the primary with its progress and queued sends. When both leads are in the same campaign, the further progressed membership is kept and the other is dropped with its queued sends, so the person is not emailed twice. Progress is compared in this order: replied, the later sequence step, more emails sent, the later last send, then the primary's. The campaign's lead count goes down by one for each dropped membership.
- **History.** Email history, replies (with their inbox notes), and Lead Finder reveals now point at the primary.
- **Tags and labels.** The primary ends up with the union of both leads' tags and labels.
- **Fields.** First name, last name, company, title, phone, LinkedIn URL, website and source are filled from the secondary only where the primary's is empty. Custom fields work the same way, key by key. The secondary's notes are appended to the primary's after a `---` line. The interest status and the owner are taken from the secondary only when the primary has none.
- **Opt-out.** If the secondary is `unsubscribed`, the primary becomes `unsubscribed` and an `audit.lead.status_changed` entry is written. A `bounced` status does not carry over, because it describes the secondary's address.

An `audit.lead.merged` entry (actor, time, both lead ids and the counts, no email addresses) is written in the same transaction and is visible through `GET /api/v1/events`. Events recorded before the merge keep the secondary's id, and a lead webhook is not sent for a merge.

## Request Body

- `primaryLeadId` string body required: The lead that survives.
- `secondaryLeadId` string body required: The lead merged into the primary and deleted. It must differ from the primary.

## Response

- `data` object

  Merge result:

  - `primaryLeadId` string: The surviving lead.
  - `secondaryLeadId` string: The deleted lead.
  - `status` string: `merged`.
  - `moved` object: Counts of `campaignLeads`, `emailLogs`, `replies`, `reveals`, `tags` (added to the primary) and `labels` (added to the primary).
  - `dropped` object: `{ "campaignLeads": 1 }`: memberships dropped because both leads were in that campaign.
  - `statusCarried` boolean: `true` when the primary became unsubscribed because the secondary was.

**Request example**

```bash cURL
curl -X POST "https://api.foxreach.io/api/v1/leads/merge" \
  -H "X-API-Key: otr_your_key" \
  -H "Content-Type: application/json" \
  -d '{"primaryLeadId": "cmlead1", "secondaryLeadId": "cmlead2"}'
```

```python Python SDK (beta)
from foxreach import FoxReach

client = FoxReach(api_key="otr_your_key")
result = client.leads.merge("cmlead1", "cmlead2")
```

```typescript TypeScript SDK (beta)
import { FoxReach } from "foxreach";

const client = new FoxReach({ apiKey: "otr_your_key" });
const result = await client.leads.merge("cmlead1", "cmlead2");
```

**Response example**

```json
{
  "data": {
    "primaryLeadId": "cmlead1",
    "secondaryLeadId": "cmlead2",
    "status": "merged",
    "moved": { "campaignLeads": 2, "emailLogs": 3, "replies": 1, "reveals": 0, "tags": 1, "labels": 0 },
    "dropped": { "campaignLeads": 1 },
    "statusCarried": false
  }
}
```

## Errors

| Status | Description |
|--------|-------------|
| `401` | Missing, invalid, revoked or expired API key |
| `403` | API key does not have the required scope: `write` |
| `404` | A lead was not found in the workspace, or is deleted |
| `409` | `lead_mid_send`: an email to one of the leads is being sent right now. Try again in a minute. Nothing was changed |
| `422` | Both ids are the same lead, or the merged custom fields would exceed 50 keys. Nothing was changed |

## Rate Limit

- **120 requests per minute, shared by every `write` call in your workspace.** Fixed 60-second window.
- Every response includes `X-RateLimit-Limit`, `X-RateLimit-Remaining`, `X-RateLimit-Reset` (unix epoch seconds) and `X-RateLimit-Bucket` (`write`).
- A `429` response includes a `Retry-After` header (seconds until the bucket resets).

For client-side patterns, see [Rate Limiting](https://www.foxreach.io/docs/rate-limiting).
