Skip to main content

Actions

The ballerinax/hubspot.crm.obj.leads package exposes the following clients:

ClientPurpose
ClientProvides full CRUD, batch, and search operations on HubSpot CRM lead objects.

Client

Provides full CRUD, batch, and search operations on HubSpot CRM lead objects.

Configuration

FieldTypeDefaultDescription
authhttp:BearerTokenConfig|OAuth2RefreshTokenGrantConfig|ApiKeysConfigRequiredAuthentication configuration: OAuth 2.0 refresh token, bearer token, or Private App API key.
serviceUrlstring"https://api.hubapi.com/crm/v3/objects/leads"Base URL of the HubSpot CRM Leads API.
httpVersionhttp:HttpVersionhttp:HTTP_2_0HTTP protocol version to use.
timeoutdecimal30Request timeout in seconds.
retryConfighttp:RetryConfig()Retry configuration for failed requests.
secureSockethttp:ClientSecureSocket()SSL/TLS configuration.
proxyhttp:ProxyConfig()HTTP proxy configuration.
validationbooleantrueEnables constraint validation on request/response payloads.

Initializing the client

import ballerinax/hubspot.crm.obj.leads as leads;
import ballerina/oauth2;

configurable string clientId = ?;
configurable string clientSecret = ?;
configurable string refreshToken = ?;

leads:Client hsLeads = check new ({
auth: {
clientId: clientId,
clientSecret: clientSecret,
refreshToken: refreshToken,
credentialBearer: oauth2:POST_BODY_BEARER
}
});

Operations

Single lead CRUD

Read a page of leads

Retrieves a paginated list of lead records. Use query parameters to filter archived leads, request specific properties, or include association data.

Parameters:

NameTypeRequiredDescription
queriesGetCrmV3ObjectsLeadsGetPageQueriesNoOptional query parameters including limit, after (pagination cursor), properties, propertiesWithHistory, associations, and archived.

Returns: CollectionResponseSimplePublicObjectWithAssociationsForwardPaging|error

Sample code:

leads:CollectionResponseSimplePublicObjectWithAssociationsForwardPaging allLeads =
check hsLeads->/.get(queries = {
properties: ["hs_lead_name"],
'limit: 10
});

Sample response:

{
"results": [
{
"id": "12345",
"properties": {
"hs_lead_name": "Jane Doe",
"createdate": "2024-01-15T10:30:00.000Z",
"lastmodifieddate": "2024-01-15T10:30:00.000Z",
"hs_object_id": "12345"
},
"createdAt": "2024-01-15T10:30:00.000Z",
"updatedAt": "2024-01-15T10:30:00.000Z",
"archived": false
}
],
"paging": {
"next": {
"after": "NTI1Cg%3D%3D",
"link": "https://api.hubapi.com/crm/v3/objects/leads?after=NTI1Cg%3D%3D"
}
}
}
Create a lead

Creates a new lead record. You can specify properties and optionally associate the lead with existing CRM objects (e.g., contacts) at creation time.

Parameters:

NameTypeRequiredDescription
payloadSimplePublicObjectInputForCreateYesLead properties and optional associations. Use associations to link to contacts or other CRM objects on creation.

Returns: SimplePublicObject|error

Sample code:

leads:SimplePublicObjectInputForCreate payload = {
associations: [
{
types: [{associationCategory: "HUBSPOT_DEFINED", associationTypeId: 578}],
to: {id: "67890"}
}
],
properties: {"hs_lead_name": "Jane Doe"}
};
leads:SimplePublicObject createdLead = check hsLeads->/.post(payload);

Sample response:

{
"id": "12345",
"properties": {
"hs_lead_name": "Jane Doe",
"createdate": "2024-01-15T10:30:00.000Z",
"lastmodifieddate": "2024-01-15T10:30:00.000Z",
"hs_object_id": "12345"
},
"createdAt": "2024-01-15T10:30:00.000Z",
"updatedAt": "2024-01-15T10:30:00.000Z",
"archived": false
}
Read a lead

