If you have read my previous blog, then you might be familiar with the term serverless functions and its usage in HubSpot.

Well! Serverless functions provide a way to create server-side code that further helps you in interacting with HubSpot and other third-party services via APIs. I mostly prefer to work with serverless functions as it allows me to work without worrying about the overhead associated with server management. 

As we know that HubSpot provides a lot of different features with serverless functions, but today we will mainly focus on the most common and highly essential feature, i.e., deals.

Deals are the way to know how much business has been done by the company and what needs to be focused on. The more successful deals are made with the customers, the more growth you will see in your company. 

Deals can be created between companies and are assigned to the contacts. Every deal has its amount, and some will include products and line items that are aligned within the deal.

Why-Serverless-functions-in-HubSpot

As a developer, I will not simply teach you how to create deals in HubSpot CMS, but we will discuss how we can create them with API's. Along with this, we will discuss the code of API inside the node.

Are you ready to jump into the process of creating deals in HubSpot using serverless functions.? Let's get started!

Steps to create Deals in HubSpot with Serverless Functions

Follow these steps to create deals with serverless functions.

Step-1: Update Package

Firstly, for creating deals in serverless functions, you need to install a certain package for which you need to update our package.json file with all the other packages. 

update-packages

{

  “name”: “serverless”,

  “version”: “1.0.0”,

  “description”: “”,

  “main”: “index.js”,

  “scripts”: {

    “test”: “echo \”Error: no test specified\” && exit 1″

  },

  “author”: “”,

  “license”: “ISC”,

  “dependencies”: {

    “axios”: “*”,

    “express”: “*”,

    “follow-redirects”: “*”,

    “http”: “*”,

    “mailgun-js”: “*”,

    “paypal-rest-sdk”: “^1.8.1”,

    “request”: “*”,

    “stripe”: “*”,

    “localStorage” : “*”,

    “util” : “*”

  }

}

If you don't know how to install npm packages in serverless functions, then you can always read my previous blog to get more information. 

Step-2: Create an Endpoint

Once all these packages are installed, we need to create one endpoint inside our serverless.json file. After this, you need to create the js file in which our deal code will reside.

Now, here you might be thinking about which type or post type your endpoint should be. If so, then I would like to say that it totally depends on one's own choice. 

add-endpoints

Step-3: Focus on Code

Once you are done with both the above steps, then you need to focus on the code that we are going to use. 

focus-on-code

// Require axios to make API requests in the function

// Redirect the user

const util = require(‘util');

const request = util.promisify(require(‘request'));

const axios = require(“axios”);

exports.main = (body, sendResponse) => {

    var cou_list = ‘abc';

    const HAPI_KEY = ‘hub-api-key';

    const vid = 1301;

    var dealname = ‘abcdef';

    var time = Date.now(); 

                //var request = require(“request”);

                var options = { method: ‘POST',

                  url: ‘https://api.hubapi.com/deals/v1/deal',

                  qs: { hapikey: HAPI_KEY },

                  headers: 

                   { ‘Content-Type': ‘application/json' },

                  body: 

                   { associations: { associatedCompanyIds: [ ], associatedVids: [ vid ] },

                     properties: 

                      [ { value: dealname, name: ‘dealname' },

                        { value: ‘checkout_abandoned', name: ‘dealstage' },

                        { value: ‘default', name: ‘pipeline' },

                        { value: time, name: ‘closedate' },

                        { value: 425, name: ‘amount' },

                        { value: ‘newbusiness', name: ‘dealtype' } ] },

                    json: true };

  request(options, function (error, response, body) {

                  if (error) throw new Error(error);        

                  sendResponse({body: ‘body', statusCode: 200});

                });

sendResponse({body: ‘exit', statusCode: 200});

};

Step-4: Add the Hub API Key

Now, it's time to add the hub API key of your account. But make sure it is added to the same account where you want to create the deal. 

Don't know how to get the HubSpot API key? 

Well! For this, you need to navigate to settings> integrations> API key. 

Now, here, you can copy your API key and proceed further. For more insight, you can refer to the below-added screenshot. 

hubspot-api-key

Step-5: Assign the Deal

Now, you can assign the deal to a contact/ company or both. For assigning the deal to a contact, you need to add the Vid of that contact in the associated vid parameter, whereas to assign the deal to the company, you need to add the company id inside the associated company ids array.

To know the Vid of the contact, you just need to open the contact and can see its URL. 

vid-of-contact

Step-6: Pass Deal Name & Stage

Once you are done with the addition of associations, you need to pass certain parameters, which will include:

  • Deal name (could be any of your product name)
  • Deal stage(deal pipeline name)

You can follow the below screenshots to get this name. 

click-on-deal
create-deal
  • Pipeline(where you want to send your deal(here we are using the default)
select-pipeline
  • Amount of deal
  • Close data
  • Deal type

Step-7: Use the Command

Once you are done with all the settings, you need to deploy this function, for which you can use the following command. 

“hs functions deploy serverless/serverless.functions”

Step-8: Run the Server

After deploying the function, you can either run the server locally or can do it in your HubSpot. 

deploy-function

Step-9: Check the Deal

Now, go to the contact whose Vid you have used and check the deal that is created by you. 

check-created-deal

All done! Congratulations! You have successfully created deals inside serverless functions in HubSpot. Furthermore, if you want to get more information about deal API, then you can refer to this document- https://legacydocs.hubspot.com/docs/methods/deals/create_deal.

The Final Say 

Creating a deal and tracking it in HubSpot using serverless functions is one of the best ways to associate the companies and contacts, which further helps in generating leads. Indeed, creating deals is not a cakewalk, but the above-mentioned steps will surely help you in making the best thing possible. 

So, what are you waiting for?

Get ready to create a deal in HubSpot without investing much time and effort.

Also, want to take the help of experts in making the best use of HubSpot CMS?

Feel free to contact us.

Frequently Asked Questions

To create a deal on HubSpot, start by going to the “Sales” section, then select “Deals.” At the top right, click “Create deal.” In the right panel that appears, you can fill in all the information about the deal, such as the deal name, associated contacts or companies, the amount involved, and the deal's stage. This helps you keep track of your sales and manage your business effectively. Creating deals manually in HubSpot allows you to organize and monitor your sales opportunities efficiently.

You can set up an automatic deal in HubSpot by creating a workflow in the Marketing section. First, you'll want to set the “Lifecycle Stage” property to “SQL” to trigger the deal creation. Then, you can use the Workflows tool to generate a deal with a title format like “TRIAL-CompanyName.” This approach ensures that when leads reach the SQL stage, deals are automatically generated with specific titles, streamlining your sales and marketing processes.

HubSpot's serverless functions have certain limits for efficient performance. These include a maximum of 50 secrets allowed per account to securely store sensitive information, a memory limit of 128MB for each function, and a cap of 100 endpoints per HubSpot account. Staying within these boundaries is important to ensure smooth and effective operations when using serverless functions within the HubSpot platform.

To create a deal pipeline in HubSpot, start by logging into your account. Once logged in, click on the settings icon located in the main navigation bar. In the left sidebar menu, find and select “Objects” and then “Deals.” After that, click on the “Pipelines” tab. In the dropdown menu labeled “Select a pipeline,” choose the “Create pipeline” option. This will initiate the process of setting up a new deal pipeline in your HubSpot account.