# Python Add Note AttributeError

**URL:** <https://community.hubspot.com/t/python-add-note-attributeerror/120688>\
**Category:** APIs & Integrations\
**Tags:** python, developer-tools, free, contacts\
**Created:** [November 17, 2024, 11:01pm UTC](https://community.hubspot.com/t/python-add-note-attributeerror/120688 "2024-11-17T23:01:31Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![TWinter2954](https://avatars.discourse-cdn.com/v4/letter/t/94ad74/32.png) [@TWinter2954](https://community.hubspot.com/u/TWinter2954)\
**Post date:** [November 17, 2024, 11:01pm UTC](https://community.hubspot.com/t/python-add-note-attributeerror/120688/1 "2024-11-17T23:01:31Z")

</div>

I have a Python script to add a note to all of my contacts that have “Active” status and the role of “Primary Owner”. The script retrieves the contacts fine; however, it fails when it tries to create a note for each contact. I’ve used [Notes - v3 | HubSpotAPI](https://developers.hubspot.com/beta-docs/reference/api/crm/engagements/notes) and [Engagements | Notes | HubSpot](https://developers.hubspot.com/beta-docs/guides/api/crm/engagements/notes) for references.

Code:

```lia-code-sample
from hubspot import HubSpot
from hubspot.crm.contacts import SimplePublicObjectInput, ApiException
from hubspot.crm.objects import ApiException
from hubspot.crm.objects.notes import SimplePublicObjectInputForCreate, ApiException
from datetime import datetime as d
import datetime as dt
import logging as log

log.basicConfig(level=log.INFO, format='%(asctime)s | %(levelname)s | %(message)s', datefmt='%m-%d-%Y %H:%M:%S')

log.info('Initializing...')

API_KEY = 'REDACTED'
BASE_URL = 'https://api.hubapi.com'
STATUS_PROP = 'hs_content_membership_status'
STATUS_VALUE = 'active'
ROLE_PROP = 'role'
ROLE_VALUE = 'Primary Owner'
ERROR_STATUS = False

client = HubSpot(access_token=API_KEY)
log.info('Connected to HubSpot')

# Create a note (engagement) to add to the contacts
def create_note_for_contact(contact_id, note_content):

 properties = {
 "hs_note_body": f"{str(note_content)}",
 "hs_timestamp": f"{str(d.now(dt.timezone.utc).timestamp() * 1000)}",
 "hubspot_owner_id": "REDACTED",
 "hs_attachment_ids": "REDACTED"
 }
 simple_public_object_input_for_create = SimplePublicObjectInputForCreate(
 associations=[{"types":[{"associationCategory":"HUBSPOT_DEFINED","associationTypeId":202}],
 "to":{contact_id}}],
 # object_write_trace_id='string',
 properties=properties
 )
 try:
 response = client.crm.objects.notes.basic_api.create(simple_public_object_input_for_create=simple_public_object_input_for_create)
 log.info(f"Note added to contact {contact_id}")
 except ApiException as e:
 log.error(f"Error adding note to contact {contact_id}: {e}")
 global ERROR_STATUS
 ERROR_STATUS = True

# Function to get contacts with 'hs_content_membership_status' = 'active'
def get_active_contacts():
 filter_criteria = {
 "filterGroups": [{
 "filters": [{
 "propertyName": f"{STATUS_PROP}",
 "operator": "EQ",
 "value": f"{STATUS_VALUE}"
 },
 {
 "propertyName": f"{ROLE_PROP}",
 "operator": "EQ",
 "value": f"{ROLE_VALUE}"
 }]
 }],
 "limit": 100, 
 "properties": ["firstname", "lastname", "hs_content_membership_status"]
 }

 try:
 # Fetch contacts with filter
 response = client.crm.contacts.search_api.do_search(filter_criteria)
 contacts = response.results
 log.info(f"Found {len(contacts)} contacts with active membership status.")
 return contacts
 except ApiException as e:
 log.error(f"Error fetching contacts: {e}")
 global ERROR_STATUS
 ERROR_STATUS = True
 return []

# Main function to add a note to all active contacts
def add_note_to_all_active_contacts(note_content):
 contacts = get_active_contacts()
 try:
 log.info(f"Adding note to {len(contacts)} contacts...")
 for contact in contacts:
 contact_id = contact.id
 create_note_for_contact(contact_id, note_content)
 except ApiException as e:
 log.error(f"Error adding note to contacts: {e}")
 global ERROR_STATUS
 ERROR_STATUS = True

def confirm_input():
 valid_responses = ['y', 'n', 'yes', 'no']
 while True:
 response = input('Are you sure you want to continue? (y/n): ')
 if response in valid_responses:
 if response.lower() in ['y', 'yes']:
 return True
 else:
 return False
 else:
 log.error('Invalid response. Please enter "y" or "n".')

if __name__ == " __main__":
 log.info('Starting...')
 # Example note content

 note = input('Enter the note to be added to all active primary contacts: \n')
 if confirm_input():
 log.info(f'User confirmed adding the following note to all active primary contacts:\n {note}')

 # Run the function to add a note to all active contacts
 add_note_to_all_active_contacts(note)

 if ERROR_STATUS:
 log.error('Error(s) occurred. Please check the logs.')
 else:
 log.info('Done!')

 else:
 log.warning('User cancelled the operation.')

```

The error I have been getting is:

 ![TWinter2954_0-1731884067390.png](https://d2qpr2jzcazej7.cloudfront.net/original/3X/0/f/0fd12391ee7012bd23a30ea126f62e32e66476bd.png)

---

<div class="post-metadata">

**Author:** ![Bhupendra\_Kumar](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/bhupendra_kumar/32/67536_2.png) [@Bhupendra\_Kumar](https://community.hubspot.com/u/Bhupendra_Kumar)\
**Post date:** [November 18, 2024, 3:46am UTC](https://community.hubspot.com/t/python-add-note-attributeerror/120688/2 "2024-11-18T03:46:06Z")

</div>

Hi ,I just reviewed your code and it looks like you are not passing the correct

`hubspot_owner_id`

in the properties json . You need to send either correct one or you can skip that .  
Also you can skip

`hs_attachment_ids `

and then try again . If you want to send the owner id you can get owners list using this endpoint

`https://api.hubapi.com/crm/v3/owners`

✔ **Mark this answer as solution if it helped you**

---

<div class="post-metadata">

**Author:** ![TWinter2954](https://avatars.discourse-cdn.com/v4/letter/t/94ad74/32.png) [@TWinter2954](https://community.hubspot.com/u/TWinter2954)\
**Post date:** [November 18, 2024, 4:30am UTC](https://community.hubspot.com/t/python-add-note-attributeerror/120688/3 "2024-11-18T04:30:13Z")

</div>

Hi [@Bhupendra\_Kumar](https://community.hubspot.com/t5/user/viewprofilepage/user-id/857649), thanks for taking a look. I verified the owner ID is correct using the endpoint you provided. I also tried omitting the hubspot\_owner\_id and hs\_attachment\_ids and received the same error.

---

<div class="post-metadata">

**Author:** ![Bhupendra\_Kumar](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/bhupendra_kumar/32/67536_2.png) [@Bhupendra\_Kumar](https://community.hubspot.com/u/Bhupendra_Kumar)\
**Post date:** [November 18, 2024, 4:44am UTC](https://community.hubspot.com/t/python-add-note-attributeerror/120688/4 "2024-11-18T04:44:59Z")

</div>

Hi @TWinter2954 , If possible Can you provide me the json that you are posting over the API so that we can figure out if error was with data or the code level.  
Thanks

---

<div class="post-metadata">

**Author:** ![Bhupendra\_Kumar](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/bhupendra_kumar/32/67536_2.png) [@Bhupendra\_Kumar](https://community.hubspot.com/u/Bhupendra_Kumar)\
**Post date:** [November 18, 2024, 4:49am UTC](https://community.hubspot.com/t/python-add-note-attributeerror/120688/5 "2024-11-18T04:49:43Z")

</div>

Also @TWinter2954 i want to add one more thing the associations JSON should be like this

```lia-code-sample
[{"types":[{"associationCategory":"HUBSPOT_DEFINED","associationTypeId":202}],
 "to":{"id":"contact_id"}}]

```

what you are sending is :

```lia-code-sample
[{"types":[{"associationCategory":"HUBSPOT_DEFINED","associationTypeId":202}],
 "to":{contact_id}}]

```

the “to” object would have id as key .

---

<div class="post-metadata">

**Author:** ![Bhupendra\_Kumar](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/bhupendra_kumar/32/67536_2.png) [@Bhupendra\_Kumar](https://community.hubspot.com/u/Bhupendra_Kumar)\
**Post date:** [November 18, 2024, 5:38am UTC](https://community.hubspot.com/t/python-add-note-attributeerror/120688/6 "2024-11-18T05:38:23Z")

</div>

Hi @TWinter2954

If that helped you can you please mark it as a solution to help the other community members.

Thanks

---

<div class="post-metadata">

**Author:** ![TWinter2954](https://avatars.discourse-cdn.com/v4/letter/t/94ad74/32.png) [@TWinter2954](https://community.hubspot.com/u/TWinter2954)\
**Post date:** [November 18, 2024, 6:42am UTC](https://community.hubspot.com/t/python-add-note-attributeerror/120688/7 "2024-11-18T06:42:27Z")

</div>

Hi [@Bhupendra\_Kumar](https://community.hubspot.com/t5/user/viewprofilepage/user-id/857649), I will try this tomorrow afternoon and let you know. Thanks!