Retrieves a single lead record by its HubSpot object ID or a custom unique identifier property.

Parameters:

NameTypeRequiredDescription
leadsIdstringYesThe HubSpot ID of the lead (or the value of the property specified in idProperty).
queriesGetCrmV3ObjectsLeadsLeadsIdGetByIdQueriesNoOptional query parameters: properties, propertiesWithHistory, associations, archived, and idProperty.

Returns: SimplePublicObjectWithAssociations|error

Sample code:

leads:SimplePublicObjectWithAssociations lead =
check hsLeads->/[leadId].get(queries = {
properties: ["hs_lead_name"]
});

Sample response:

{
"id": "12345",
"properties": {
"hs_lead_name": "Jane Doe",
"createdate": "2024-01-15T10:30:00.000Z",
"lastmodifieddate": "2024-01-16T08:00:00.000Z",
"hs_object_id": "12345"
},
"createdAt": "2024-01-15T10:30:00.000Z",
"updatedAt": "2024-01-16T08:00:00.000Z",
"archived": false,
"associations": {}
}
Update a lead

Performs a partial update on a lead record. Only the fields included in the payload are modified; omitted fields remain unchanged.

Parameters:

NameTypeRequiredDescription
leadsIdstringYesThe HubSpot ID of the lead to update.
payloadSimplePublicObjectInputYesA map of property names to new values.
queriesPatchCrmV3ObjectsLeadsLeadsIdUpdateQueriesNoOptional idProperty to resolve the lead by a custom unique identifier.

Returns: SimplePublicObject|error

Sample code:

leads:SimplePublicObject updatedLead = check hsLeads->/[leadId].patch(
payload = {properties: {"hs_lead_name": "Jane Smith"}}
);

Sample response:

{
"id": "12345",
"properties": {
"hs_lead_name": "Jane Smith",
"createdate": "2024-01-15T10:30:00.000Z",
"lastmodifieddate": "2024-01-16T09:00:00.000Z",
"hs_object_id": "12345"
},
"createdAt": "2024-01-15T10:30:00.000Z",
"updatedAt": "2024-01-16T09:00:00.000Z",
"archived": false
}
Archive a lead

Archives (soft-deletes) a lead record. Archived leads are hidden from default list views but can be retrieved with archived=true.

Parameters:

NameTypeRequiredDescription
leadsIdstringYesThe HubSpot ID of the lead to archive.

Returns: error?

Sample code:

check hsLeads->/[leadId].delete();

Batch operations

Create a batch of leads

Creates multiple lead records in a single API call. Each input in the batch can include properties and associations.

Parameters:

NameTypeRequiredDescription
payloadBatchInputSimplePublicObjectInputForCreateYesA batch input containing an array of lead creation payloads under inputs.

Returns: BatchResponseSimplePublicObject|BatchResponseSimplePublicObjectWithErrors|error

Sample code:

leads:BatchInputSimplePublicObjectInputForCreate batchPayload = {
inputs: [
{associations: [], properties: {"hs_lead_name": "Alice Johnson"}},
{associations: [], properties: {"hs_lead_name": "Bob Williams"}}
]
};
leads:BatchResponseSimplePublicObject|leads:BatchResponseSimplePublicObjectWithErrors result =
check hsLeads->/batch/create.post(batchPayload);

Sample response:

{
"status": "COMPLETE",
"results": [
{
"id": "11111",
"properties": {"hs_lead_name": "Alice Johnson", "hs_object_id": "11111"},
"createdAt": "2024-01-15T10:30:00.000Z",
"updatedAt": "2024-01-15T10:30:00.000Z",
"archived": false
},
{
"id": "22222",
"properties": {"hs_lead_name": "Bob Williams", "hs_object_id": "22222"},
"createdAt": "2024-01-15T10:30:00.000Z",
"updatedAt": "2024-01-15T10:30:00.000Z",
"archived": false
}
],
"requestedAt": "2024-01-15T10:30:00.000Z",
"startedAt": "2024-01-15T10:30:00.001Z",
"completedAt": "2024-01-15T10:30:00.100Z"
}
Read a batch of leads by internal ID, or unique property values

