<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Attach file to deal record using API in APIs &amp; Integrations</title>
    <link>https://community.hubspot.com/t5/APIs-Integrations/Attach-file-to-deal-record-using-API/m-p/721792#M58985</link>
    <description>&lt;P&gt;Hi,&amp;nbsp;&lt;a href="https://community.hubspot.com/t5/user/viewprofilepage/user-id/470083"&gt;@SSharma043&lt;/a&gt;&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":waving_hand:"&gt;👋&lt;/span&gt; Were you able to get this one sorted out? If not, I'll share a quick example using Postman.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Summary:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Upload the file&lt;/LI&gt;
&lt;LI&gt;Create the Note&lt;/LI&gt;
&lt;LI&gt;Associate the File to the Note&lt;/LI&gt;
&lt;LI&gt;Associate the Note to the Object&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;Steps&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Upload your file via API and note the value for “id”&lt;BR /&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="files-1.png" style="width: 400px;"&gt;&lt;img src="https://community.hubspot.com/t5/image/serverpage/image-id/79709iF68F37BCC4A80738/image-size/medium?v=v2&amp;amp;px=400" role="button" title="files-1.png" alt="files-1.png" /&gt;&lt;/span&gt;&lt;/LI&gt;
&lt;LI&gt; Files added can be associated to an Object as a Note&lt;/LI&gt;
&lt;LI&gt;Use the 'id” vale as the value for “hs_attachment_ids” when creating a note&lt;BR /&gt;&lt;LI-CODE lang="markup"&gt;POST https://api.hubapi.com/crm/v3/objects/notes​&lt;/LI-CODE&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="files-2.png" style="width: 400px;"&gt;&lt;img src="https://community.hubspot.com/t5/image/serverpage/image-id/79710i914961775B0BAB63/image-size/medium?v=v2&amp;amp;px=400" role="button" title="files-2.png" alt="files-2.png" /&gt;&lt;/span&gt;&lt;/LI&gt;
&lt;LI&gt;&amp;nbsp; Then you can make a call to associate the Note to a Deal&lt;BR /&gt;&lt;LI-CODE lang="markup"&gt;PUT https://api.hubspot.com/crm/v3/objects/notes/{note_ID}/associations/deal/{deal_ID}/214​&lt;/LI-CODE&gt;&lt;BR /&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="files-3.png" style="width: 400px;"&gt;&lt;img src="https://community.hubspot.com/t5/image/serverpage/image-id/79711i31B097D37F00E880/image-size/medium?v=v2&amp;amp;px=400" role="button" title="files-3.png" alt="files-3.png" /&gt;&lt;/span&gt;&lt;BR /&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="files-4.png" style="width: 400px;"&gt;&lt;img src="https://community.hubspot.com/t5/image/serverpage/image-id/79712i583E298DC7E31C9F/image-size/medium?v=v2&amp;amp;px=400" role="button" title="files-4.png" alt="files-4.png" /&gt;&lt;/span&gt;&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;I hope this helps you or the next person who is looking to upload and associate files to objects in this manner.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best,&lt;/P&gt;
&lt;P&gt;Jaycee&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt; &lt;/P&gt;</description>
    <pubDate>Tue, 22 Nov 2022 19:46:10 GMT</pubDate>
    <dc:creator>Jaycee_Lewis</dc:creator>
    <dc:date>2022-11-22T19:46:10Z</dc:date>
    <item>
      <title>Attach file to deal record using API</title>
      <link>https://community.hubspot.com/t5/APIs-Integrations/Attach-file-to-deal-record-using-API/m-p/719945#M58797</link>
      <description>&lt;P&gt;I have uploaded File in Hubspot using API.&lt;BR /&gt;Now I want attach that to deal record using API&amp;nbsp;&lt;BR /&gt;How can I do it&lt;BR /&gt;Please Help!!&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've attached screenshots for refrence&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="This file I want to attach to deal record" style="width: 999px;"&gt;&lt;img src="https://community.hubspot.com/t5/image/serverpage/image-id/79408i4117D8C214761F70/image-size/large?v=v2&amp;amp;px=999" role="button" title="image_2.png" alt="This file I want to attach to deal record" /&gt;&lt;span class="lia-inline-image-caption" onclick="event.preventDefault();"&gt;This file I want to attach to deal record&lt;/span&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Here I want to attach file using API" style="width: 999px;"&gt;&lt;img src="https://community.hubspot.com/t5/image/serverpage/image-id/79409i5D18B6773223C72C/image-size/large?v=v2&amp;amp;px=999" role="button" title="image_5.png" alt="Here I want to attach file using API" /&gt;&lt;span class="lia-inline-image-caption" onclick="event.preventDefault();"&gt;Here I want to attach file using API&lt;/span&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 17 Nov 2022 19:42:24 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/APIs-Integrations/Attach-file-to-deal-record-using-API/m-p/719945#M58797</guid>
      <dc:creator>SSharma043</dc:creator>
      <dc:date>2022-11-17T19:42:24Z</dc:date>
    </item>
    <item>
      <title>Re: Attach file to deal record using API</title>
      <link>https://community.hubspot.com/t5/APIs-Integrations/Attach-file-to-deal-record-using-API/m-p/721792#M58985</link>
      <description>&lt;P&gt;Hi,&amp;nbsp;&lt;a href="https://community.hubspot.com/t5/user/viewprofilepage/user-id/470083"&gt;@SSharma043&lt;/a&gt;&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":waving_hand:"&gt;👋&lt;/span&gt; Were you able to get this one sorted out? If not, I'll share a quick example using Postman.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Summary:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Upload the file&lt;/LI&gt;
