Python HubSpot API v3 SDK(Client) files and sample apps
Sample Applications can be found in sample-apps folder
See the API docs.
If you just want to use the package, run:
pip install --upgrade hubspot-api-clientMake sure you have Python 3.5+ and pip installed.
fromhubspotimportHubSpotapi_client=HubSpot()
# or with api_keyapi_client=HubSpot(api_key='your_api_key')
# or with access_tokenapi_client=HubSpot()
api_client.access_token='your_access_token'fromhubspot.auth.oauthimportApiExceptiontry:
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'
)
exceptApiExceptionase:
print("Exception when calling create_token method: %s\n"%e)fromhubspot.crm.contactsimportSimplePublicObjectInputfromhubspot.crm.contacts.exceptionsimportApiExceptiontry:
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
)
exceptApiExceptionase:
print("Exception when creating contact: %s\n"%e)fromhubspot.crm.contactsimportApiExceptiontry:
contact_fetched=api_client.crm.contacts.basic_api.get_by_id('contact_id')
exceptApiExceptionase:
print("Exception when requesting contact by id: %s\n"%e)fromhubspot.crm.objectsimportApiExceptiontry:
my_custom_objects_page=api_client.crm.objects.basic_api.get_page(object_type="my_custom_object_type")
exceptApiExceptionase:
print("Exception when requesting custom objects: %s\n"%e)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.
fromhubspot.cms.audit_logsimportApiExceptiontry:
audit_logs_page=api_client.cms.audit_logs.default_api.get_page()
exceptApiExceptionase:
print("Exception when calling cards_api->create: %s\n"%e)fromhubspot.utils.oauthimportget_auth_urlauth_url=get_auth_url(
scopes=('contacts',),
client_id='client_id',
redirect_uri='http://localhost'
)Example of usage from Webhooks Sample App:
importosfromflaskimportrequestfromhubspot.utils.webhooksimportvalidate_signaturefromhubspot.exceptionsimportInvalidSignatureErrortry:
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"),
)
exceptInvalidSignatureError:
print("Request signature is not valid")You can pass an instance of urllib3.util.retry.Retry class to configure http client retries. With internal error retry middleware:
fromhubspotimportHubSpotfromurllib3.util.retryimportRetryretry=Retry(
total=3,
backoff_factor=0.3,
status_forcelist=(500, 502, 504),
)
api_client=HubSpot(retry=retry)Or with rate limit retry middleware:
fromhubspotimportHubSpotfromurllib3.util.retryimportRetryretry=Retry(
total=5,
status_forcelist=(429,),
)
api_client=HubSpot(retry=retry)Install the package locally:
pip install -e .
Set up the development virtualenv:
make
Run tests:
make test
Run Black for code formatting:
make fmt