Retrieves multiple lead records in a single request, identified by their HubSpot IDs or custom unique identifier properties.

Parameters:

NameTypeRequiredDescription
payloadBatchReadInputSimplePublicObjectIdYesBatch read input containing inputs (array of IDs), properties, and optionally propertiesWithHistory and idProperty.
queriesPostCrmV3ObjectsLeadsBatchReadReadQueriesNoOptional archived flag to include archived leads.

Returns: BatchResponseSimplePublicObject|BatchResponseSimplePublicObjectWithErrors|error

Sample code:

leads:BatchReadInputSimplePublicObjectId readPayload = {
inputs: [{id: "11111"}, {id: "22222"}],
properties: ["hs_lead_name"]
};
leads:BatchResponseSimplePublicObject|leads:BatchResponseSimplePublicObjectWithErrors batchResult =
check hsLeads->/batch/read.post(readPayload);

Sample response:

{
"status": "COMPLETE",
"results": [
{
"id": "11111",
"properties": {"hs_lead_name": "Alice Johnson", "hs_object_id": "11111"},
"createdAt": "2024-01-15T10:30:00.000Z",
"updatedAt": "2024-01-15T10:30:00.000Z",
"archived": false
},
{
"id": "22222",
"properties": {"hs_lead_name": "Bob Williams", "hs_object_id": "22222"},
"createdAt": "2024-01-15T10:30:00.000Z",
"updatedAt": "2024-01-15T10:30:00.000Z",
"archived": false
}
],
"requestedAt": "2024-01-15T10:30:00.000Z",
"startedAt": "2024-01-15T10:30:00.001Z",
"completedAt": "2024-01-15T10:30:00.050Z"
}
Update a batch of existing leads

Updates multiple existing lead records in a single API call. Each input must include the lead's id and the properties to update.

Parameters:

NameTypeRequiredDescription
payloadBatchInputSimplePublicObjectBatchInputYesBatch update input containing an inputs array; each element has id and properties.

Returns: BatchResponseSimplePublicObject|BatchResponseSimplePublicObjectWithErrors|error

Sample code:

leads:BatchInputSimplePublicObjectBatchInput updatePayload = {
inputs: [
{id: "11111", properties: {"hs_lead_name": "Alice Johnson-Updated"}},
{id: "22222", properties: {"hs_lead_name": "Robert Williams"}}
]
};
leads:BatchResponseSimplePublicObject|leads:BatchResponseSimplePublicObjectWithErrors updateResult =
check hsLeads->/batch/update.post(updatePayload);

Sample response:

{
"status": "COMPLETE",
"results": [
{
"id": "11111",
"properties": {"hs_lead_name": "Alice Johnson-Updated", "hs_object_id": "11111"},
"createdAt": "2024-01-15T10:30:00.000Z",
"updatedAt": "2024-01-16T09:00:00.000Z",
"archived": false
},
{
"id": "22222",
"properties": {"hs_lead_name": "Robert Williams", "hs_object_id": "22222"},
"createdAt": "2024-01-15T10:30:00.000Z",
"updatedAt": "2024-01-16T09:00:00.000Z",
"archived": false
}
],
"requestedAt": "2024-01-16T09:00:00.000Z",
"startedAt": "2024-01-16T09:00:00.001Z",
"completedAt": "2024-01-16T09:00:00.080Z"
}
Archive a batch of leads

Archives multiple lead records in a single API call.

Parameters:

