# Upload a file using api In C#

**URL:** <https://community.hubspot.com/t/upload-a-file-using-api-in-c/28546>\
**Category:** APIs & Integrations\
**Tags:** sdk, starter\
**Created:** [November 30, 2020, 12:49pm UTC](https://community.hubspot.com/t/upload-a-file-using-api-in-c/28546 "2020-11-30T12:49:01Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Abhimanyu](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/abhimanyu/32/78108_2.png) [@Abhimanyu](https://community.hubspot.com/u/Abhimanyu)\
**Post date:** [November 30, 2020, 12:49pm UTC](https://community.hubspot.com/t/upload-a-file-using-api-in-c/28546/1 "2020-11-30T12:49:01Z")

</div>

Hi

I want to use this endpoint to upload file to hub spot in c#.

`http://api.hubapi.com/filemanager/api/v3/files/upload?hapikey=dem`

Documnet URL: [https://legacydocs.hubspot.com/docs/methods/files/v3/upload\_new\_file](https://legacydocs.hubspot.com/docs/methods/files/v3/upload_new_file)

I am not able to make request model for that any help will be appriciated.

Thank you.

---

<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:** [November 30, 2020, 6:33pm UTC](https://community.hubspot.com/t/upload-a-file-using-api-in-c/28546/2 "2020-11-30T18:33:40Z")

</div>

Hey @Abhimanyu

I know there is a [wrapper](https://github.com/hubspot-net/HubSpot.NET) that allows you to upload a file to an engagement. Maybe looking at that you can figure out how to compose this.

[@quentin\_lamamy](https://community.hubspot.com/t5/user/viewprofilepage/user-id/148828) , I know you usually work with the CRM, but have you tried uploading to the file manager?

---

<div class="post-metadata">

**Author:** ![quentin\_lamamy](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/quentin_lamamy/32/20212_2.png) [@quentin\_lamamy](https://community.hubspot.com/u/quentin_lamamy)\
**Post date:** [December 1, 2020, 12:34am UTC](https://community.hubspot.com/t/upload-a-file-using-api-in-c/28546/3 "2020-12-01T00:34:41Z")

</div>

@dennisedsonThanks for the ping

@AbhimanyuWhat is your actual code and what is the answer of the hubspot api ?

---

<div class="post-metadata">

**Author:** ![Abhimanyu](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/abhimanyu/32/78108_2.png) [@Abhimanyu](https://community.hubspot.com/u/Abhimanyu)\
**Post date:** [December 1, 2020, 10:32am UTC](https://community.hubspot.com/t/upload-a-file-using-api-in-c/28546/4 "2020-12-01T10:32:42Z")

</div>

Hi [@quentin\_lamamy](https://community.hubspot.com/t5/user/viewprofilepage/user-id/148828)  
Here’s my code

using (var request = new HttpRequestMessage(new HttpMethod(“POST”), “[http://api.hubapi.com/filemanager/api/v3/files/upload?hapikey=demo](http://api.hubapi.com/filemanager/api/v3/files/upload?hapikey=demo)”))  
{  
var multipartContent = new MultipartFormDataContent();  
var options = new  
{  
options = new { access = “PUBLIC\_INDEXABLE” }  
};  
var properties = JsonConvert.SerializeObject(options);  
var file1 = new ByteArrayContent(File.ReadAllBytes(“C:\\Users\\abhimanyu.kumar\\Downloads\\1.png”));  
file1.Headers.Add(“Content-Type”, “multipart/form-data”);  
multipartContent.Add(file1, “+”, Path.GetFileName(“1.png”));  
multipartContent.Add(new StringContent(properties));  
multipartContent.Add(new StringContent(“/Abhimanyu\_Test”), “folderPath”);  
request.Content = multipartContent;

var response = await httpClient.SendAsync(request);

}

And Response is

{StatusCode: 400, ReasonPhrase: ‘Bad Request’, Version: 1.1, Content: System.Net.Http.HttpConnection+HttpConnectionResponseContent, Headers:  
{  
Date: Tue, 01 Dec 2020 10:27:29 GMT  
Connection: keep-alive  
Set-Cookie: \_\_cfduid=d439fa73df3a0b3f3f5d5b3b862563a331606818449; expires=Thu, 31-Dec-20 10:27:29 GMT; path=/; [domain=.hubapi.com](http://domain=.hubapi.com); HttpOnly; SameSite=Lax  
X-Trace: 2B3B778C5B16B2FEEB75D4A27E9BE1A3C8C64CF3A1000000000000000000  
X-HubSpot-RateLimit-Daily: 500000  
X-HubSpot-RateLimit-Daily-Remaining: 499907  
X-HubSpot-RateLimit-Interval-Milliseconds: 10000  
X-HubSpot-RateLimit-Remaining: 99  
X-HubSpot-RateLimit-Max: 100  
X-HubSpot-RateLimit-Secondly: 10  
X-HubSpot-RateLimit-Secondly-Remaining: 9  
Access-Control-Allow-Credentials: false  
CF-Cache-Status: DYNAMIC  
cf-request-id: 06bf70e0e90000ab04850e7000000001  
CF-RAY: 5fac1dae3824ab04-MAA  
Cache-Control: no-store, must-revalidate, no-cache  
Server: cloudflare  
Content-Type: text/html; charset=iso-8859-1  
Content-Length: 267  
}}

---

<div class="post-metadata">

**Author:** ![quentin\_lamamy](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/quentin_lamamy/32/20212_2.png) [@quentin\_lamamy](https://community.hubspot.com/u/quentin_lamamy)\
**Post date:** [December 1, 2020, 11:05am UTC](https://community.hubspot.com/t/upload-a-file-using-api-in-c/28546/5 "2020-12-01T11:05:25Z")

</div>

Thanks for the answer, i suggest you to use the new endpoint documented [here](https://developers.hubspot.com/docs/api/cms/files) and use this php [working code](https://community.hubspot.com/t5/APIs-Integrations/CMS-Files-API-on-PHP-Upload-a-new-file-Error-400-Bad-Request/m-p/381560)

---

<div class="post-metadata">

**Author:** ![Abhimanyu](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/abhimanyu/32/78108_2.png) [@Abhimanyu](https://community.hubspot.com/u/Abhimanyu)\
**Post date:** [December 2, 2020, 11:02am UTC](https://community.hubspot.com/t/upload-a-file-using-api-in-c/28546/6 "2020-12-02T11:02:33Z")

</div>

Hi [@quentin\_lamamy](https://community.hubspot.com/t5/user/viewprofilepage/user-id/148828) @dennisedson  
this is working code but intead of file path “C:\\Users\\abhimanyu.kumar\\Downloads\\1.png” I wasnt to send bytes like  
var file = new ByteArrayContent(File.ReadAllBytes(“C:\\Users\\abhimanyu.kumar\\Downloads\\1.png”));  
file.Headers.Add(“Content-Type”, “application/octet-stream”);  
And I am trying it and response I am getting is **unspupported type  
What is supported type for file?**

**Working Code:**  
var options1 = new { access = “PUBLIC\_INDEXABLE” };

var properties1 = JsonConvert.SerializeObject(options1);  
var request1 = new RestRequest(Method.POST);  
var client = new RestClient(“[http://api.hubapi.com/files/v3/files?hapikey=demo](http://api.hubapi.com/files/v3/files?hapikey=demo)”);  
request1.AddFile(“file”, “C:\\Users\\abhimanyu.kumar\\Downloads\\1.png”);  
request1.AddParameter(“folderId”, “381726034078”);  
request1.AddParameter(“options”, properties1);  
IRestResponse response1 = client.Execute(request1);

---

<div class="post-metadata">

**Author:** ![quentin\_lamamy](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/quentin_lamamy/32/20212_2.png) [@quentin\_lamamy](https://community.hubspot.com/u/quentin_lamamy)\
**Post date:** [December 2, 2020, 1:15pm UTC](https://community.hubspot.com/t/upload-a-file-using-api-in-c/28546/7 "2020-12-02T13:15:44Z")

</div>

Maybe the file header is wrong but if you found a way to do it it’s a good new. C# is not my main programming lang, i use to learn at university but now i’m most of the time doing nodeJs and php

---

<div class="post-metadata">

**Author:** ![Abhimanyu](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/abhimanyu/32/78108_2.png) [@Abhimanyu](https://community.hubspot.com/u/Abhimanyu)\
**Post date:** [December 2, 2020, 8:37pm UTC](https://community.hubspot.com/t/upload-a-file-using-api-in-c/28546/8 "2020-12-02T20:37:35Z")

</div>

I am sending the file header ‘application/octet-stream’ as mention in the document [https://legacydocs.hubspot.com/docs/methods/files/v3/upload\_new\_file?\_ga=2.255046920.1059212467.1604502924-1273358196.1598626193](https://legacydocs.hubspot.com/docs/methods/files/v3/upload_new_file?_ga=2.255046920.1059212467.1604502924-1273358196.1598626193)  
And are you sending the path of the file or bytes?

---

<div class="post-metadata">

**Author:** ![quentin\_lamamy](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/quentin_lamamy/32/20212_2.png) [@quentin\_lamamy](https://community.hubspot.com/u/quentin_lamamy)\
**Post date:** [December 3, 2020, 10:17am UTC](https://community.hubspot.com/t/upload-a-file-using-api-in-c/28546/9 "2020-12-03T10:17:35Z")

</div>

Why are you using the old api ?

---

<div class="post-metadata">

**Author:** ![Abhimanyu](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/abhimanyu/32/78108_2.png) [@Abhimanyu](https://community.hubspot.com/u/Abhimanyu)\
**Post date:** [December 4, 2020, 4:12am UTC](https://community.hubspot.com/t/upload-a-file-using-api-in-c/28546/10 "2020-12-04T04:12:04Z")

</div>

I am using this api: [http://api.hubapi.com/files/v3/files?hapikey=key](http://api.hubapi.com/files/v3/files?hapikey=key)  
Main issue is file.

---

<div class="post-metadata">

**Author:** ![quentin\_lamamy](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/quentin_lamamy/32/20212_2.png) [@quentin\_lamamy](https://community.hubspot.com/u/quentin_lamamy)\
**Post date:** [December 4, 2020, 10:50am UTC](https://community.hubspot.com/t/upload-a-file-using-api-in-c/28546/11 "2020-12-04T10:50:11Z")

</div>

I send a message to someone from the hubspot team, maybe he can help you

---

<div class="post-metadata">

**Author:** ![zwolfson](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/zwolfson/32/81360_2.png) [@zwolfson](https://community.hubspot.com/u/zwolfson)\
**Post date:** [December 4, 2020, 2:57pm UTC](https://community.hubspot.com/t/upload-a-file-using-api-in-c/28546/12 "2020-12-04T14:57:11Z")

</div>

Hi @Abhimanyu

I think the issue is that the overall request should use the content type of multipart/form-data. Then the file itself should have applicaiton/octet-stream.

---

<div class="post-metadata">

**Author:** ![Abhimanyu](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/abhimanyu/32/78108_2.png) [@Abhimanyu](https://community.hubspot.com/u/Abhimanyu)\
**Post date:** [December 5, 2020, 5:40pm UTC](https://community.hubspot.com/t/upload-a-file-using-api-in-c/28546/13 "2020-12-05T17:40:37Z")

</div>

Hi finally I am to to upload bytes to hubspot in C#

**Working code**

var file = File.ReadAllBytes(“C:\\Users\\abhimanyu.kumar\\Downloads\\1.png”);  
var request = new RestRequest(Method.POST);  
var client = new RestClient(“[http://api.hubapi.com/files/v3/files?hapikey=key](http://api.hubapi.com/files/v3/files?hapikey=key)”);

request.AddFile(“file”, file, “FileName.png”, “application/octet-stream”);

request.AddParameter(“folderPath”, “/path”);  
request.AddParameter(“options”, properties);  
IRestResponse response = client.Execute(request);
