How HubSpot Works with Python Requests

As a B2B marketing agency, our development team regularly tackles projects where standalone applications need to be able to “speak” with one another. To solve this challenge, Python is our team’s go-to for creating such workflows and handling web requests.

Notably, Python’s Requests is a popular HTTP request package used by organizations such as Nike, Twitter, Spotify, Microsoft, and Amazon. Per Requests documentation, the package “allows you to send organic, grass-fed HTTP/1.1 requests, without the need for manual labor. There’s no need to manually add query strings to your URLs, or to form-encode your POST data.” In other words, the package allows developers to push and pull data to and from API endpoints in only a few lines of code.

Our dev team uses Requests in nearly every Python script that performs a web request to an API; scripts hitting HubSpot, Salesforce, Google Maps, Desk, and DigitalOcean APIs have all utilized the Requests package to push and pull necessary data points for varying tasks. Let’s take a look how Requests works with HubSpot in particular.

HubSpot Integrations

These days, our dev team works on a variety of HubSpot-related projects and often needs to integrate a completely separate application with HubSpot. The Requests package enables us to take on these projects by allowing for reliable, fast, and flexible scripts that utilize HTTP requests.

For example, one of our clients was looking to make the switch from Salesforce to HubSpot. The native HubSpot integration makes contact, company, and deal data easy to migrate via a front-end interface, but activity history (such as previous calls made, emails sent, or meetings scheduled on a contact, company, or deal) can only be migrated on the back-end via the HubSpot API. This meant that a script had to be written to push hundreds of thousands of individual activities on the associated contact, company, or deal into HubSpot.

The Requests package was heavily relied upon for several tasks:

  • HubSpot object IDs had to be correlated to Salesforce object IDs, as the Salesforce activity data had to be imported to the corresponding object (contact, company, or deal). GET requests via the Requests package were made to the APIs to associate IDs to activity data.
  • Each activity type (calls, emails, notes, etc.) had to be pushed to a seperate POST request to HubSpot, as each activity type utilizes a unique data object. Requests allowed for functions surrounding each activity type to be scripted to allow the appropriate activity to be imported to the corresponding HubSpot object.
  • When dealing with large data sets, error logging and handling is key for debugging when things go awry. The Requests package plays a big role in this process, as each POST response was written to a logging file. If the script hit an error that prevented it from running, we simply had to open the logging file and analyze the most recent response.

Simple Walkthrough

Let’s take a high-level look at the Requests package and why our team likes it so much.

Performing a GET request to an endpoint to receive JSON data can be done in just a few lines of code:

import requests
req = requests.get('')

Here, we’re assigning the GET request to a variable, req. Then, we’re printing req and utilizing Request’s built-in JSON function, .json(), which will show the GET request data as a JSON object on the console.

To see just the response code, simply print the req variable. If successful, your output will be:

<Response [200]>

Performing a POST request also requires only a few lines of code. The only difference here is that we have to pass a data object to the request function, using the built-in JSON package:

import requests, json
data = { "key":  "value" }
req ='', data=json.dumps(data))

Here, we assign our data object to a variable, data, and push it to the request as a JSON object. A successful request here will result in a 200 Response. We can also print both the response code and response JSON here using the same methods outlined in the GET request.

As you can see, Requests makes HTTP requests quite simple. While scripts can easily snowball into hundreds or thousands of lines of code, the Requests package keeps the HTTP request calls straightforward, allowing developers to focus more on data manipulation and less on writing the actual request.

Final Thoughts

As a marketing agency, we lean heavily on our dev team to make things work. When clients come to us asking to integrate two separate applications, we’re confident our developers can make it happen in a timely manner. The Requests package provides our developers with a tool that allows for simple HTTP requests, no matter how complex the workflow, so they can develop intraquate, flexible workflows, making us look like superheros to our clients.

Contact us today for more information about Requests and HubSpot.

Read more blogs.

Take me home!