# Switching a website from HubSpot to the CRM

For the websites that send their enquiries (CTAs) to HubSpot today: contextual.com,
remarketing.com, advertising.net, and any other app that writes to the "Admedia Adv" portal
(the `dq_*`, `signup_*` and `am_*` fields). The CRM already has the same fields, the CTA
Submissions object and every past enquiry, loaded from HubSpot, so a site only changes where it
sends them.

Each site does the **same 4 steps it does with HubSpot**, with the same field names. Only the
address, the key and the calls change.

## 1. Before you start

| What               | Where                                                                                                                                                                                                                                                |
| ------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| API address        | staging `https://api.crm-staging.admedia.com/api/v1`, production: given at go-live                                                                                                                                                                   |
| Key (one per site) | CRM → Settings → Integrations → **Service keys** → new key, named after the site. Scopes as in HubSpot: `crm.objects.contacts.read` / `.write`, `crm.objects.companies.read` / `.write`, `crm.objects.custom.write` (notes and links come with them) |
| Tracking code      | CRM → Settings → **Tracking code** (replaces HubSpot's tracking script)                                                                                                                                                                              |
| API reference      | `<API address>/docs` (every route, body and answer)                                                                                                                                                                                                  |

Every call sends `Authorization: Bearer crm_sk_...` and `Content-Type: application/json`. A key
can also have an IP allow-list and is logged per call (the key's Logs tab).

Environment variables: replace `HUBSPOT_ACCESS_TOKEN` and `NEXT_PUBLIC_HUBSPOT_PORTAL_ID` with
e.g. `CRM_API_URL`, `CRM_SERVICE_KEY` and the tracking code.

## 2. The calls, HubSpot → CRM

| Step                                    | HubSpot today                                                                                                          | CRM                                                                                                                          |
| --------------------------------------- | ---------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------- |
| Find the contact by email               | `POST /crm/v3/objects/contacts/search`                                                                                 | `GET /contacts?limit=1&filter=<filter on email>` (below), then `GET /contacts/{id}` for its current values                   |
| Create or update the contact            | `POST /crm/v3/objects/contacts`, `PATCH /crm/v3/objects/contacts/{id}`, and the 409 "Existing ID" retry                | **one call:** `POST /contacts/upsert` (creates or updates by email; no 409 retry needed)                                     |
| CTA Submission record + link to contact | `POST /crm/v3/objects/p44357896_cta_submissions`, then `PUT .../associations/default/...`, delete it if the link fails | **one call:** `POST /objects/cta_submission/records` with `contactId`: the record and its link are made together, or neither |
| Find or create the company, link it     | `POST /crm/v3/objects/companies/search`, `POST /crm/v3/objects/companies`, `PUT .../associations/...`                  | `GET /companies?limit=1&filter=<filter on domain>`, else `POST /companies`; then send its id in the upsert's `companyIds`    |
| Timeline note                           | `POST /crm/v3/objects/notes` with association 202                                                                      | `POST /activities/notes` with `links: [{ "type": "contact", "id": "<contact id>" }]`                                         |

### Filters

`filter` is JSON in the query string (URL-encode it):

```json
{
  "version": 1,
  "groups": [{ "conditions": [{ "field": "email", "op": "eq", "value": "jane@brand.com" }] }]
}
```

For a company use `"field": "domain"` (the domain without `www.`, as today).

### Contact upsert

```
POST /contacts/upsert
{
  "email": "jane@brand.com",
  "firstName": "Jane",
  "lastName": "Doe",
  "companyName": "Brand Inc",
  "lifecycleStage": "252039267",
  "leadStatus": "NEW",
  "companyIds": ["<company id>"],
  "customProperties": {
    "contact_website": "https://brand.com",
    "lead_type": "Advertiser",
    "cta_type": "talk_to_our_team",
    "ctx_product": "contextual",
    "ctx_placement": "nav_primary",
    "adc_first_utm_source": "google",
    "last_cta_submitted_at": "2026-11-27T10:00:00.000Z"
  }
}
```

Answer: `{ "created": true|false, "contact": { "id": ..., ... } }`.

- Only the fields you send change. Keep doing what the site does now: send the "first visit"
  fields (`adc_first_*`, `first_touch_lead_source`, lead status) only when the contact's current
  value is empty (read it with `GET /contacts/{id}`), and the "latest" fields every time.
- **Field names stay HubSpot's** except these built-in ones:

  | HubSpot          | CRM (top level of the body)        |
  | ---------------- | ---------------------------------- |
  | `email`          | `email`                            |
  | `firstname`      | `firstName`                        |
  | `lastname`       | `lastName`                         |
  | `phone`          | `phone`                            |
  | `jobtitle`       | `jobTitle`                         |
  | `company`        | `companyName`                      |
  | `lifecyclestage` | `lifecycleStage` (same values)     |
  | `hs_lead_status` | `leadStatus` (same values, `NEW`)  |
  | `website`        | `customProperties.contact_website` |
  | `country`        | `customProperties.contact_country` |

  Every other field (`adc_*`, `ctx_*`, `adv_*`, `rm_*`, `lead_*`, `dq_*`, `signup_*`,
  `hs_google_click_id`, `message`, …) goes in `customProperties` under its HubSpot name.

- Dates: ISO-8601 UTC text (`2026-11-27T10:00:00.000Z`), not epoch milliseconds.
- Dropdowns take the same values as HubSpot.
- Don't send an owner: the CRM's workflows assign it, as HubSpot's do.

### CTA Submission

```
POST /objects/cta_submission/records
{
  "contactId": "<contact id from the upsert>",
  "customProperties": {
    "cta_type": "talk_to_our_team",
    "ctx_product": "contextual",
    "ctx_placement": "nav_primary",
    "ctx_source_page": "https://contextual.com/contact",
    "message": "…"
  }
}
```

Same fields as HubSpot's object (`cta_type`, `message`, `ctx_*`, `rm_*`, `am_*`). Its create date
is the submission time, as in HubSpot.

## 3. Errors

A refused call answers `4xx`/`5xx` with `{ "error": { "code", "message", "details" } }`. Keep the
site's rule: if the contact or the CTA Submission fails, tell the visitor and don't count the
submission; a company or note failure is only logged.

## 4. Workflows and tracking

- Owner assignment, the owner's email and the new-lead reminder run in the CRM's workflows
  (migrated from HubSpot). Keep sending `last_cta_submitted_at` (a new value each time) and
  `ctx_environment` (`staging` outside production, so tests email nobody).
- Replace HubSpot's tracking script with the CRM's tracking code. HubSpot's `hutk` cookie is no
  longer needed.
- `scripts/hubspot-fields.mjs` is not needed: the CRM already has the fields. Ask the CRM team for a
  new field.

## 5. Test, then switch

1. Point the site's staging at the staging API with a staging key and send each form once. Check
   in the CRM: the contact, its fields, the CTA Submission linked to it, the company and the note.
2. At go-live the CRM team loads the last HubSpot enquiries; the sites switch at the same time
   and then remove their HubSpot token.
