APIs & Integrations

SSharma043
Participant

Attach file to deal record using API

SOLVE

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 refrenceThis file I want to attach to deal recordThis file I want to attach to deal record

Here I want to attach file using APIHere I want to attach file using API

0 Upvotes
1 Accepted solution
Jaycee_Lewis
Solution
Community Manager
Community Manager

Attach file to deal record using API

SOLVE

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
  •  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​
    files-2.png
  •   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
    files-4.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

 

linkedin

Jaycee Lewis

Developer Community Manager

Community | HubSpot

View solution in original post

9 Replies 9
riipenadmin
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}")

 

 

 

 

0 Upvotes
lennart-sve
Participant

Attach file to deal record using API

SOLVE

Very helpful, thanks!

Jaycee_Lewis
Community Manager
Community Manager

Attach file to deal record using API

SOLVE

Glad I could help! — Jaycee

linkedin

Jaycee Lewis

Developer Community Manager

Community | HubSpot

Jaycee_Lewis
Solution
Community Manager
Community Manager

Attach file to deal record using API

SOLVE

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
  •  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​
    files-2.png
  •   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
    files-4.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

 

linkedin

Jaycee Lewis

Developer Community Manager

Community | HubSpot

HarrisDoug
Participant

Attach file to deal record using API

SOLVE

Great solution, thanks Jaycee!

MKaneko
Member

Attach file to deal record using API

SOLVE

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

IHernandez4
Member

Attach file to deal record using API

SOLVE

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

0 Upvotes
03482
Member

Attach file to deal record using API

SOLVE

@Jaycee_Lewis can you breife the request body and headers what you are paasing while uploading the file

0 Upvotes
DzungNguyen
Participant

Attach file to deal record using API

SOLVE

Thanks for your solution! it works for me