# How every recipe works

> The three moves behind every CRM hook-up. Send the new lead, receive call.completed, write the outcome back.

Every CRM recipe here is the same three moves. Once you know them, any CRM or automation tool will do.

## 1. Send each new lead

Make a key in **Settings > API** from the **CRM speed to lead** set, limited to the campaigns it should fill. It adds leads, starts their calls (Start calls), and reads outcomes and events. Tick Webhooks (read and write) too if your tool will set up its own webhook endpoint. See [Keys and authentication](https://developers.callview.ai/guides/authentication/#the-ready-made-sets).

When a lead lands in your CRM, send it to `POST /leads`:

| Setting | Value |
| --- | --- |
| Method and address | `POST https://v2-api.callview.ai/api/v1/external/leads` |
| Login | HTTP Basic with your key ID and secret, or the header `Authorization: Bearer cs_live_...` |
| `Idempotency-Key` header | Something from the CRM record and its data, like `crm-<record id>-<last updated time>`, so a retry can't make two leads and a changed record gets a new key. The first answer to a key is kept for 24 hours, errors included: after fixing a refused lead, send it with a new key. See [Safe retries](https://developers.callview.ai/guides/idempotency/). |
| Body | JSON with only the fields below. Unknown fields are refused, so don't send the CRM's whole record. |

```json
{
  "campaign_id": "7c1f2b9e-4a3d-4e8f-9b21-5d6a7e8f9a01",
  "external_id": "<the CRM record id>",
  "phone": "<the phone>",
  "name": "<first and last name>",
  "email": "<email>",
  "fields": { "source": "<where the lead came from>" },
  "consent": { "source": "web_form", "agreed_at": "<when they agreed>", "url": "<the form's page>" },
  "call_now": true
}
```

`external_id` is what ties everything together: every call and event about this lead carries it back to you.

## 2. Receive `call.completed`

Make a webhook endpoint that points at your tool's "receive a webhook" address:

```bash
curl -X POST 'https://v2-api.callview.ai/api/v1/external/webhook_endpoints' \
  -u "$CALLVIEW_KEY_ID:$CALLVIEW_SECRET" \
  -H 'Content-Type: application/json' \
  -d '{ "url": "https://hooks.example.com/your-tool-address", "events": ["call.completed", "lead.opted_out"], "campaign_ids": ["7c1f2b9e-4a3d-4e8f-9b21-5d6a7e8f9a01"] }'
```

`campaign_ids` lists the campaigns the key is limited to. An endpoint without it covers every campaign, which a key limited to some campaigns isn't allowed to make (`403 permission_denied`). With a key for every campaign you can leave it out.

Keep the `whsec_` secret from the reply. If your tool can run a little code, [check the signature](https://developers.callview.ai/guides/webhooks/#check-the-signature). If it can't, keep the address private: it's the only thing standing between a stranger and your CRM.

## 3. Write the outcome back

From the event, the fields you'll want:

| From the event | Put it in the CRM as |
| --- | --- |
| `data.object.lead.external_id` | Which record to update |
| `data.object.outcome` | A dropdown with the [outcome words](https://developers.callview.ai/guides/outcomes/) |
| `data.object.outcome_label` | The outcome as your team names it |
| `data.object.note` | A note on the record |
| `data.object.handled_by.name` | Who spoke to them |
| `data.object.callback_at` | A task or a follow-up date |
| `data.object.duration_seconds` | Call length |

For the recording or the transcript, call `GET /calls/{id}/recording` or `GET /calls/{id}/transcript` (the paths are in `recording_path` and `transcript_path`). The key needs Recordings & transcripts (`recordings:read`) for these, which the CRM speed to lead set leaves out. A recording link works for 5 minutes, so fetch it when someone wants to listen. Don't save the link itself.

`lead.opted_out` means the person asked not to be called. Mark them do-not-call in the CRM too.

## Build it with a test key first

Use a test key and the [magic numbers](https://developers.callview.ai/guides/test-mode/) while you wire it up. `+15550100001` gives you an interested lead in about 4 minutes, and `+15550100002` a voicemail in about a minute. When it all works, change the key and the webhook endpoint to live ones.
