Back

Step by Step Guide to Building a ConnectWise Manage API Integration in C#

Aug 16, 20246 minute read

Introduction

Hey there, fellow developer! Ready to dive into the world of ConnectWise Manage API integration? You're in for a treat. This guide will walk you through the process of building a robust integration using C#. We'll cover everything from authentication to handling complex endpoints, all while keeping things snappy and to the point. Let's get started!

Prerequisites

Before we jump in, make sure you've got:

  • Visual Studio or your favorite C# IDE
  • .NET Core SDK (latest version)
  • ConnectWise Manage API credentials (you've got these, right?)

Setting Up the Project

First things first, let's create a new C# project:

  1. Fire up Visual Studio
  2. Create a new .NET Core Console Application
  3. Name it something cool like "ConnectWiseIntegration"

Now, let's grab some NuGet packages:

Install-Package Newtonsoft.Json
Install-Package RestSharp

These will make our lives much easier when dealing with JSON and HTTP requests.

Authentication

ConnectWise uses API key authentication. Here's how to set it up:

var client = new RestClient("https://api-na.myconnectwise.net/v4_6_release/apis/3.0/"); client.AddDefaultHeader("Authorization", "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes($"{companyId}+{publicKey}:{privateKey}")));

Making API Requests

Let's create a base API client:

public class ConnectWiseClient { private readonly RestClient _client; public ConnectWiseClient(string companyId, string publicKey, string privateKey) { _client = new RestClient("https://api-na.myconnectwise.net/v4_6_release/apis/3.0/"); _client.AddDefaultHeader("Authorization", "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes($"{companyId}+{publicKey}:{privateKey}"))); } public T Get<T>(string resource) { var request = new RestRequest(resource, Method.GET); var response = _client.Execute<T>(request); return response.Data; } // Implement Post, Put, Delete methods similarly }

Working with ConnectWise Manage Endpoints

Now for the fun part! Let's interact with some endpoints:

var client = new ConnectWiseClient("companyId", "publicKey", "privateKey"); // Get companies var companies = client.Get<List<Company>>("/company/companies"); // Get tickets var tickets = client.Get<List<Ticket>>("/service/tickets"); // Create a time entry var timeEntry = new TimeEntry { /* populate properties */ }; client.Post("/time/entries", timeEntry);

Handling Responses and Errors

Always expect the unexpected:

try { var result = client.Get<T>(resource); // Process result } catch (Exception ex) { Console.WriteLine($"Oops! Something went wrong: {ex.Message}"); // Log the error, maybe retry the request }

Implementing Pagination

ConnectWise API uses continuation tokens for pagination. Here's how to handle it:

string continuationToken = null; do { var request = new RestRequest("/company/companies", Method.GET); if (continuationToken != null) request.AddQueryParameter("continuationToken", continuationToken); var response = _client.Execute<List<Company>>(request); // Process companies continuationToken = response.Headers.FirstOrDefault(h => h.Name == "Continuation-Token")?.Value.ToString(); } while (continuationToken != null);

Best Practices

  • Respect rate limits (check headers for limit info)
  • Use caching when appropriate
  • Implement retry logic for failed requests
  • Use async/await for better performance

Testing and Debugging

Always test your integration thoroughly:

[Fact] public void CanGetCompanies() { var client = new ConnectWiseClient("testCompanyId", "testPublicKey", "testPrivateKey"); var companies = client.Get<List<Company>>("/company/companies"); Assert.NotEmpty(companies); }

Conclusion

And there you have it! You're now equipped to build a solid ConnectWise Manage API integration. Remember, this is just the beginning. There's a whole world of endpoints and features to explore. Keep experimenting, and don't be afraid to dive deeper into the API documentation.

Additional Resources

Happy coding, and may your integration be ever successful!