# API to Merge Contacts

**URL:** <https://community.hubspot.com/t/api-to-merge-contacts/42240>\
**Category:** APIs & Integrations\
**Tags:** python, contacts, starter\
**Created:** [August 12, 2021, 3:27pm UTC](https://community.hubspot.com/t/api-to-merge-contacts/42240 "2021-08-12T15:27:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![lsignori](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/lsignori/32/99949_2.png) [@lsignori](https://community.hubspot.com/u/lsignori)\
**Post date:** [August 12, 2021, 3:27pm UTC](https://community.hubspot.com/t/api-to-merge-contacts/42240/1 "2021-08-12T15:27:20Z")

</div>

I am trying to use the HubSpot API using this method: [Accounts Dashboard | HubSpot](https://legacydocs.hubspot.com/docs/methods/contacts/merge-contacts).

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:

```lia-code-sample
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
 }

```

---

<div class="post-metadata">

**Author:** ![natsumimori](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/natsumimori/32/39807_2.png) [@natsumimori](https://community.hubspot.com/u/natsumimori)\
**Post date:** [August 13, 2021, 3:40am UTC](https://community.hubspot.com/t/api-to-merge-contacts/42240/2 "2021-08-13T03:40:02Z")

</div>

Thank you for your post @lsignori .

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

---

<div class="post-metadata">

**Author:** ![JBeatty](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/jbeatty/32/52252_2.png) [@JBeatty](https://community.hubspot.com/u/JBeatty)\
**Post date:** [August 13, 2021, 2:41pm UTC](https://community.hubspot.com/t/api-to-merge-contacts/42240/3 "2021-08-13T14:41:03Z")

</div>

Hi @lsignori,

You are making a get request to a post endpoint, you need to modify your code to follow the r[equest’s post documentation](https://docs.python-requests.org/en/latest/api/#requests.post). 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.

```lia-code-sample
#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})

```

---

<div class="post-metadata">

**Author:** ![lsignori](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/lsignori/32/99949_2.png) [@lsignori](https://community.hubspot.com/u/lsignori)\
**Post date:** [August 13, 2021, 3:14pm UTC](https://community.hubspot.com/t/api-to-merge-contacts/42240/4 "2021-08-13T15:14:41Z")

</div>

Update:

```lia-code-sample
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\>

---

<div class="post-metadata">

**Author:** ![lsignori](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/lsignori/32/99949_2.png) [@lsignori](https://community.hubspot.com/u/lsignori)\
**Post date:** [August 13, 2021, 6:12pm UTC](https://community.hubspot.com/t/api-to-merge-contacts/42240/5 "2021-08-13T18:12:06Z")

</div>

FINAL UPDATE (this worked)

```lia-code-sample
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)

```
