# Python - help uploading a file via CMS API

**URL:** <https://community.hubspot.com/t/python-help-uploading-a-file-via-cms-api/68164>\
**Category:** APIs & Integrations\
**Tags:** python, cms-content\
**Created:** [October 29, 2022, 2:54pm UTC](https://community.hubspot.com/t/python-help-uploading-a-file-via-cms-api/68164 "2022-10-29T14:54:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![PedroTayer](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/pedrotayer/32/60590_2.png) [@PedroTayer](https://community.hubspot.com/u/PedroTayer)\
**Post date:** [October 29, 2022, 2:54pm UTC](https://community.hubspot.com/t/python-help-uploading-a-file-via-cms-api/68164/1 "2022-10-29T14:54:55Z")

</div>

Hi guys, I’m trying to upload a .csv file to file manager via [Accounts Dashboard | HubSpot](https://developers.hubspot.com/docs/api/files/files) but I get a 400. The lack of example on documentation made me come here.

My headers and url are:

`headers = {'content-type' : 'multipart/form-data', 'authorization': myauth}url = 'https://api.hubapi.com/files/v3/files'`

Im trying to:

`with open(path_to_csv_file) as fh: data = {'file': fh, 'folderId': '89858067367', 'fileName': 'test', 'options': json.dumps({ 'access': 'PRIVATE', 'overwrite': False, 'duplicateValidationStrategy': 'NONE', 'duplicateValidationScope': 'EXACT_FOLDER' }) } req = requests.post(url, files=data, headers=headers)`

Where am I goind wrong?

---

<div class="post-metadata">

**Author:** ![Jaycee\_Lewis](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/jaycee_lewis/32/61352_2.png) [@Jaycee\_Lewis](https://community.hubspot.com/u/Jaycee_Lewis)\
**Post date:** [October 31, 2022, 3:42pm UTC](https://community.hubspot.com/t/python-help-uploading-a-file-via-cms-api/68164/2 "2022-10-31T15:42:14Z")

</div>

Hi, @PedroTayer 👋 Thanks for reaching out! Hey, @taran42 @JBeatty, do you have any experience or troubleshooting tips you can share with @PedroTayer?

Thank you very much! — Jaycee

---

<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:** [October 31, 2022, 7:33pm UTC](https://community.hubspot.com/t/python-help-uploading-a-file-via-cms-api/68164/3 "2022-10-31T19:33:29Z")

</div>

Hi @PedroTayer,

2 things, first you don’t want to specify the content type, because for multipart/form-data encoding that contains the boundary and you are overwriting that, and secondly you want to use the files parameter for the post function to tell requests to do multipart/form-data. Here is some sample code I got working:

```lia-code-sample
import json, requests, os
BEARER_TOKEN = os.environ['BEARER_TOKEN']

headers = {'authorization': "Bearer " + BEARER_TOKEN}
url = 'https://api.hubapi.com/files/v3/files'

with open("test.txt") as fh:
 files = {'file': fh}
 data = {
 'folderId': '',
 'fileName': 'testFileName',
 'options': json.dumps({
 'access': 'PRIVATE',
 'overwrite': False,
 'duplicateValidationStrategy': 'NONE',
 'duplicateValidationScope': 'EXACT_FOLDER'
 })
 }

 resp = requests.post(url, files=files, data=data, headers=headers)

```

Best,

---

<div class="post-metadata">

**Author:** ![PedroTayer](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/pedrotayer/32/60590_2.png) [@PedroTayer](https://community.hubspot.com/u/PedroTayer)\
**Post date:** [October 31, 2022, 11:26pm UTC](https://community.hubspot.com/t/python-help-uploading-a-file-via-cms-api/68164/4 "2022-10-31T23:26:09Z")

</div>

Thank you very much it worked! I’ve uploaded it and associated with deal using v1 engagement considering the file as a note.

Just another thing, might help someone, I’ve managed to upload the file directly from a dataframe by using (the encoding is according to portuguese-br):

`import iocsv_buffer = io.BytesIO()df.to_csv(csv_buffer, sep=",", index=False, mode="wb", encoding="ISO-8859-1")csv_buffer.seek(0)files = {'file': csv_buffer}`
