> ## Documentation Index
> Fetch the complete documentation index at: https://docs.newlead.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Sync leads to a CRM

> Create and update CRM contacts from NewLead webhook events.

This recipe keeps a CRM up to date with the leads of a text agent. Every lead event carries the complete [`lead` object](https://docs.newlead.ai/webhook-events#the-lead-object), so any of them can create or update the contact.

**1. Create the endpoint**

Register the URL of your integration with the four lead events, and store the `secret` from the response.

```bash theme={null}
curl -X POST https://rest.newlead.ai/v1/webhook_endpoints \
  -H "Authorization: Bearer $NEWLEAD_API_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: crm-sync-endpoint-001" \
  -d '{
    "text_agent_id": "<TEXT_AGENT_ID>",
    "url": "https://crm.example.com/webhooks/newlead",
    "description": "CRM sync",
    "events": [
      "lead.created",
      "lead.qualified",
      "lead.appointment_booked",
      "lead.handoff"
    ]
  }'
```

**2. Accept each delivery once**

[Verify the signature](https://docs.newlead.ai/webhook-signatures.md), skip any `X-Webhook-Delivery` you already processed and respond with a `2xx` within 10 seconds.

**3. Find or create the contact**

Do this on every event, so that a missed `lead.created` does not leave a contact out of the CRM.

1. Search by `lead.contact_id`, stored in a custom field of the contact.
2. If nothing matches, search by `lead.phone` or `lead.email`, so you do not duplicate someone who was already in the CRM.
3. If the contact still does not exist, create it. Save `lead.contact_id` in the custom field.

| CRM field | NewLead field |
| - | - |
| Name | `lead.name` |
| Phone | `lead.phone` |
| Email | `lead.email` |
| Channel | `lead.channel` |
| Source | `lead.source.type` |
| NewLead ID (custom field) | `lead.contact_id` |

**4. Apply each event**

| Event | What to do in the CRM |
| - | - |
| `lead.created` | Nothing more: the previous step created the contact. |
| `lead.qualified` | Copy `lead.qualification.answers` to contact fields or a note, and move the funnel stage. |
| `lead.appointment_booked` | Create an activity on `appointment.starts_at`. |
| `lead.handoff` | If `handoff.requires_human` is `true`, create a task for the sales team with `handoff.message`. |

## Without webhooks: poll for changes

If your integration cannot receive webhooks, ask for the leads with new activity every few minutes. Keep the time of each poll, and start the next one a few minutes earlier, so a lead that changed while you were polling is not missed.

```bash theme={null}
curl "https://rest.newlead.ai/v1/leads?text_agent_id=<TEXT_AGENT_ID>&last_activity_after=2026-11-19T09:55:00Z" \
  -H "Authorization: Bearer $NEWLEAD_API_KEY"
```

`last_activity_at` changes with every message in the conversation, sent or received, and when a call starts on that lead. A new lead starts with its creation time, so it also matches. Editing a lead does not change it. A voice agent's phone calls belong to its own leads: poll them with `voice_agent_id`. Follow `next_cursor` until `has_more` is `false`: results are ordered by creation date, not by activity.

Webhooks are still the better option: they arrive within seconds and include what changed.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.