# finding all the attachments

**URL:** <https://community.hubspot.com/t/finding-all-the-attachments/75841>\
**Category:** APIs & Integrations\
**Tags:** python\
**Created:** [February 23, 2023, 12:55pm UTC](https://community.hubspot.com/t/finding-all-the-attachments/75841 "2023-02-23T12:55:58Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![rajeev-fas](https://avatars.discourse-cdn.com/v4/letter/r/f4b2a3/32.png) [@rajeev-fas](https://community.hubspot.com/u/rajeev-fas)\
**Post date:** [February 23, 2023, 12:55pm UTC](https://community.hubspot.com/t/finding-all-the-attachments/75841/1 "2023-02-23T12:55:58Z")

</div>

Hello

I am looking for a way to list out and download all the attachments associated to a company id using python api.

Can someone please guide me through how to do this?

---

<div class="post-metadata">

**Author:** ![Jaycee\_Lewis](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/jaycee_lewis/32/61352_2.png) [@Jaycee\_Lewis](https://community.hubspot.com/u/Jaycee_Lewis)\
**Post date:** [February 24, 2023, 12:15am UTC](https://community.hubspot.com/t/finding-all-the-attachments/75841/2 "2023-02-24T00:15:24Z")

</div>

Hi, @rajeev-fas. I’d suggest starting out with the [Search API documentation](https://developers.hubspot.com/docs/api/crm/search).

Hey, @ChrisoKlepke @MatthiasWeber do either of you have a simple Python-based Search API example saved?

Best,

Jaycee

---

<div class="post-metadata">

**Author:** ![MatthiasWeber](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/matthiasweber/32/27498_2.png) [@MatthiasWeber](https://community.hubspot.com/u/MatthiasWeber)\
**Post date:** [February 24, 2023, 6:54am UTC](https://community.hubspot.com/t/finding-all-the-attachments/75841/3 "2023-02-24T06:54:14Z")

</div>

Hi [@Jaycee\_Lewis](https://community.hubspot.com/t5/user/viewprofilepage/user-id/127074) - no Python-knowledge. Sorry.

---

<div class="post-metadata">

**Author:** ![Jaycee\_Lewis](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/jaycee_lewis/32/61352_2.png) [@Jaycee\_Lewis](https://community.hubspot.com/u/Jaycee_Lewis)\
**Post date:** [February 24, 2023, 4:29pm UTC](https://community.hubspot.com/t/finding-all-the-attachments/75841/4 "2023-02-24T16:29:21Z")

</div>

Dang it, @MatthiasWeber! My brain knew that, but my fingers betrayed me 🤣

---

<div class="post-metadata">

**Author:** ![ChrisoKlepke](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/chrisoklepke/32/11213_2.png) [@ChrisoKlepke](https://community.hubspot.com/u/ChrisoKlepke)\
**Post date:** [February 24, 2023, 1:59pm UTC](https://community.hubspot.com/t/finding-all-the-attachments/75841/5 "2023-02-24T13:59:36Z")

</div>

Thank you for tagging [@Jaycee\_Lewis](https://community.hubspot.com/t5/user/viewprofilepage/user-id/127074).

Hey @rajeev-fas,

This code should point you in the right direction:

```lia-code-sample
import os
from pprint import pprint
from hubspot import HubSpot
from hubspot.crm.companies import ApiException as company_execption
from hubspot.crm.objects.notes import ApiException as engagement_exception
from dotenv import load_dotenv

load_dotenv()

hubspot = HubSpot(access_token=os.getenv("access_token"))
company_id = 5800956379 # ID of the company you want the attachments for
attachment_ids = []

# Attachments are tied to note engagments. Get all notes for the company

try:
 get_notes = hubspot.crm.companies.associations_api.get_all(
 company_id=company_id, to_object_type="note", limit=500
 )
 # pprint(get_notes.results) uncomment for debug

except company_execption as e:
 print("Exception when calling associations_api->get_all: %s\n" % e)

# Iterate through all the note ids from the response and get their properties

for note in get_notes.results:
 try:
 get_note = hubspot.crm.objects.notes.basic_api.get_by_id(
 note_id=note.to_object_id, properties=["hs_attachment_ids"], archived=False
 )
 # pprint(get_note.properties) uncomment for debug

 attachment_id = get_note.properties["hs_attachment_ids"]

 # Check if the note has an attachment
 if attachment_id is not None:
 id_list = attachment_id.split(";")
 # pprint(id_list) uncomment for debug

 # Extend the list of attachments
 attachment_ids.extend(id_list)

 except engagement_exception as e:
 print("Exception when calling basic_api->get_by_id: %s\n" % e)

# Print out the complete list of attachment ids. To download use /files/v3/files/{fileId} endpoint
pprint(attachment_ids)

```

If you found this post helpful, consider helping others in the community to find answers faster by marking this as a solution. I’d really appreciate it.

Cheers,

Chriso

---

<div class="post-metadata">

**Author:** ![rajeev-fas](https://avatars.discourse-cdn.com/v4/letter/r/f4b2a3/32.png) [@rajeev-fas](https://community.hubspot.com/u/rajeev-fas)\
**Post date:** [February 25, 2023, 3:28am UTC](https://community.hubspot.com/t/finding-all-the-attachments/75841/6 "2023-02-25T03:28:07Z")

</div>

Hello Christoph,  
I was able to run the above code but it does not return anything. I am using company\_id which has 10+ attachements when I look into Hubspot portal.  
The notes section for this company is empty in the portal. Since in the above code we are querying “notes”, my question is are we using the right

“to\_object\_type”  
Thank you all for all the ongoing help with this.

---

<div class="post-metadata">

**Author:** ![ChrisoKlepke](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/chrisoklepke/32/11213_2.png) [@ChrisoKlepke](https://community.hubspot.com/u/ChrisoKlepke)\
**Post date:** [February 27, 2023, 7:45am UTC](https://community.hubspot.com/t/finding-all-the-attachments/75841/7 "2023-02-27T07:45:28Z")

</div>

Hey @rajeev-fas ,

in general, if you upload any attachments, a note gets created where the respective file can also be found. That is btw also the case when you create attachments through a form upload.

To get to the file/s from the API, you have to go through the associated activities to find them. There is no _to\_object\_type_ called _attachments_ or something similar.

This might sound stupid, but maybe you’re currently filtering notes in the activities when looking at the record.

Now, it is hard for us to diagnose these things at a distance. So without any more information, it will be very hard for us to help you any further. For example: How are these attachments created in the first place?

Cheers,

Chriso

---

<div class="post-metadata">

**Author:** ![rajeev-fas](https://avatars.discourse-cdn.com/v4/letter/r/f4b2a3/32.png) [@rajeev-fas](https://community.hubspot.com/u/rajeev-fas)\
**Post date:** [February 27, 2023, 1:29pm UTC](https://community.hubspot.com/t/finding-all-the-attachments/75841/8 "2023-02-27T13:29:07Z")

</div>

Thank you Chriso, you put me in the right track. Here is the complete code which will find all the email attachments by company id.

from hubspot import HubSpot

from hubspot.crm.companies import ApiException as company\_execption

from hubspot.crm.objects.notes import ApiException as engagement\_exception

from dotenv import load\_dotenv

import os

from pprint import pprint

import requests

import mimetypes

import gzip

import json

from urllib.parse import unquote

import csv

from datetime import datetime

import random

import string

access\_token = “YourAccessToken”

company\_id = “YourCompanyID”

hubspot = HubSpot(access\_token=access\_token)

attachment\_ids = []

# email attachments are tied to email engagments. Get all emails for the company

try:

get\_emails = hubspot.crm.companies.associations\_api.get\_all(

company\_id=company\_id, to\_object\_type=“EMAIL”, limit=500)

#pprint(get\_emails.results) #uncomment for debug

except company\_execption as e:

print(“Exception when calling associations\_api-\>get\_all: %s\n” % e)

for email in get\_emails.results:

try:

get\_email = hubspot.crm.objects.emails.basic\_api.get\_by\_id(

email\_id=email.to\_object\_id, properties=[“hs\_attachment\_ids”], archived=False)

# pprint(get\_email.properties) uncomment for debug

attachment\_id = get\_email.properties[“hs\_attachment\_ids”]

# Check if the email has an attachment

if attachment\_id is not None:

id\_list = attachment\_id.split(“;”)

# Extend the list of attachments

attachment\_ids.extend(id\_list)

except engagement\_exception as e:

print(“Exception when calling basic\_api-\>get\_by\_id: %s\n” % e)

attachment\_ids = [x for x in attachment\_ids if x != ‘’]

print(len(attachment\_ids))

---

<div class="post-metadata">

**Author:** ![ChrisoKlepke](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/chrisoklepke/32/11213_2.png) [@ChrisoKlepke](https://community.hubspot.com/u/ChrisoKlepke)\
**Post date:** [February 27, 2023, 1:53pm UTC](https://community.hubspot.com/t/finding-all-the-attachments/75841/9 "2023-02-27T13:53:48Z")

</div>

Glad I was able to help 😉

---

<div class="post-metadata">

**Author:** ![Franci](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/franci/32/225468_2.png) [@Franci](https://community.hubspot.com/u/Franci)\
**Post date:** [September 15, 2023, 12:38pm UTC](https://community.hubspot.com/t/finding-all-the-attachments/75841/10 "2023-09-15T12:38:50Z")

</div>

Hey there!

We’ve just released the Attachment From Form app on the HubSpot Marketplace!

If you’re tired of playing hide-and-seek with files in your HubSpot database, you’re in the right place! Attachment From Form simplifies the process by linking files submitted in a form directly to the corresponding contact record. Say goodbye to the endless file hunt and say hello to effortless access!

Here are the standout features:

1. Effortless Form-Attachment Connection: Easily link your forms to contact attachments.

2. Efficient File Retrieval: When a contact submits a file type property in your form, it appears in the attachment card, saving you valuable time.

3. Organized Activity Section: Attachment From Form creates custom activities, making it a breeze to find attached files on the contact page while maintaining a comprehensive property attribution history.

Get organized and supercharge your efficiency with the Attachment From Form app now! And the best part? It won’t cost you a dime.

**You can install it right away → [LINK](https://ecosystem.hubspot.com/marketplace/apps/marketing/data-management/attachment-from-form-1752956)**
