Sub topic 9
Creating Activities Through the API
Assume you understand basic CRM records, pipeline stages, and JSON. You should also know how to identify a deal, contact, and company by their record IDs. An API is a programmatic interface that lets your code create or update CRM records instead of using the web interface.
An activity is a record of work around a deal. Examples include a call, email, meeting, note, or follow-up task. The API request must identify the activity type, its content, and the CRM records connected to it.
The request model
Most activity requests contain these parts:
| Part | Purpose | Example |
|---|---|---|
| Authentication | Proves that the caller has permission | OAuth access token |
| Activity properties | Stores the time, text, status, or subject | `hs_note_body` |
| Association | Links the activity to other records | Note linked to a deal |
| Record ID | Identifies the related CRM record | `DEAL_ID` |
Salesforce uses objects such as Task, Event, and EmailMessage. HubSpot uses CRM objects such as notes, calls, meetings, and tasks. The names differ, but the workflow is similar:
flowchart TD
A[Prepare authentication] --> B[Identify deal and contact IDs]
B --> C[Build activity properties]
C --> D[Add associations]
D --> E[Send API request]
E --> F{Request succeeds?}
F -->|Yes| G[Store returned activity ID]
F -->|No| H[Inspect status and error]Creating a Salesforce activity
In Salesforce, a completed call can be represented by a Task. WhatId normally links the task to a business record such as an opportunity. WhoId links it to a person, such as a contact or lead.
Task t = new Task(
Subject = 'Discovery call with Dana Lee',
Type = 'Call',
Status = 'Completed',
ActivityDate = Date.today(),
Description = 'Confirmed budget owner. Next step: send pricing by Friday.',
WhatId = opportunityId,
WhoId = contactId
);
insert t;The variables opportunityId and contactId must contain valid Salesforce IDs before this code runs. In production code, obtain those IDs from a trusted lookup rather than accepting arbitrary user input.
A Salesforce task can also represent future work. Use a status such as Not Started, set a due date, and write the expected outcome in the description. Do not mark a task complete merely because the API request succeeded. Request success means the record was created, not that the work happened.
Creating a HubSpot activity
HubSpot creates a note through the CRM objects API. The example below associates the note with a deal.
import os
import requests
url = "https://api.hubapi.com/crm/v3/objects/notes"
headers = {
"Authorization": f"Bearer {os.environ['HUBSPOT_TOKEN']}",
"Content-Type": "application/json",
}
body = {
"properties": {
"hs_timestamp": "2026-11-27T15:00:00Z",
"hs_note_body": "Demo went well. Next step: security review on Dec 4.",
},
"associations": [
{
"to": {"id": os.environ["DEAL_ID"]},
"types": [
{"associationCategory": "HUBSPOT_DEFINED", "associationTypeId": 214}
],
}
],
}
resp = requests.post(url, headers=headers, json=body, timeout=30)
resp.raise_for_status()
print(resp.json()["id"])The association type ID tells HubSpot how the note relates to the deal. Check the value in your account or API documentation before deploying. Custom objects and customized pipelines can use different IDs.
Handle errors and retries
Check the HTTP response rather than assuming that the request worked.
401or403usually indicates an invalid token or missing permission.404often means that a record ID is wrong or inaccessible.400commonly means a required property or association is invalid.429means that the API rate limit was exceeded.5xxresponses may be temporary service errors.
Retry temporary failures with a delay. Avoid blindly retrying every failure, because a second request can create a duplicate activity. Where supported, use an idempotency key. Otherwise, store the returned activity ID or use your own external event ID to detect duplicates.
Verify the result
After creating an activity, verify both the response and the CRM record. Confirm that:
- The returned ID is saved.
- The timestamp uses the intended time zone.
- The activity appears on the correct deal.
- The contact or company association is present.
- The text includes an outcome and a specific next step.
Exercise
A HubSpot note was created successfully, but it does not appear on the deal record. What should you check first?
Show answer
Check the association object, especially the deal ID and association type ID. A successful note creation response does not prove that the note was linked to the intended deal.