I struggled for a while with uploading documents to Connectwise via the API. This post has waited to be born for about a year, it was information I didn’t want to loose so I kept it in a dusty folder on my desktop.
The information presented is aimed at the Connectwise ReST API but who knows, it may be handy for someone else struggling with a similar problem. I needed to be able to upload files to a Connectwise Service Ticket. Looking at the API documentation resulted in more questions than answers.
Just use the SDK provided by Connectwise
I have used the SDK before, my problem with the SDK is that it seemed to break quite often and need updated while my manual code using HttpClient survived without issues. I wanted to stick with my code and not rely on a third party dll.
The Client
I’m assuming that you are already using System.Net.Http.HttpClient and not the SDK and that you know how to configure the client. If not then this is how I configure my HttpClient. I pull the information for Codebase from the official endpoint https://na.myconnectwise.net/login/companyinfo/YOUR_COMPANY_NAME
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Get Company Info
CWCompanyInfocompanyInfo=awaitGetCompanyInfo();// Create Client
HttpClienthttpClient=newHttpClient(){BaseAddress=newUri($"https://{companyInfo.SiteUrl}/{companyInfo.Codebase}apis/3.0")};httpClient.DefaultRequestHeaders.Add("Authorization",string.Format("Basic {0}",Base64Encode(string.Format("COMPANY+{0}:{1}",Registry.GetValue(CWCLIENT_REGISTRY_ROOT,"pub",""),Registry.GetValue(CWCLIENT_REGISTRY_ROOT,"priv","")))));httpClient.DefaultRequestHeaders.Add("Accept",$"application/vnd.connectwise.com+json; version={companyInfo.VersionCode}");httpClient.DefaultRequestHeaders.Add("clientId","CONNECTWISE-CLIENTID-HERE");httpClient.DefaultRequestHeaders.Add("cache-control","no-cache");
The Endpoint
The endpoint we will end up using is under ‘Documents’ in the API specs, [POST] /system/documents.
Connectwise Endpoint
The endpoint lists the following schema which led me to question if this was even the right endpoint.
Using HttpClient was actually easier than I had hoped. I found a few other random articles that demonstrated file uploads to other APIs and finally pieced it together.