Skip to content

A Developer's Guide to HubSpot CRM Objects: Deals Object

Welcome back to our series, "A Developer's Guide to HubSpot CRM Objects." Our last post introduced you to the company object and API for HubSpot. However, in the dynamic world of Customer Relationship Management (CRM), having a robust system to track and manage deals is crucial for businesses aiming to thrive. The HubSpot CRM offers powerful tools to streamline sales processes, among which the deals object and API stand out for their versatility and integration capabilities. This guide dives deep into the deals object, explores the deals API, and provides practical tips for developers leveraging these tools in their projects.

Understanding the Deals Object

At its core, the deals object in HubSpot represents a potential revenue-generating opportunity with a contact or company. It is a pivotal component of the sales pipeline, enabling sales teams to track deals through various stages until they are won or lost. Each deal in HubSpot is associated with properties that capture essential information such as deal name, amount, stage, and expected close date, among others.

Deal Associations

In HubSpot, deal associations play a crucial role in organizing and managing the relational data across different entities within the CRM. Associations allow you to connect deals with other relevant CRM objects such as contacts, companies, activities (tasks, emails, calls), and line items (products or services involved in a deal). This connectivity provides a more integrated view of how various elements interact within the sales process, offering deeper insights and aiding in comprehensive management.

Associating Activities and Deals

Activities in HubSpot include any customer interactions or internal tasks, such as emails, calls, meetings, or custom activities. Activities are synonymous with HubSpot engagementsAssociating these activities with deals helps track a particular sales transaction's interaction history and communication timeline. This is crucial for sales teams to maintain context, follow up appropriately, and drive deals toward closure.

You can associate various activities directly with a deal when creating or updating a deal. This is done through the associations object in the API request. When you post a new deal, you can include associations to existing records, including activities like meetings or notes. Here is how creating a new deal with activity associations with the deals API would look:

