# Attach file to deal record using API

**URL:** <https://community.hubspot.com/t/attach-file-to-deal-record-using-api/69500>\
**Category:** APIs & Integrations\
**Tags:** python\
**Created:** [November 17, 2022, 7:42pm UTC](https://community.hubspot.com/t/attach-file-to-deal-record-using-api/69500 "2022-11-17T19:42:24Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![SSharma043](https://avatars.discourse-cdn.com/v4/letter/s/c37758/32.png) [@SSharma043](https://community.hubspot.com/u/SSharma043)\
**Post date:** [November 17, 2022, 7:42pm UTC](https://community.hubspot.com/t/attach-file-to-deal-record-using-api/69500/1 "2022-11-17T19:42:24Z")

</div>

I have uploaded File in Hubspot using API.  
Now I want attach that to deal record using API  
How can I do it  
Please Help!!

I’ve attached screenshots for refrence

 ![image_2.png](https://d2qpr2jzcazej7.cloudfront.net/original/3X/a/c/ac7b43bc5882c723899eca53906e6a971ba7f1b4.png)  
This file I want to attach to deal record

 ![image_5.png](https://d2qpr2jzcazej7.cloudfront.net/original/2X/a/a7dc26e7ca95d1db38fe15538d38c9fbbca30f25.png)  
Here I want to attach file using API

---

<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:** [November 22, 2022, 7:46pm UTC](https://community.hubspot.com/t/attach-file-to-deal-record-using-api/69500/2 "2022-11-22T19:46:10Z")

</div>

Hi, @SSharma043 👋 Were you able to get this one sorted out? If not, I’ll share a quick example using Postman.

Summary:

- Upload the file
- Create the Note
- Associate the File to the Note
- Associate the Note to the Object

Steps

- Upload your file via API and note the value for “id”

 ![files-1.png](https://d2qpr2jzcazej7.cloudfront.net/original/3X/f/b/fbb95fef9483b34377273acbcb592d70d60a457e.png)

- Files added can be associated to an Object as a Note

- Use the 'id” vale as the value for “hs\_attachment\_ids” when creating a note  
`POST https://api.hubapi.com/crm/v3/objects/notes​`  

- Then you can make a call to associate the Note to a Deal  
`PUT https://api.hubspot.com/crm/v3/objects/notes/{note_ID}/associations/deal/{deal_ID}/214​`

 ![files-3.png](https://d2qpr2jzcazej7.cloudfront.net/original/2X/b/bb6d8004b47be43b2603db8d9c6de4714253afb9.png)

 ![files-4.png](https://d2qpr2jzcazej7.cloudfront.net/original/3X/0/4/04d36f1d30882d08f3dc7458420b6e162c1dd9ba.png)

I hope this helps you or the next person who is looking to upload and associate files to objects in this manner.

Best,

Jaycee

---

<div class="post-metadata">

**Author:** ![DzungNguyen](https://avatars.discourse-cdn.com/v4/letter/d/ebca7d/32.png) [@DzungNguyen](https://community.hubspot.com/u/DzungNguyen)\
**Post date:** [November 24, 2022, 7:09am UTC](https://community.hubspot.com/t/attach-file-to-deal-record-using-api/69500/3 "2022-11-24T07:09:53Z")

</div>

Thanks for your solution! it works for me

---

<div class="post-metadata">

**Author:** ![03482](https://avatars.discourse-cdn.com/v4/letter/0/e95f7d/32.png) [@03482](https://community.hubspot.com/u/03482)\
**Post date:** [May 12, 2023, 7:16am UTC](https://community.hubspot.com/t/attach-file-to-deal-record-using-api/69500/4 "2023-05-12T07:16:54Z")

</div>

[@Jaycee\_Lewis](https://community.hubspot.com/t5/user/viewprofilepage/user-id/127074) can you breife the request body and headers what you are paasing while uploading the file

---

<div class="post-metadata">

**Author:** ![IHernandez4](https://avatars.discourse-cdn.com/v4/letter/i/22d042/32.png) [@IHernandez4](https://community.hubspot.com/u/IHernandez4)\
**Post date:** [June 20, 2023, 8:05am UTC](https://community.hubspot.com/t/attach-file-to-deal-record-using-api/69500/5 "2023-06-20T08:05:38Z")

</div>

Hi [@Jaycee\_Lewis](https://community.hubspot.com/t5/user/viewprofilepage/user-id/127074) , 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

---

<div class="post-metadata">

**Author:** ![MKaneko](https://avatars.discourse-cdn.com/v4/letter/m/3d9bf3/32.png) [@MKaneko](https://community.hubspot.com/u/MKaneko)\
**Post date:** [October 25, 2023, 4:30pm UTC](https://community.hubspot.com/t/attach-file-to-deal-record-using-api/69500/6 "2023-10-25T16:30:28Z")

</div>

Hi,

I belive I have this mostly worked out, however when I associate the deal to the note. I appears that the attachement is being deleted.

EDIT: I was using an invalid ID field for the attachment

---

<div class="post-metadata">

**Author:** ![HarrisDoug](https://avatars.discourse-cdn.com/v4/letter/h/db5fbb/32.png) [@HarrisDoug](https://community.hubspot.com/u/HarrisDoug)\
**Post date:** [January 9, 2024, 5:55pm UTC](https://community.hubspot.com/t/attach-file-to-deal-record-using-api/69500/7 "2024-01-09T17:55:43Z")

</div>

Great solution, thanks Jaycee!

---

<div class="post-metadata">

**Author:** ![lennart-sve](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/lennart-sve/32/5494_2.png) [@lennart-sve](https://community.hubspot.com/u/lennart-sve)\
**Post date:** [November 29, 2023, 9:57am UTC](https://community.hubspot.com/t/attach-file-to-deal-record-using-api/69500/8 "2023-11-29T09:57:06Z")

</div>

Very helpful, thanks!

---

<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:** [December 11, 2023, 8:44pm UTC](https://community.hubspot.com/t/attach-file-to-deal-record-using-api/69500/9 "2023-12-11T20:44:00Z")

</div>

Glad I could help! — Jaycee

---

<div class="post-metadata">

**Author:** ![riipenadmin](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/riipenadmin/32/84057_2.png) [@riipenadmin](https://community.hubspot.com/u/riipenadmin)\
**Post date:** [February 1, 2024, 5:13am UTC](https://community.hubspot.com/t/attach-file-to-deal-record-using-api/69500/10 "2024-02-01T05:13:01Z")

</div>

here’s a post of the actual code in python, assuming you’ve got a url to the attachment you want to associate.

literally just fill in your auth\_key and HubSpot ID and this will work.

your private app (with the auth\_key) will need to have deal permissions and file permissions

```lia-code-sample
# 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 & 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&orderBy=updated&showDetails={file_id}")
 print(f"https://app.hubspot.com/contacts/{HUB_ID}/record/0-3/{deal_id}")

```

---

<div class="post-metadata">

**Author:** ![AEACGi](https://avatars.discourse-cdn.com/v4/letter/a/82dd89/32.png) [@AEACGi](https://community.hubspot.com/u/AEACGi)\
**Post date:** [June 3, 2024, 5:52pm UTC](https://community.hubspot.com/t/attach-file-to-deal-record-using-api/69500/11 "2024-06-03T17:52:26Z")

</div>

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”.

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.
