API to Merge Contacts

I am trying to use the HubSpot API using this method: Accounts Dashboard | HubSpot.

I have a CSV of “New Contact IDs” that need to be merged with the duplicate “Old Contact IDs”, so I am iterating through these to bulk update by replacing the ID in the URL and the vidToMerge ID. However, I keep getting 404 and 405 errors saying the resource is not found. Here is what I have:

import csv
import pandas as pd
from pandas import *

# reading CSV file
data = read_csv("sample_import.csv")
# converting column data to list
main_contacts = data['New_Contact_ID'].tolist()
contacts_to_merge = data['Old_Contact_ID'].tolist()

for main_id in main_contacts:
 endpoint = requests.get('https://api.hubapi.com/contacts/v1/contact/merge-vids/{}/?hapikey=xxxx'.format(id))
 for old_id in contacts_to_merge:
 {
 "vidToMerge": old_id
 }

Thank you for your post @lsignori .

Hello @JBeatty and @PCarlson , would you be able to suggest an advice for @lsignori ?

Hi @lsignori,

You are making a get request to a post endpoint, you need to modify your code to follow the request’s post documentation. The reason you are getting a 404 is because that endpoint does not exist with a get method, it only exists with a post method.

#Instead of this
endpoint = requests.get('https://api.hubapi.com/contacts/v1/contact/merge-vids/{}/?hapikey=xxxx'.format(id))

#Format your request like this, though you will need to change your loops, and how they nest
endpoint = requests.post('https://api.hubapi.com/contacts/v1/contact/merge-vids/{}/?hapikey=xxxx'.format(id), data={"vidToMerge": 1343774})

Update:

id_num = 0
for main_id in main_contacts:
 old_id = contacts_to_merge[id_num]
 print(main_id, old_id)
 vidToMerge = old_id
 endpoint = requests.post('https://api.hubapi.com/contacts/v1/contact/merge-vids/{}/?hapikey=xxx'.format(main_id), data={"vidToMerge": vidToMerge})
 id_num += 1
 print(endpoint)

This seems to work but I always get a 415 error:

174105 1216500 <Response [415]>

174126 1194857 <Response [415]>

174210 1732125 <Response [415]>

174222 1196561 <Response [415]>

174263 1730569 <Response [415]>

174105 1216500 <Response [415]> <html> <head> <meta http-equiv=“Content-Type” content=“text/html;charset=utf-8”/> <title>Error 415 Unsupported Media Type</title> </head> <body><h2>HTTP ERROR 415</h2> <p>Problem accessing /contacts/v1/contact/merge-vids/174105/. Reason: <pre> Unsupported Media Type</pre></p> </body> </html> 174126 1194857 <Response [415]> <html> <head> <meta http-equiv=“Content-Type” content=“text/html;charset=utf-8”/> <title>Error 415 Unsupported Media Type</title> </head> <body><h2>HTTP ERROR 415</h2> <p>Problem accessing /contacts/v1/contact/merge-vids/174126/. Reason: <pre> Unsupported Media Type</pre></p> </body> </html>

FINAL UPDATE (this worked)

headers = {}
headers["Content-Type"]="application/json"
querystring = {"hapikey":"xxx"}
id_num = 0
for main_id in main_contacts:
 old_id = contacts_to_merge[id_num]
 print(main_id, old_id)
 vidToMerge = old_id
 url = 'https://api.hubapi.com/contacts/v1/contact/merge-vids/{}/?hapikey=xxx'.format(main_id)
 payload=json.dumps({"vidToMerge": vidToMerge})
 id_num += 1
 response = requests.request("POST", url, data=payload, headers=headers, params=querystring)
 print(response)
 print(response.text)