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.
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.
{
“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.
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.
// 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.
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.
Step-6: Pass Deal Name & Stage
Once you are done with the addition of associations, you need to pass certain parameters, which will include:
You can follow the below screenshots to get this name.
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.
Step-9: Check the Deal
Now, go to the contact whose Vid you have used and check the deal that is created by you.
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.