How to Get Contacts from the HubSpot CRM API in Python (2023)

Engineering

How to Get Contacts from the HubSpot CRM API in Python (1)

Lucien Chemaly

March 30, 2023

HubSpot is an all-in-one CRM platform with a comprehensive suite of tools and resources that helps businesses to connect their marketing, sales, and service efforts. Its integrated approach as well as its user-friendliness, flexibility, and top-notch customer service make HubSpot a popular choice among businesses of all sizes.

Developers may need to integrate with HubSpot to access and manage customer data, such as contacts, to personalize and automate marketing efforts. For example, a developer could use the HubSpot API to retrieve contact information and use it to trigger personalized email campaigns based on a contact's behavior on the company's website.

This article shows you how to use the Python programming to manage contacts on HubSpot. It offers detailed steps to efficiently access, retrieve, and generate contacts on the HubSpot platform using Python.

The code used in this tutorial can be found in this GitHub repository.

Setting Up Your HubSpot Account

Before getting started on the tutorial, you need to get your HubSpot account set up.

If you don't have a HubSpot account, sign up for a free or demo account on the HubSpot website.

How to Get Contacts from the HubSpot CRM API in Python (2)

Follow the instructions to sign up. Once your account is created, you'll be logged in to HubSpot. If you already have an account, simply log in.

How to Get Contacts from the HubSpot CRM API in Python (3)

(Video) Hubspot Add Contacts Using API

Create Contacts

Next, you need to create several contacts to be used later in this tutorial. Use this exercise to also familiarize yourself with the HubSpot platform.

To create a new contact, navigate to Contacts from the top navigation bar, then click on Create contact in the top right corner of the page. Fill out the form with the necessary information, including first name, last name, and email. Click on the Create button to submit the contact.

How to Get Contacts from the HubSpot CRM API in Python (4)

To delete a contact, click on the contact in the list of contacts in the HubSpot dashboard. Click on Actions and then Delete to remove the contact permanently.

How to Get Contacts from the HubSpot CRM API in Python (5)

Create four to five contacts with whatever details you wish to use later in this tutorial.

How to Get Contacts from the HubSpot CRM API in Python (6)

Create a HubSpot App and Generate an Application Token

Lastly, you need to create a HubSpot app to get a token for authenticating requests made to your HubSpot account, which you'll use later in the tutorial.

Click on Settings in the top navigation bar, then navigate to the Private Apps section under Integrations. Click on Create a private app and fill in the information required.

How to Get Contacts from the HubSpot CRM API in Python (7)

Next, toggle to Scopes, search for contacts, then check all the boxes for Read and Write related to contacts. This will give your application permission to have access to your contacts.

How to Get Contacts from the HubSpot CRM API in Python (8)

Lastly, click on Create app in the top right corner.

You should get a prompt that states Your private app was created. This prompt contains your application token. Click on Copy and save the token somewhere safe because you'll use it later.

(Video) Integration and API Workshop - July 2020

How to Get Contacts from the HubSpot CRM API in Python (9)

Using Python with HubSpot

With all the setup out of the way, you can now start the tutorial.

Prerequisites

To follow along with this tutorial, you need the following installed on your machine:

Once you have these installed, go to your terminal or shell and install the requests library using the following command:

pip3 install requests

Note: You might have already installed this library with Python. To avoid any issues, it's best to make sure it's installed before proceeding to the next step.

Getting HubSpot Contacts Using the requests.get Method

Create a file named get_hubspot_contacts.py and add the following code to it:

import requests# Set the API endpoint and headersendpoint = "https://api.hubapi.com/contacts/v1/lists/all/contacts/all"headers = {"Authorization": "Bearer Your_Token"}# Make a GET request to the APIresponse = requests.get(endpoint, headers=headers)# Parse the JSON data from the responsedata = response.json()# Loop through the contacts and print their informationfor contact in data["contacts"]: print(contact)

Replace <code class="blog_inline-code">Your_Token</code> with the token you got from the previous step.

In the code above, you start by importing the needed library, requests, to make the HTTP call. You then specify the endpoint URL and the headers that are related to your HubSpot account. Next, you call the HubSpot endpoint using the <code class="blog_inline-code">request.get()</code> method, and finally, if the result is successful, you display the contacts by using a <code class="blog_inline-code">for loop</code> and the <code class="blog_inline-code">print()</code> function.

Run the code from the terminal or command line using the following command:

python3 get_hubspot_contacts.py

You should see the contacts you created on HubSpot as the output.

How to Get Contacts from the HubSpot CRM API in Python (10)

(Video) Hubspot API Integration Adding Contacts directly from Website made in PHP

Creating HubSpot Contacts Using the requests.post Method

Create a file named create_hubspot_contact.py and add the following code to it:

import requests# Set the API endpointurl = "https://api.hubapi.com/crm/v3/objects/contacts"# Set the request headersheaders = { "Content-Type": "application/json", "Authorization": "Bearer Your_Token"}# Set the contact informationcontact = { "properties": { "email": "example@python.com", "firstname": "Tutorial", "lastname": "Test1" }}# Send the requestresponse = requests.post(url, json=contact, headers=headers)if response.status_code == 201: print("Contact Created Successfully")else: print("Failed To Create Contact")

Replace Your_Token with the token you got earlier.

