Skip to main content

hubspot-api-python

Python HubSpot API v3 SDK(Client) files and sample apps

Sample Applications can be found in sample-apps folder

Documentation

See the API docs.

Installation

If you just want to use the package, run:

pip install --upgrade hubspot-api-client

Requirements

Make sure you have Python 3.5+ and pip installed.

Quickstart

Configuring HubSpot client

from hubspot import HubSpot

api_client = HubSpot()
# or with api_key
api_client = HubSpot(api_key='your_api_key')
# or with access_token
api_client = HubSpot()
api_client.access_token = 'your_access_token'

OAuth API

Obtain OAuth2 access token:

from hubspot.auth.oauth import ApiException

try:
    tokens = api_client.auth.oauth.default_api.create_token(
        grant_type="authorization_code",
        redirect_uri='http://localhost',
        client_id='client_id',
        client_secret='client_secret',
        code='code'
    )
except ApiException as e:
    print("Exception when calling create_token method: %s\n" % e)

CRM API

Create contact:

from hubspot.crm.contacts import SimplePublicObjectInput
from hubspot.crm.contacts.exceptions import ApiException

try:
    simple_public_object_input = SimplePublicObjectInput(
        properties={"email": "email@example.com"}
    )
    api_response = api_client.crm.contacts.basic_api.create(
        simple_public_object_input=simple_public_object_input
    )
except ApiException as e:
    print("Exception when creating contact: %s\n" % e)

Get contact by id:

from hubspot.crm.contacts import ApiException

try:
    contact_fetched = api_client.crm.contacts.basic_api.get_by_id('contact_id')
except ApiException as e:
    print("Exception when requesting contact by id: %s\n" % e)

Get custom objects page:

from hubspot.crm.objects import ApiException

try:
    my_custom_objects_page = api_client.crm.objects.basic_api.get_page(object_type="my_custom_object_type")
except ApiException as e:
    print("Exception when requesting custom objects: %s\n" % e)

Get all:

get_all method is available for all major objects and works like

all_contacts = api_client.crm.contacts.get_all()

Please note that pagination is used under the hood to get all results.

CMS API

Get audit logs:

from hubspot.cms.audit_logs import ApiException

try:
    audit_logs_page = api_client.cms.audit_logs.default_api.get_page()
except ApiException as e:
    print("Exception when calling cards_api->create: %s\n" % e)

Using utils

Get OAuth url:

from hubspot.utils.oauth import get_auth_url

auth_url = get_auth_url(
    scopes=('contacts',),
    client_id='client_id',
    redirect_uri='http://localhost'
)

Validate HubSpot request signature

Example of usage from Webhooks Sample App:

import os
from flask import request
from hubspot.utils.webhooks import validate_signature
from hubspot.exceptions import InvalidSignatureError

try:
    validate_signature(
        signature=request.headers["X-HubSpot-Signature"],
        signature_version=request.headers["X-HubSpot-Signature-Version"],
        http_uri=request.base_url,
        request_body=request.data.decode("utf-8"),
        client_secret=os.getenv("HUBSPOT_CLIENT_SECRET"),
    )
except InvalidSignatureError:
    print("Request signature is not valid")

Retry middleware

You can pass an instance of urllib3.util.retry.Retry class to configure http client retries. With internal error retry middleware:

from hubspot import HubSpot
from urllib3.util.retry import Retry

retry = Retry(
    total=3,
    backoff_factor=0.3,
    status_forcelist=(500, 502, 504),
)
api_client = HubSpot(retry=retry)

Or with rate limit retry middleware:

from hubspot import HubSpot
from urllib3.util.retry import Retry

retry = Retry(
    total=5,
    status_forcelist=(429,),
)
api_client = HubSpot(retry=retry)

Contributing

Install the package locally:

pip install -e .

Set up the development virtualenv:

make

Run tests:

make test

Run Black for code formatting:

make fmt

Release files for hubspot-api-client 3.7.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for hubspot-api-client 3.7.2
File Size Uploaded
hubspot-api-client-3.7.2.tar.gz 919.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for hubspot-api-client 3.7.2
File Interpreter ABI Platform
hubspot_api_client-3.7.2-py3-none-any.whl Python 3 none any Details

Total release size:2.8 MB

Release files / hubspot-api-client-3.7.2.tar.gz

Download URL hubspot-api-client-3.7.2.tar.gz
Size 919.3 kB
Tags Source
SHA-256 checksum
How to use checksums
872c20e4705b3738da4622e76660b58f8cb43a943b3ec2d9e6c646b24a30c424
BLAKE2b-256 checksum
How to use checksums
5f993dc10ff8f75c1f6cb4c8c33c246b25c8679536952ea1d01f71e19262617c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/3.7.3 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2

Release files / hubspot_api_client-3.7.2-py3-none-any.whl

Download URL hubspot_api_client-3.7.2-py3-none-any.whl
Size 1.9 MB
Tags Python 3
SHA-256 checksum
How to use checksums
654de9c3ac90d6ecdd997a442d6c9b71b8d383b18fcb01595edeb16579c52ca8
BLAKE2b-256 checksum
How to use checksums
779d1f6afb5724a652698cc231a42f79c04f9d39e6591ff8fe12c4c3f21cee19
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/3.7.3 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2

Release history Release notifications | RSS feed

11.1.0

2 release files

11.0.0

2 release files

10.0.0

2 release files

9.0.0

2 release files

8.2.1

2 release files

8.2.0

2 release files

8.1.1

2 release files

8.1.0

2 release files

8.0.0

2 release files

7.5.0

2 release files

7.4.0

2 release files

7.3.1

2 release files

7.3.0

2 release files

7.2.2

2 release files

7.2.1

2 release files

7.2.0

2 release files

7.1.0

2 release files

7.0.0

2 release files

6.1.0

2 release files

6.0.0

2 release files

5.1.1

2 release files

5.1.0

2 release files

5.0.1

2 release files

5.0.0

2 release files

4.0.6

2 release files

4.0.5

2 release files

4.0.4

2 release files

4.0.3

2 release files

4.0.2

2 release files

4.0.1

2 release files

4.0.0

2 release files

3.8.2

2 release files

3.8.1

2 release files

3.8.0

2 release files

This release

3.7.2 This release

2 release files

3.7.1

2 release files

3.7.0

2 release files

3.6.1

2 release files

3.6.0

2 release files

3.5.1

2 release files

3.5.0

2 release files

3.4.2

2 release files

3.4.1

2 release files

3.4.0

2 release files

3.3.0

2 release files

3.2.0

2 release files

3.1.0

2 release files

3.0.0

2 release files

2.1.0

2 release files

2.0.0

2 release files

1.2.0

2 release files

1.1.0

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page