APIs & Integrations

rajeev-fas
Member

finding all the attachments

SOLVE

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? 

 

 

0 Upvotes
1 Accepted solution
rajeev-fas
Solution
Member

finding all the attachments

SOLVE

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))

View solution in original post

9 Replies 9
Franci
Top Contributor | Diamond Partner
Top Contributor | Diamond Partner

finding all the attachments

SOLVE

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

Francesco Collacciani

HubSpot Specialist @ Exelab

#1 HubSpot Diamond Partner in Italy

emailAddress
francesco.collacciani@exelab.com
website
https://www.exelab.com/en/
Get in touch with us!
0 Upvotes
Jaycee_Lewis
Community Manager
Community Manager

finding all the attachments

SOLVE

Hi, @rajeev-fas. I'd suggest starting out with the Search API documentation.

 

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

 

Best,

Jaycee 

 

 

linkedin

Jaycee Lewis

Developer Community Manager

Community | HubSpot

ChrisoKlepke
Key Advisor | Elite Partner
Key Advisor | Elite Partner

finding all the attachments

SOLVE

Thank you for tagging @Jaycee_Lewis.

 

Hey @rajeev-fas

 

This code should point you in the right direction:

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

rajeev-fas
Member

finding all the attachments

SOLVE

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. 
ChrisoKlepke
Key Advisor | Elite Partner
Key Advisor | Elite Partner

finding all the attachments

SOLVE

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

0 Upvotes
rajeev-fas
Solution
Member

finding all the attachments

SOLVE

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))
ChrisoKlepke
Key Advisor | Elite Partner
Key Advisor | Elite Partner

finding all the attachments

SOLVE

Glad I was able to help 😉 

0 Upvotes
MatthiasWeber
Guide | Partner
Guide | Partner

finding all the attachments

SOLVE

Hi @Jaycee_Lewis - no Python-knowledge. Sorry.

0 Upvotes
Jaycee_Lewis
Community Manager
Community Manager

finding all the attachments

SOLVE

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

linkedin

Jaycee Lewis

Developer Community Manager

Community | HubSpot