Package: hubspot-api-client
hubspot-api-pythonPythonHubSpot APIv3 SDK(Client) files and sample appsSample Applications can be found inSample appsDocumentationSee theAPI docs.InstallationIf you just want to use the package, run:pipinstall--upgradehubspot-api-clientRequirementsMake sure you havePython 3.5+andpipinstalled.QuickstartConfiguring HubSpot clientfromhubspotimportHubSpotapi_client=HubSpot(access_token='your_access_token')# or set your access token laterapi_client=HubSpot()api_client.access_token='your_access_token'You'll need to create aprivate appto get your access token or you can obtain OAuth2 access token.Hapikey support:Please, note that hapikey is no longer supported after v5.1.0. You can get more info about hapikey sunsethere. Also, plese, visit amigration guideif you need help with a migration process.OAuth APIObtain OAuth2 access token:fromhubspot.auth.oauthimportApiExceptiontry:tokens=api_client.auth.oauth.tokens_api.create(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)CRM APICreate contact:fromhubspot.crm.contactsimportSimplePublicObjectInputForCreatefromhubspot.crm.contacts.exceptionsimportApiExceptiontry:simple_public_object_input_for_create=SimplePublicObjectInputForCreate(properties={"email":"email@example.com"})api_response=api_client.crm.contacts.basic_api.create(simple_public_object_input_for_create=simple_public_object_input_for_create)exceptApiExceptionase:print("Exception when creating contact:%s\n"%e)Get contact by id: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)Get custom objects page: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:get_allmethod is available for all objects (Companies, Contacts, Deals and etc).all_contacts=api_client.crm.contacts.get_all()Please note that pagination is used under the hood to get all results.Search:do_searchmethod is available for all objects (Companies, Contacts, Deals and etc).Example Search by date:importhubspotfromdateutilimportparserfrompprintimportpprintfromhubspot.crm.contactsimportPublicObjectSearchRequest,ApiExceptionapi_client=hubspot.Client.create(access_token="YOUR_ACCESS_TOKEN")# timestamp in millisecondsdate=str(int(parser.isoparse("XXXX-XX-XXTXX:XX:XX.XXXZ").timestamp()*1000))public_object_search_request=PublicObjectSearchRequest(filter_groups=[{"filters":[{"value":date,"propertyName":"lastmodifieddate","operator":"EQ"}]}],limit=10)try:api_response=api_client.crm.contacts.search_api.do_search(public_object_search_request=public_object_search_request)pprint(api_response)exceptApiExceptionase:print("Exception when calling search_api->do_search:%s\n"%e)CMS APIGet audit logs: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)Using utilsGet OAuth url:fromhubspot.utils.oauthimportget_auth_urlauth_url=get_auth_url(scopes=('contacts',),client_id='client_id',redirect_uri='http://localhost')Validate HubSpot request signatureExampleof usage fromWebhooks Sample App:importosfromdatetimeimportdatetimefromflaskimportrequestfromhubspot.utils.signatureimportSignatureSignature.is_valid(signature=request.headers["X-HubSpot-Signature"],client_secret=os.getenv("HUBSPOT_CLIENT_SECRET"),request_body=request.data.decode("utf-8"),http_uri=request.base_url,signature_version=request.headers["X-HubSpot-Signature-Version"],timestamp=datetime.now().timestamp())Retry middlewareYou can pass an instance ofurllib3.util.retry.Retryclass 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)Convert response object to dictto_dictmethod is available for most response objectscontacts=api_client.crm.contacts.basic_api.get_page()forcontactincontacts:print(contact.to_dict())Sample AppsPlease, take a look at ourSample appsContributingInstall the package locally:pip install -e .Set up the development virtualenv:makeRun tests:make testRunBlackfor code formatting:make fmt