Skip to main content

HubSpot API client

Project description

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

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

OAuth API

Obtain OAuth2 access token:

from hubspot.auth.oauth import ApiException

try:
    tokens = hubspot.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 = hubspot.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 = hubspot.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 = hubspot.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 = hubspot.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 = hubspot.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),
)
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,),
)
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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hubspot-api-client-3.5.1.tar.gz (728.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

hubspot_api_client-3.5.1-py3-none-any.whl (1.5 MB view details)

Uploaded Python 3

File details

Details for the file hubspot-api-client-3.5.1.tar.gz.

File metadata

  • Download URL: hubspot-api-client-3.5.1.tar.gz
  • Upload date:
  • Size: 728.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.9.1

File hashes

Hashes for hubspot-api-client-3.5.1.tar.gz
Algorithm Hash digest
SHA256 f48cff5a8f10fac9650c8859a1c3c5285ab8b3d27399a1671d51c2c2d518a3a5
MD5 a946c902482dfcdb72e53dffc0d61470
BLAKE2b-256 98b89d716007e1ea92aa3624d683f554ac5331baa18d146cec408aa60e3b46c9

See more details on using hashes here.

File details

Details for the file hubspot_api_client-3.5.1-py3-none-any.whl.

File metadata

  • Download URL: hubspot_api_client-3.5.1-py3-none-any.whl
  • Upload date:
  • Size: 1.5 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.9.1

File hashes

Hashes for hubspot_api_client-3.5.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c0cce88012d2c2d3eb65ba969f74ac8a05a53db88cb026cdc1a116c6fa8fdf06
MD5 f630202afac19a3957e08faff7352e05
BLAKE2b-256 e6c0f42e7937cfb80700619514c5d799c73fe356b52499f16f9bd030ff8c4c17

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page