# HubSpot

> Send new HubSpot contacts to CallView from a workflow, and write the call's outcome back to the contact.

This follows the three moves in [How every recipe works](https://developers.callview.ai/recipes/). HubSpot menus differ a little between plans; the names below are the usual ones.

You need a HubSpot plan with workflows that can run custom code or send webhooks.

## Send new contacts

1. In HubSpot, make a contact-based **workflow**. Trigger it the way your leads arrive, for example "Form submission" on your quote form.
2. Add a **Custom code** action (Node.js). Add your CallView secret as a secret named `CALLVIEW_SECRET`, and pass in the contact's `hs_object_id`, `firstname`, `lastname`, `email` and `phone`.
3. Paste this, with your campaign's ID:

```js
exports.main = async (event, callback) => {
  const c = event.inputFields;
  const res = await fetch('https://v2-api.callview.ai/api/v1/external/leads', {
    method: 'POST',
    headers: {
      Authorization: `Bearer ${process.env.CALLVIEW_SECRET}`,
      'Content-Type': 'application/json',
      'Idempotency-Key': `hubspot-${c.hs_object_id}-create`,
    },
    body: JSON.stringify({
      campaign_id: '7c1f2b9e-4a3d-4e8f-9b21-5d6a7e8f9a01',
      external_id: `hubspot-${c.hs_object_id}`,
      phone: c.phone,
      name: `${c.firstname ?? ''} ${c.lastname ?? ''}`.trim(),
      email: c.email,
      consent: { source: 'web_form', agreed_at: new Date().toISOString(), url: 'https://example.com/quote' },
      call_now: true,
    }),
  });
  const body = await res.json();
  callback({ outputFields: { callview_status: body.data?.status ?? body.error?.code } });
};
```

A `409 duplicate_lead` or `422 on_dnc_list` is an answer, not a fault: the contact is already with CallView, or asked not to be called. Let the workflow carry on.

## Write the outcome back

Point a webhook endpoint at whatever receives events for you:

- **A HubSpot workflow that starts from a webhook**, if your plan has one. Use `data.object.lead.external_id` (minus the `hubspot-` prefix) to find the contact, then set your properties.
- **Zapier or Make** in between, if not. See [Zapier](https://developers.callview.ai/recipes/zapier/) or [Make](https://developers.callview.ai/recipes/make/), and use their HubSpot "update contact" step.
- **A small receiver of your own** (see [Webhooks](https://developers.callview.ai/guides/webhooks/)) that calls HubSpot's API.

Make contact properties for the outcome (a dropdown with the [outcome words](https://developers.callview.ai/guides/outcomes/)), the note, and who took the call, and fill them from the event.