&lt;LI&gt;Create the Note&lt;/LI&gt;
&lt;LI&gt;Associate the File to the Note&lt;/LI&gt;
&lt;LI&gt;Associate the Note to the Object&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;Steps&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Upload your file via API and note the value for “id”&lt;BR /&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="files-1.png" style="width: 400px;"&gt;&lt;img src="https://community.hubspot.com/t5/image/serverpage/image-id/79709iF68F37BCC4A80738/image-size/medium?v=v2&amp;amp;px=400" role="button" title="files-1.png" alt="files-1.png" /&gt;&lt;/span&gt;&lt;/LI&gt;
&lt;LI&gt; Files added can be associated to an Object as a Note&lt;/LI&gt;
&lt;LI&gt;Use the 'id” vale as the value for “hs_attachment_ids” when creating a note&lt;BR /&gt;&lt;LI-CODE lang="markup"&gt;POST https://api.hubapi.com/crm/v3/objects/notes​&lt;/LI-CODE&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="files-2.png" style="width: 400px;"&gt;&lt;img src="https://community.hubspot.com/t5/image/serverpage/image-id/79710i914961775B0BAB63/image-size/medium?v=v2&amp;amp;px=400" role="button" title="files-2.png" alt="files-2.png" /&gt;&lt;/span&gt;&lt;/LI&gt;
&lt;LI&gt;&amp;nbsp; Then you can make a call to associate the Note to a Deal&lt;BR /&gt;&lt;LI-CODE lang="markup"&gt;PUT https://api.hubspot.com/crm/v3/objects/notes/{note_ID}/associations/deal/{deal_ID}/214​&lt;/LI-CODE&gt;&lt;BR /&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="files-3.png" style="width: 400px;"&gt;&lt;img src="https://community.hubspot.com/t5/image/serverpage/image-id/79711i31B097D37F00E880/image-size/medium?v=v2&amp;amp;px=400" role="button" title="files-3.png" alt="files-3.png" /&gt;&lt;/span&gt;&lt;BR /&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="files-4.png" style="width: 400px;"&gt;&lt;img src="https://community.hubspot.com/t5/image/serverpage/image-id/79712i583E298DC7E31C9F/image-size/medium?v=v2&amp;amp;px=400" role="button" title="files-4.png" alt="files-4.png" /&gt;&lt;/span&gt;&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;I hope this helps you or the next person who is looking to upload and associate files to objects in this manner.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best,&lt;/P&gt;
&lt;P&gt;Jaycee&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt; &lt;/P&gt;</description>
      <pubDate>Tue, 22 Nov 2022 19:46:10 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/APIs-Integrations/Attach-file-to-deal-record-using-API/m-p/721792#M58985</guid>
      <dc:creator>Jaycee_Lewis</dc:creator>
      <dc:date>2022-11-22T19:46:10Z</dc:date>
    </item>
    <item>
      <title>Re: Attach file to deal record using API</title>
      <link>https://community.hubspot.com/t5/APIs-Integrations/Attach-file-to-deal-record-using-API/m-p/722546#M59028</link>
      <description>&lt;P&gt;Thanks for your solution! it works for me&lt;/P&gt;</description>
      <pubDate>Thu, 24 Nov 2022 07:09:53 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/APIs-Integrations/Attach-file-to-deal-record-using-API/m-p/722546#M59028</guid>
      <dc:creator>DzungNguyen</dc:creator>
      <dc:date>2022-11-24T07:09:53Z</dc:date>
    </item>
    <item>
      <title>Re: Attach file to deal record using API</title>
      <link>https://community.hubspot.com/t5/APIs-Integrations/Attach-file-to-deal-record-using-API/m-p/792998#M63951</link>
      <description>&lt;P&gt;&lt;a href="https://community.hubspot.com/t5/user/viewprofilepage/user-id/127074"&gt;@Jaycee_Lewis&lt;/a&gt;&amp;nbsp;can you breife the request body and headers what you are paasing while uploading the file&lt;/P&gt;</description>
      <pubDate>Fri, 12 May 2023 07:16:54 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/APIs-Integrations/Attach-file-to-deal-record-using-API/m-p/792998#M63951</guid>
      <dc:creator>03482</dc:creator>
      <dc:date>2023-05-12T07:16:54Z</dc:date>
    </item>
    <item>
      <title>Re: Attach file to deal record using API</title>
      <link>https://community.hubspot.com/t5/APIs-Integrations/Attach-file-to-deal-record-using-API/m-p/809392#M65033</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.hubspot.com/t5/user/viewprofilepage/user-id/127074"&gt;@Jaycee_Lewis&lt;/a&gt;&amp;nbsp;, can you share an example for how this would work on Python? novice coder here and I'm having trouble getting the file upload to work on Python&lt;/P&gt;</description>
      <pubDate>Tue, 20 Jun 2023 08:05:38 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/APIs-Integrations/Attach-file-to-deal-record-using-API/m-p/809392#M65033</guid>
      <dc:creator>IHernandez4</dc:creator>
      <dc:date>2023-06-20T08:05:38Z</dc:date>
    </item>
    <item>
      <title>Re: Attach file to deal record using API</title>
      <link>https://community.hubspot.com/t5/APIs-Integrations/Attach-file-to-deal-record-using-API/m-p/869990#M68535</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I belive I have this mostly worked out,&amp;nbsp; however when I associate the deal to the note.&amp;nbsp;&amp;nbsp; I appears that the attachement is being deleted.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;EDIT:&amp;nbsp; I was using an invalid ID field for the attachment&lt;/P&gt;</description>
      <pubDate>Wed, 25 Oct 2023 18:52:06 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/APIs-Integrations/Attach-file-to-deal-record-using-API/m-p/869990#M68535</guid>
      <dc:creator>MKaneko</dc:creator>
      <dc:date>2023-10-25T18:52:06Z</dc:date>
    </item>
    <item>
      <title>Re: Attach file to deal record using API</title>
      <link>https://community.hubspot.com/t5/APIs-Integrations/Attach-file-to-deal-record-using-API/m-p/886283#M69370</link>
      <description>&lt;P&gt;Very helpful, thanks!&lt;/P&gt;</description>
      <pubDate>Wed, 29 Nov 2023 09:57:06 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/APIs-Integrations/Attach-file-to-deal-record-using-API/m-p/886283#M69370</guid>
      <dc:creator>lennart-sve</dc:creator>
      <dc:date>2023-11-29T09:57:06Z</dc:date>
    </item>
    <item>
      <title>Re: Attach file to deal record using API</title>
      <link>https://community.hubspot.com/t5/APIs-Integrations/Attach-file-to-deal-record-using-API/m-p/892163#M69654</link>
      <description>&lt;P&gt;Glad I could help! — Jaycee&lt;/P&gt;</description>
      <pubDate>Mon, 11 Dec 2023 20:44:00 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/APIs-Integrations/Attach-file-to-deal-record-using-API/m-p/892163#M69654</guid>
      <dc:creator>Jaycee_Lewis</dc:creator>
      <dc:date>2023-12-11T20:44:00Z</dc:date>
    </item>
    <item>
      <title>Re: Attach file to deal record using API</title>
      <link>https://community.hubspot.com/t5/APIs-Integrations/Attach-file-to-deal-record-using-API/m-p/902997#M70183</link>
      <description>&lt;P&gt;Great solution, thanks Jaycee!&lt;/P&gt;</description>
      <pubDate>Tue, 09 Jan 2024 17:55:43 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/APIs-Integrations/Attach-file-to-deal-record-using-API/m-p/902997#M70183</guid>
      <dc:creator>HarrisDoug</dc:creator>
      <dc:date>2024-01-09T17:55:43Z</dc:date>
    </item>
    <item>
      <title>Re: Attach file to deal record using API</title>
      <link>https://community.hubspot.com/t5/APIs-Integrations/Attach-file-to-deal-record-using-API/m-p/915203#M70761</link>
      <description>&lt;P&gt;here's a post of the actual code in python, assuming you've got a url to the attachment you want to associate.&lt;/P&gt;&lt;P&gt;literally just fill in your auth_key and HubSpot ID and this will work.&lt;/P&gt;&lt;P&gt;your private app (with the auth_key) will need to have deal permissions and file permissions&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# this script uploads a file to hubspot, and then associates it to a deal using a note
