# API / Add contacts to a static list

**URL:** <https://community.hubspot.com/t/api-add-contacts-to-a-static-list/40640>\
**Category:** APIs & Integrations\
**Tags:** developer-tools\
**Created:** [July 13, 2021, 11:48am UTC](https://community.hubspot.com/t/api-add-contacts-to-a-static-list/40640 "2021-07-13T11:48:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![AbdelMOULIDA](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/abdelmoulida/32/76793_2.png) [@AbdelMOULIDA](https://community.hubspot.com/u/AbdelMOULIDA)\
**Post date:** [July 13, 2021, 11:48am UTC](https://community.hubspot.com/t/api-add-contacts-to-a-static-list/40640/1 "2021-07-13T11:48:42Z")

</div>

Hello,

_ **Context** _:  
Every week I do two to three contact imports that I add to a statistical list afterwards to trigger a workflow (send emails).  
I want to automate these imports using Python/API-HubSpot.  
I coded a code that doesn’t work, I don’t really understand where the error comes from.

What I want to do: import contacts on HubSpot with the API and then add them in my static list.

_ **My code** _:

```

AddList = “[https://api.hubapi.com/contacts/v1/lists/LIST\_ID/add?hapikey=API-Key](https://api.hubapi.com/contacts/v1/lists/LIST_ID/add?hapikey=API-Key)”

emailList = “”

with open(“contacts - in.csv”) as csv\_file:  
csv\_reader = csv.reader(csv\_file, delimiter=‘,’)  
line\_count = 0  
for row in csv\_reader:  
# This will skip the header row;  
if line\_count == 0:  
line\_count += 1  
pass  
else:  
# My emails happen to be in column c, you may need to change this.  
# If the csv file is open in excel column A = 0, B = 1, C = 2, D = 3…  
emailList += “\”“+row[2] + “\”,\n”  
line\_count += 1

mydata = “”“{  
“vids”: [],  
“emails”: [  
“””+emailList[:-2]+“”" #This will remove the extra spaces and characters at the end  
]  
}“”"

headers = {‘content-type’: ‘application/json’}

response = requests.request(“POST”, AddList, headers = headers, data = mydata)  
print(response.text.encode(‘utf8’))  
print(response.status\_code)

```

_ **ERROR:** _

![](https://d2qpr2jzcazej7.cloudfront.net/original/3X/1/4/1473ad178457032f4561ac9f3f3ce80fe4ab3862.png)

Thanks for your help 🙂

---

<div class="post-metadata">

**Author:** ![dennisedson](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/dennisedson/32/28044_2.png) [@dennisedson](https://community.hubspot.com/u/dennisedson)\
**Post date:** [July 13, 2021, 2:48pm UTC](https://community.hubspot.com/t/api-add-contacts-to-a-static-list/40640/2 "2021-07-13T14:48:34Z")

</div>

@AbdelMOULIDA ,

Any possibility of printing out what the json body looks like that you are sending?

---

<div class="post-metadata">

**Author:** ![AbdelMOULIDA](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/abdelmoulida/32/76793_2.png) [@AbdelMOULIDA](https://community.hubspot.com/u/AbdelMOULIDA)\
**Post date:** [July 13, 2021, 3:07pm UTC](https://community.hubspot.com/t/api-add-contacts-to-a-static-list/40640/3 "2021-07-13T15:07:07Z")

</div>

Hello @dennisedson , thanks for your feedback.

If the contact already exist on HubSpot (It works):

![](https://d2qpr2jzcazej7.cloudfront.net/original/3X/a/1/a17cf7ed304f2886bf8b236c6c8d4ac0dfcfc6d0.png)

If the contact doesn’t exist on HubSpot, I have this error and the contact is not created:

![AbdelMOULIDA_2-1626188748297.png](https://d2qpr2jzcazej7.cloudfront.net/original/3X/9/5/950743c99eed5d41c0be77a9e8c768123ab82903.png)

---

<div class="post-metadata">

**Author:** ![dennisedson](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/dennisedson/32/28044_2.png) [@dennisedson](https://community.hubspot.com/u/dennisedson)\
**Post date:** [July 14, 2021, 2:24pm UTC](https://community.hubspot.com/t/api-add-contacts-to-a-static-list/40640/4 "2021-07-14T14:24:18Z")

</div>

@AbdelMOULIDA , the contact needs to exist before you can add it to the list.

---

<div class="post-metadata">

**Author:** ![system](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/system/32/124_2.png) [@system](https://community.hubspot.com/u/system)\
**Post date:** [July 14, 2021, 9:55am UTC](https://community.hubspot.com/t/api-add-contacts-to-a-static-list/40640/5 "2021-07-14T09:55:15Z")

</div>

Hi @AbdelMOULIDA ,  
Your input data contains “#” inside it which is a special character and need to be escaped.Do the string escaping of your content before passing the info through json.

Hope this helps!

If we were able to answer your query, kindly help the community by marking it as a solution.  
Thanks and Regards.

---

<div class="post-metadata">

**Author:** ![AbdelMOULIDA](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/abdelmoulida/32/76793_2.png) [@AbdelMOULIDA](https://community.hubspot.com/u/AbdelMOULIDA)\
**Post date:** [July 16, 2021, 7:41am UTC](https://community.hubspot.com/t/api-add-contacts-to-a-static-list/40640/6 "2021-07-16T07:41:27Z")

</div>

Hi [@webdew](https://community.hubspot.com/t5/user/viewprofilepage/user-id/131510) ,

Thank you, It works :).

Best regards.