NameTypeRequiredDescription
payloadBatchInputSimplePublicObjectIdYesBatch archive input with an inputs array of lead IDs to archive.

Returns: error?

Sample code:

check hsLeads->/batch/archive.post({
inputs: [{id: "11111"}, {id: "22222"}]
});
Create or update a batch of leads

Creates new leads or updates existing ones based on a unique identifier property. Records matching the identifier are updated; non-matching records are created.

Parameters:

NameTypeRequiredDescription
payloadBatchInputSimplePublicObjectBatchInputUpsertYesBatch upsert input containing inputs (each with idProperty, id, and properties).

Returns: BatchResponseSimplePublicUpsertObject|BatchResponseSimplePublicUpsertObjectWithErrors|error

Sample code:

leads:BatchInputSimplePublicObjectBatchInputUpsert upsertPayload = {
inputs: [
{idProperty: "hs_object_id", id: "11111", properties: {"hs_lead_name": "Alice Johnson"}},
{idProperty: "hs_object_id", id: "99999", properties: {"hs_lead_name": "New Lead"}}
]
};
leads:BatchResponseSimplePublicUpsertObject|leads:BatchResponseSimplePublicUpsertObjectWithErrors upsertResult =
check hsLeads->/batch/upsert.post(upsertPayload);

Sample response:

{
"status": "COMPLETE",
"results": [
{
"id": "11111",
"properties": {"hs_lead_name": "Alice Johnson", "hs_object_id": "11111"},
"createdAt": "2024-01-15T10:30:00.000Z",
"updatedAt": "2024-01-16T10:00:00.000Z",
"archived": false,
"new": false
},
{
"id": "33333",
"properties": {"hs_lead_name": "New Lead", "hs_object_id": "33333"},
"createdAt": "2024-01-16T10:00:00.000Z",
"updatedAt": "2024-01-16T10:00:00.000Z",
"archived": false,
"new": true
}
],
"requestedAt": "2024-01-16T10:00:00.000Z",
"startedAt": "2024-01-16T10:00:00.001Z",
"completedAt": "2024-01-16T10:00:00.090Z"
}
Filter, Sort, and Search CRM Objects

Searches lead records using filter groups, sorting criteria, and property projection. Supports complex queries combining multiple property filters with AND/OR logic.

Parameters:

NameTypeRequiredDescription
payloadPublicObjectSearchRequestYesSearch request with optional query (full-text), filterGroups (structured filters), sorts, properties, limit, and after (pagination cursor).

Returns: CollectionResponseWithTotalSimplePublicObjectForwardPaging|error

Sample code:

leads:PublicObjectSearchRequest searchReq = {
query: "Body Fit",
properties: ["hs_lead_name"],
filterGroups: [
{
filters: [
{propertyName: "hs_lead_name", operator: "CONTAINS_TOKEN", value: "Fit"}
]
}
],
'limit: 5
};
leads:CollectionResponseWithTotalSimplePublicObjectForwardPaging searchResult =
check hsLeads->/search.post(searchReq);

Sample response:

{
"total": 2,
"results": [
{
"id": "55555",
"properties": {
"hs_lead_name": "Body Fit Gold Member",
"createdate": "2024-01-10T08:00:00.000Z",
"lastmodifieddate": "2024-01-12T08:00:00.000Z",
"hs_object_id": "55555"
},
"createdAt": "2024-01-10T08:00:00.000Z",
"updatedAt": "2024-01-12T08:00:00.000Z",
"archived": false
},
{
"id": "66666",
"properties": {
"hs_lead_name": "Body Fit Silver Member",
"createdate": "2024-01-11T08:00:00.000Z",
"lastmodifieddate": "2024-01-11T08:00:00.000Z",
"hs_object_id": "66666"
},
"createdAt": "2024-01-11T08:00:00.000Z",
"updatedAt": "2024-01-11T08:00:00.000Z",
"archived": false
}
],
"paging": {}
}