In the code above, you again start by importing the needed library, requests, to make the HTTP call, and then you specify the endpoint URL and the headers that are related to your HubSpot account. Next, you define the contact information that you want to create, after which you call the HubSpot endpoint using the request.post() method. If the result is successful, you should see the contact created on your HubSpot contacts.

Run the code from the terminal or command line using the following command:

python3 create_hubspot_contact.py

You should see "Contact Created Successfully" as the output.

How to Get Contacts from the HubSpot CRM API in Python (11)

If you check the HubSpot portal, you should see your newly created contact.

How to Get Contacts from the HubSpot CRM API in Python (12)

Creating a Webhook Using Python to Fetch HubSpot Contacts

If you want to automate the process of retrieving new or updated contact information and integrating it with other systems or applications, using a webhook with Python to fetch HubSpot contacts can be useful.

In this section, you'll use Flask to create an endpoint that will serve as an API, or webhook, to call to get contacts. Flask is a lightweight Python framework used to create APIs.

Install the Flask library with the following command:

pip3 install flask
(Video) 3 Ways to Retrieve Properties for a Record Using the HubSpot APIs

Next, create a file named app.py and add the following code to it:

from flask import Flaskimport requestsapp = Flask(__name__)@app.route("/contacts", methods=["GET"])def get_contacts(): # Set the API endpoint and headers endpoint = "https://api.hubapi.com/contacts/v1/lists/all/contacts/all" headers = {"Authorization": "Bearer Your_Token"} # Make a GET request to the API response = requests.get(endpoint, headers=headers) # Parse the JSON data from the response data = response.json() return dataif __name__ == "__main__": app.run(debug=True)

Remember to replace <code class="blog_inline-code">Your_Token</code> with the token you got earlier.

In the code above, you start by importing the <code class="blog_inline-code">requests</code> library to make the HTTP call and the <code class="blog_inline-code">flask</code> library to create and run your endpoint. You then define the route for the HTTP GET request of the endpoint, which is <code class="blog_inline-code">/contacts</code>. The route calls the <code class="blog_inline-code">get_contacts()</code> function, which calls the HubSpot endpoint and returns the contacts. This route defines the endpoint URL and the headers related to your HubSpot account.

Run the code from the terminal or command line using the following command:

flask run

How to Get Contacts from the HubSpot CRM API in Python (13)

Next, open your browser and paste in the following URL: http://localhost:5000/contacts. You should see the contacts displayed in your browser.

How to Get Contacts from the HubSpot CRM API in Python (14)

Managing Integrations with Merge's Unified API

But what if you are not proficient in programming languages like Python? Or what if HubSpot is just one of several integrations you and your team need to build and maintain?

That's where a tool like Merge can help.

Merge.dev offers a unified API for all suites of integrations by providing a single API endpoint that connects to multiple platforms and services—including HubSpot. The API endpoint acts as a bridge between the different platforms and services, allowing users to interact with all of them using a single API call.

To achieve this, Merge uses a system of plugins that allows it to connect to different platforms and services. These plugins act as a bridge between the Merge API endpoint and the various platforms and services. Each plugin is responsible for handling the specific communication protocols, APIs, and data structures of the platform or service it connects to.

When a user makes an API call to Merge, the call is routed to the appropriate plugin based on the information provided in the call. The plugin then processes the call and returns the appropriate response. This allows users to interact with all the different platforms and services through a single, unified API endpoint.

(Video) How to Create Private App on HubSpot and Retrieve Access Token? - QT14

Merge also allows developers to build custom plugins to connect to new platforms and services, which can be added to the API endpoint. This means that Merge is constantly expanding its integration capabilities and support for new platforms and services.

Conclusion

This article showed you how to use Python to interact with the HubSpot CRM to fetch and create contacts. You learned how to create a webhook to automatically retrieve HubSpot contacts when the endpoint is accessed.

You also learned how the Merge unified API allows you to connect with over forty different platforms, including HubSpot, through a single call, eliminating the need for multiple integrations. Simplifying the process of integrating with various tools and services allows you to focus on your core product, leading to increased efficiency and improved bottom line.

Videos

1. NodeJS Hubspot API Basics (Owners & Deals API)
(optikalefx)
2. Migrate HubSpot Data Structure with my custom python script!
(Lloyd Solves)
3. Coded HubSpot Automation Workshop
(Aptitude 8 - Build it better on HubSpot)
4. Solving Problems with AI: Unformatted Contact Data For CRM Import
(say.digital)
5. Hubspot and Twilio Integration | How to Get Contacts from Hubspot to Twilio
(ApiX-Drive [SaaS Service])
6. How to efficiently segment your contact database | HubSpot Basic Training
(Huble Digital)
Top Articles
Latest Posts
Article information

Author: Roderick King

Last Updated: 02/04/2023

Views: 5839

Rating: 4 / 5 (51 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Roderick King

Birthday: 1997-10-09

Address: 3782 Madge Knoll, East Dudley, MA 63913

Phone: +2521695290067

Job: Customer Sales Coordinator

Hobby: Gunsmithing, Embroidery, Parkour, Kitesurfing, Rock climbing, Sand art, Beekeeping

Introduction: My name is Roderick King, I am a cute, splendid, excited, perfect, gentle, funny, vivacious person who loves writing and wants to share my knowledge and understanding with you.