<jupyter_start><jupyter_text>HubSpot - Get Task **Tags:** hubspot sales crm engagements task **Author:** [<NAME>](https://www.linkedin.com/in/calok64/)  Input  Import libraries<jupyter_code>from datetime import datetime, timedelta
import requests, math
import json<jupyter_output><empty_output><jupyter_text>Setup your HubSpot👉 Access your [HubSpot API key](https://knowledge.hubspot.com/integrations/how-do-i-get-my-hubspot-api-key)<jupyter_code>HS_API_TOKEN = "YOUR_HUBSPOT_API_KEY"<jupyter_output><empty_output><jupyter_text>Setup your task info<jupyter_code>contact_id = 1551
owner_id = 111111086

# Time delay to get tasks created since N days, where N is no of days. For ex. Get tasks created since 1 day
time_delay = 10

#Number of tasks to be retrieved
no_of_tasks = 10<jupyter_output><empty_output><jupyter_text>Model  Function to get recent tasks<jupyter_code>def get_task(contact_id,owner_id,time_delay,no_of_tasks):
    """
    Engagement type = TASK  
    """
    
    # Calc timestamp
    Previous_Date = datetime.now() - timedelta(days=time_delay)
    Previous_tstamp = Previous_Date.timestamp() * 1000
    Previous_tstamp = math.trunc(Previous_tstamp)
     
    
    url = "https://api.hubapi.com/engagements/v1/engagements/recent/modified"
    params = {"hapikey": HS_API_TOKEN,"since":Previous_tstamp,"count":no_of_tasks}
    headers = {'Content-Type': "application/json"}
    # Post requests
    res = requests.get(url,headers=headers,params=params)
    
    if res.status_code == 200:
        
        res_json = res.json()

        # Check requests
        try:
            res.raise_for_status()
        except requests.HTTPError as e:
            raise (e)
        res_json = res.json()

        return res_json
    else:
        print("Task not found")<jupyter_output><empty_output><jupyter_text>Output  Get Recent task<jupyter_code>results = get_task(contact_id,owner_id,time_delay,no_of_tasks)
for key in results["results"]:
    print("---------------")
    print(key['engagement']['id'])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11888454011741682, 'fraction_numerical': 0.0136986301369863, 'mean_word_length': 4.074441687344913, 'pattern_counts': {'":': 3, '<': 22, '<?xml version=': 0, '>': 22, 'https://': 3, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '110713', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 725, 'n_tokens_neox': 690, 'n_words': 134, 'path': 'HubSpot/HubSpot_Get_Task.ipynb', 'repo_name': 'srini047/awesome-notebooks', 'size': '2049'}