# from this hubspot community post: https://community.hubspot.com/t5/APIs-Integrations/Attach-file-to-deal-record-using-API/m-p/719945#M58797
# from this hubspot files documentation: https://developers.hubspot.com/docs/api/files/files
# from this hubspot notes documentation: https://developers.hubspot.com/docs/api/crm/notes
# from this hubspot associations documentation: https://developers.hubspot.com/docs/api/crm/associations

import requests, json, time

auth_key = "YOUR_AUTH_KEY"
HUB_ID = YOUR_HUB_ID     # just for the print statements at the end

def upload_file(file_name, file_url):
    file_id = ""

    # upload the file
    url = f"https://api.hubapi.com/files/v3/files/import-from-url/async"
    headers = {'content-type': 'application/json','authorization': f"Bearer {auth_key}"}
    data = {
        "folderPath": "/",
        # "folderId": ""
        "access": "PUBLIC_INDEXABLE",
        "name": file_name,
        "url": file_url
    }

    response = requests.post(url, headers=headers, data=json.dumps(data))
    print(response.status_code)
    # print(response.text)
    task_id = json.loads(response.text)['id']

    # ping until the upload is completed, then return the file ID
    while True:
        time.sleep(2)
        url = f"https://api.hubapi.com/files/v3/files/import-from-url/async/tasks/{task_id}/status"
        response = requests.get(url, headers=headers)
        print(response.status_code)
        response_json = json.loads(response.text)
        if response_json['status'] == "COMPLETE":
            file_id = response_json['result']['id']
            break
        
    return file_id