POST /crm/v3/objects/deals { "properties": { "dealname": "New Deal", "dealstage": "contractsent", "pipeline": "default", "amount": "1500.00", "closedate": "2023-12-07T16:50:06.678Z", "hubspot_owner_id": "910901" }, "associations": [ { "to": { "id": "201" }, "types": [ { "associationCategory": "HUBSPOT_DEFINED", "associationTypeId": 5 // Assuming 5 is the association type ID for the desired activity } ] }, { "to": { "id": "301" }, "types": [ { "associationCategory": "HUBSPOT_DEFINED", "associationTypeId": 3 // Assuming 3 is the type ID for another type of activity or record } ] } ] }

In this example, toObjectId specifies the ID of the activity you want to associate with the deal, and associationTypeId determines the type of association based on HubSpot's predefined values or those retrieved via this endpoint: /crm/v4/associations/{fromObjectType}/{toObjectType}/labels.

Retrieving Deals with Associated Activities

To fetch deals and see which activities are associated with them, you can use the associations parameter in your GET request to specify which associated records you want to retrieve:

GET https://api.hubapi.com/crm/v4/associations/deals/{toObjectType}/labels

This will return an associationTypeId indicating the association type between the deal or activity.

Updating Deal Associations

To add or update associations after a deal has been created, you can use a PUT request to modify the deal's associated records:

PUT https://api.hubapi.com/crm/v3/objects/deals/{dealId}/associations/{toObjectType}/{toObjectId}/{associationTypeId}

This request allows you to link new activities to an existing deal, ensuring that all relevant interactions are captured and associated correctly.

Removing Associations

If you need to disassociate an activity from a deal, you can make a DELETE request:

DELETE https://api.hubapi.com/crm/v3/objects/deals/{dealId}/associations/{toObjectType}/{toObjectId}/{associationTypeId}

While the functionality is powerful, managing these associations can sometimes be complex, especially when dealing with large numbers of deals and activities. Common challenges include ensuring data consistency, managing bulk associations efficiently, and handling the API's response limits.

To test out these requests, check out the associated Postman collection here.

Associating Line Items and Deals

HubSpot's line items are critical for managing transaction details within deals, especially for sales involving products or services. Line items are individual instances of products, and when a product is attached to a deal, it becomes a line item. Line items can include specifics such as pricing, quantity, and descriptions, which are crucial for invoicing and revenue tracking. From an API perspective, managing line items and their associations with deals involves several endpoints that allow you to create, update, retrieve, and associate line items directly to deals within HubSpot's CRM.

The HubSpot Line Items API allows you to manage individual line items associated with deals, providing detailed control over transaction-specific data, which is essential for sales processes. Before associating line items with deals, you first need to create them. Here's how you can create a line item using the API, assuming there is already a product existing:

POST /crm/v3/objects/line_items { "properties": { "name": "New standalone line item", "quantity": "10", "price": "20.00", "hs_product_id": "existing_product_id" // Replace with actual product Id }, "associations": { "deal": [ "12345" // This is the dealId ], "quote": [ "67890" // This is the quoteId (optional) ] } }

This endpoint creates a new line item with specified properties such as name, quantity, price, and description.

Associating Line Items with Deals

Once you have created line items, you can associate them with an existing deal. This is done using the associations endpoint in the HubSpot API.

PUT https://api.hubapi.com/crm/v3/objects/deals/{dealId}/associations/line_item/{lineItemId}/{associationTypeId}

The associationTypeId can be found or confirmed via the Associations API, which lets you retrieve possible associations between different object types.

Retrieving Deals with Associated Line Items

To see the line items associated with a particular deal, you can retrieve the deal with its associated line items included.

GET https://api.hubapi.com/crm/v3/objects/deals/{dealId}?associations=line_item

This request will return the deal information and any associated line items, allowing you to view what products or services are included in a specific deal.

Updating and Deleting Associations

If you need to update or delete an association between a deal and a line item, you can use the API to modify or remove the relevant association.

To update:

PUT https://api.hubapi.com/crm/v3/objects/deals/{dealId}/associations/line_item/{lineItemId}/{associationTypeId}

To remove:

DELETE https://api.hubapi.com/crm/v3/objects/deals/{dealId}/associations/line_item/{lineItemId}/{associationTypeId}

These endpoints allow you to manage the lifecycle of deal and line item associations, ensuring that the deal records stay current and accurate as the sales process evolves.

To test out these requests, check out the associated Postman collection here.

Leveraging the Deals API

The deals API is a powerful interface that allows developers to programmatically interact with the deals object within HubSpot. It supports various operations, including creating, updating, and retrieving deals and managing associations with contacts and companies.

Here’s a breakdown of why the deals object and API are helpful and what they are intended for.

  • Deal Management: Developers can create, update, retrieve, and delete deal records. This enables seamless integration of HubSpot with other business systems like ERP or custom backend systems where deal-related data needs to be synchronized.
  • Pipeline Management: The API allows manipulation of deal stages within sales pipelines, helping to automate workflow transitions based on external events or internal rules.
  • Data Synchronization: It supports the synchronization of deal data across platforms, ensuring all stakeholders have up-to-date information on sales progress, forecasts, and outcomes.
  • Reporting and Analytics: By managing deal data through the API, developers can facilitate reporting and analytics on sales activities, helping businesses make data-driven decisions to optimize their sales processes.

Example Usage

HubSpot's deals API is designed to help developers manage sales processes by programmatically interacting with deal records within the HubSpot platform. This API is integral to HubSpot's broader CRM suite, facilitating the automation and synchronization of sales activities, deal tracking, and pipeline management.

Below, we have provided examples of using the deals object to illustrate a use case requested by our developer community.

Use Case: Attach a file to a Deal using the Deals API

Step 1: Obtain OAuth Access

To use the HubSpot Deals API, developers must authenticate through OAuth or a private app access token. OAuth is suitable for public and private applications, while private app access tokens are exclusive to private applications.

First, create an application in HubSpot, which can be designated as either public or private. The choice of authentication method—OAuth for both app types or a private app access token for private apps—will depend on the nature of your application. We will use OAuth to authenticate our requests for this use case example.

Step 2: Upload the file using the File Manager API

The files tool is meant to manage and store files in HubSpot. Files hosted in HubSpot can be uploaded and used in both HubSpot and external content. So, first, upload the file to HubSpot's file system using the Files Manager API. You will use a POST request to the Files API endpoint.

POST https://api.hubapi.com/files/v3/files

Headers:

Authorization: Bearer YOUR_ACCESS_TOKEN Content-Type: multipart/form-data

This will upload the file and return a response that includes the fileId.

Step 3: Create a Note with the Attached File and Associate with Deal

Once the file is uploaded and you have the fileId, you can attach it to a deal record using the engagements API with an engagement type.

Engagement types include notes, calls, tasks, meetings, etc. We’ll leverage the engagement-type notes for this example and use the Notes API to create a note and attach the previously uploaded file by referencing the fileId.

POST https://api.hubapi.com/crm/v3/objects/notes

Headers:

Authorization: Bearer YOUR_ACCESS_TOKEN Content-Type: application/json

JSON request body example:

{ "properties": { "hs_timestamp": "current timestamp in ISO 8601 format", "hs_note_body": "Description of the note and context for the attachment.", "hs_attachment_ids": "122692044085"// This is the fileId }, "associations": [ { "to": { "id": "7891023" // This is the dealId }, "types": [ { "associationCategory": "HUBSPOT_DEFINED", "associationTypeId": "214" // This is the associationTypeId for notes with deals } ] } ] }

You will need the correct associationTypeId to associate notes with deals. This ID can be found in the Associations v4 API documentation, and the associationTypeId for note to deal is 214.

Step 4: Verify the Note Attachment

To ensure the file and note have been successfully attached to the deal, you may retrieve the deal or the note from their respective APIs to check the associations and attachments.

Following these steps allows you to systematically attach a file to a note and link it to a deal in HubSpot, which is useful for keeping all related documents and communications organized within your CRM system. To test out this example yourself, check out this Postman collection.

Conclusion

HubSpot's deals object and API offer developers a powerful toolkit for managing sales opportunities and integrating CRM functionalities into applications. By understanding the deals object, mastering the API, and following best practices, developers can unlock the full potential of HubSpot to drive sales and business growth.