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.
Feb 1, 202412:13 AM - edited Feb 1, 202412:15 AM
Participant
Attach file to deal record using API
SOLVE
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
# 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}")
Hi @Jaycee_Lewis , 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