Hubspot Integration Guide – Using Hubspot API to Integrate

What is Hubspot?

HubSpot is an all-in-one business automation tool that helps an Organization to attract clients, convert them into a lead and close the deal effectively bringing the best results for a business. It has been described as unique because it strives to provide its customers with an all-in-one approach.

Hubspot Integration Guide  Using Hubspot API to Integrate

Features of Hubspot

  • Blogging
  • Calls-to-Action and Landing Pages
  • Email Marketing
  • Marketing Automation
  • Lead Management
  • Analytics
  • CMS
  • Social Media
  • SEO
  • Salesforce Integration
  • Ads

Hubspot Integration

Hubspot Authentication using OAuth 2.0

There are two means of authentication provided by Hubspot API, OAuth, and API key. It is an easy and fast process to authenticate using API key, but for a secured and commercial use Hubspot highly recommends OAuth.

Almost all the endpoints support both OAuth and API keys, there are very few endpoints which only support OAuth ( like timeline events ). Irrespective of the authentication used, the requests are identical and would return the same results.

Create a Developer Account

The best way to get started is by creating developer accounts. From this developer account, you can spin up test accounts that have their own API keys. To get started with OAuth you need to create an application using a developer account, you can create a developer account here.

Create a Developer Account

Create an App in Hubspot

You need to create an app in order to initiate the OAuth connection. When you authenticate your integration using OAuth, the details for your app are shown to the user at the time of authorizing the connection. Before creating an App in Hubspot please make sure that you already have a developer account. To create an App, click on the “Apps” tab on the navigation and you should get a screen like below.

Create an App in Hubspot

While creating an app, you’ll get a screen showing the settings for your new app. The name of your app will be displayed to the users when they authenticate your app with HubSpot. You will additionally see the other settings for your app, as well as the description, and choices for adding support contact information. You’ll also have the ability to upload the logo and a wordmark that will be displayed to HubSpot users.

settings for your app

You will also find the Auth settings for your app, including the client ID, client secret and scopes used by your app. These items are mandatory for Authorization.

client secret and scopes

Initiating the Integration

In this article, I’m using Flask ( A Python framework ) for the authentication with Hubspot. I have created a small flask application for this authentication, click here for the Code. If you are new to Flask then follow the instructions from this tutorial. Once you save the code and run the file, please follow the steps below:

step 1: 

In this step, you will get the “Authorization code”. The URL for this function is ‘http://127.0.0.1:5000/get-auth-code’. Before executing the file you need to update values of the following variables (CLIENT_ID and CLIENT_SECRET). Once you hit the above URL it will redirect you to a page where you need to select an application as shown in the picture below.

choose an account

After selecting the application, you will be asked to grant access for Integration.

request for integration permissions

As soon as you click on the “Grant access” button, you will be redirected to a URL having the auth_code as a parameter. The URL looks like this as shown below.

grant access

Please copy the code somewhere, as it is mandatory for the further steps.

step 2:
In this step, you will get the “access token“. The URL for this function is ‘http://127.0.0.1:5000/get-access-token’. Before executing the file you need to update the value of the variable named ‘CODE’. Once you hit the above URL it will return a JSON data with access_token, expires_in and refresh_token. Please copy the access_token and refresh_token somewhere. Remember, the access token will gets expired in 6 hours.

access token

step 3:

As the access token will no longer valid after 6 hours, you need to get a new access token every 6 hours. In this step, you will refresh the “access token” using “refresh token”. The URL for this function is ‘http://127.0.0.1:5000/get-refresh-token’. Before executing the file you need to update the value of the variable named ‘REFRESH_TOKEN’. Once you hit the above URL it will return a JSON data with access_token, expires_in and refresh_token.

After getting the access token, you can start retrieving and manipulating data of different objects in Hubspot like contact, company, Deal and many more. That is all about the introduction of Hubspot and initiating integration with Hubspot using OAuth 2.0.

from flask import Flask, redirect
import requests
import json

app = Flask(__name__) 

CLIENT_ID = 'ENTER CLIENT ID HERE'
CLIENT_SECRET = 'ENTER CLIENT SECRET HERE'
CODE = 'ENTER CODE HERE'
REFRESH_TOKEN = 'ENTER REFRESH_TOKEN HERE'


@app.route('/get-auth-code') 
def get_oauth_code():
    CLIENT_ID = 'ENTER CLIENT ID HERE' # update the client Id  
    url = f'https://app.hubspot.com/oauth/authorize?client_id={CLIENT_ID}&scope=contacts&redirect_uri=https://127.0.0.1:5000'
    return redirect(url, code=302)

@app.route('/get-access-token') 
def get_access_token():

    data = {
        'grant_type' : 'authorization_code',
        'client_id' : CLIENT_ID,
        'client_secret' : CLIENT_SECRET,
        'scope' : 'contacts',
        'redirect_uri' : 'https://127.0.0.1:5000',
        'code' : CODE
    } 

    headers = {'Content-Type' : 'application/x-www-form-urlencoded;charset=utf-8'}

    url = 'https://api.hubapi.com/oauth/v1/token'
    resp = requests.post(url, data = data, headers = headers)
    return resp.json()

@app.route('/refresh-access-token') 
def refresh_access_token():
    data = {
        'grant_type' : 'refresh_token',
        'client_id' : CLIENT_ID,
        'client_secret' : CLIENT_SECRET,
        'refresh_token' : REFRESH_TOKEN
    } 

    headers = {'Content-Type' : 'application/x-www-form-urlencoded;charset=utf-8'}

    url = 'https://api.hubapi.com/oauth/v1/token'
    resp = requests.post(url, data = data, headers = headers)
    return resp.json()


if __name__ == '__main__': 
    app.run(debug=True)

Contact us for Hubspot Integration

If you have a problem related to Hubspot integration that you want to solve – engage Dhruvsoft Hubspot team. Contact us for more information.