HubSpot API Client

12.0.0 · active · verified Fri Apr 10

The `hubspot-api-client` is the official Python SDK for interacting with the HubSpot API (v3). It provides an easy-to-use interface to manage CRM objects, marketing events, files, and more. While the library follows semantic versioning, the underlying HubSpot API itself releases new major versions twice a year (March and September) using a date-based versioning scheme (e.g., /2026-03/), each supported for a minimum of 18 months.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the HubSpot client using a private app access token and create a new contact. Ensure your private app has the necessary 'crm.objects.contacts.write' scope.

import os
from hubspot import HubSpot
from hubspot.crm.contacts import SimplePublicObjectInputForCreate
from hubspot.crm.contacts.exceptions import ApiException

# Get your private app access token from environment variables for security
# In HubSpot, go to Settings -> Integrations -> Private Apps to create one.
ACCESS_TOKEN = os.environ.get('HUBSPOT_PRIVATE_APP_TOKEN', 'YOUR_PRIVATE_APP_TOKEN')

if not ACCESS_TOKEN or ACCESS_TOKEN == 'YOUR_PRIVATE_APP_TOKEN':
    print("Warning: HubSpot private app token not set. Please set HUBSPOT_PRIVATE_APP_TOKEN environment variable or replace 'YOUR_PRIVATE_APP_TOKEN'.")
    exit(1)

api_client = HubSpot(access_token=ACCESS_TOKEN)

try:
    simple_public_object_input_for_create = SimplePublicObjectInputForCreate(
        properties={"email": "example@example.com", "firstname": "Test", "lastname": "User"}
    )
    api_response = api_client.crm.contacts.basic_api.create(
        simple_public_object_input_for_create=simple_public_object_input_for_create
    )
    print("Contact created:")
    print(api_response.to_dict())
except ApiException as e:
    print(f"Exception when creating contact: {e}")
    # Common error: scopes not set for the private app. Check your private app permissions.

# Example of getting a contact by email
try:
    contact_by_email = api_client.crm.contacts.basic_api.get_by_email("example@example.com")
    print("Contact found by email:")
    print(contact_by_email.to_dict())
except ApiException as e:
    print(f"Exception when getting contact by email: {e}")

view raw JSON →