def associate_file_deal(fileId, dealId):
    # associate the file to a note &amp;amp; associate the note to a deal
    url = f"https://api.hubapi.com/crm/v3/objects/notes"
    headers = {'content-type': "application/json",'authorization': f"Bearer {auth_key}"}
    data = {
        "associations": [{
            "types": [{
                "associationCategory": "HUBSPOT_DEFINED",
                "associationTypeId": 214
            }],
            "to": {
                "id": dealId
            }
        }],
        "properties": {
            "hs_note_body": "",
            "hs_timestamp": int(time.time())*1000,  # current time in ms
            "hs_attachment_ids": fileId
        }
    }

    response = requests.post(url, headers=headers, data=json.dumps(data))
    print(response.status_code)
    # print(response.text)


if __name__ == "__main__":
    file_name = "catanrules.pdf"
    file_url = "https://www.catan.com/sites/default/files/2021-06/catan_base_rules_2020_200707.pdf"
    deal_id = 15236862608

    file_id = upload_file(file_name, file_url)
    associate_file_deal(file_id, deal_id)

    print(f"https://app.hubspot.com/files/{HUB_ID}/?sortDirection=descending&amp;amp;orderBy=updated&amp;amp;showDetails={file_id}")
    print(f"https://app.hubspot.com/contacts/{HUB_ID}/record/0-3/{deal_id}")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 01 Feb 2024 05:15:58 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/APIs-Integrations/Attach-file-to-deal-record-using-API/m-p/915203#M70761</guid>
      <dc:creator>riipenadmin</dc:creator>
      <dc:date>2024-02-01T05:15:58Z</dc:date>
    </item>
    <item>
      <title>Re: Attach file to deal record using API</title>
      <link>https://community.hubspot.com/t5/APIs-Integrations/Attach-file-to-deal-record-using-API/m-p/986216#M74096</link>
      <description>&lt;P&gt;Hello guys, we are new users on hubspot and recently started using Hubspot. We are currently evaluating the hubspot and trying to view/download the file attachments that comes through our website to Hubspot. We see the right file name on the object but trying to view/download the file name it gives us error of "This doesn't appear to reference a file in your hubspot account. It may have been edited or removed".&amp;nbsp;&lt;/P&gt;&lt;P&gt;We tried creating a custom field called attachments and make its type as file but no results. Can anyone walkthrough us with step by step guide? Very obliged to get the support from community.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 03 Jun 2024 17:52:26 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/APIs-Integrations/Attach-file-to-deal-record-using-API/m-p/986216#M74096</guid>
      <dc:creator>AEACGi</dc:creator>
      <dc:date>2024-06-03T17:52:26Z</dc:date>
    </item>
  </channel>
</rss>

