Back

Step by Step Guide to Building a ConnectWise Manage API Integration in JS

Aug 16, 20247 minute read

Introduction

Hey there, fellow dev! 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 JavaScript. We'll cover everything from setup to deployment, so buckle up!

Prerequisites

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

  • Node.js and npm installed (you know the drill)
  • A ConnectWise Manage account with API credentials (if you don't have these, go bug your admin!)

Setting up the project

Let's get this show on the road:

mkdir connectwise-integration cd connectwise-integration npm init -y npm install axios dotenv

Authentication

First things first, let's get you authenticated:

  1. Grab your API keys from ConnectWise Manage.
  2. Create a .env file in your project root:
CW_COMPANY_ID=your_company_id
CW_PUBLIC_KEY=your_public_key
CW_PRIVATE_KEY=your_private_key
CW_CLIENT_ID=your_client_id
  1. Now, let's set up authentication in your index.js:
require('dotenv').config(); const axios = require('axios'); const auth = Buffer.from(`${process.env.CW_COMPANY_ID}+${process.env.CW_PUBLIC_KEY}:${process.env.CW_PRIVATE_KEY}`).toString('base64'); const api = axios.create({ baseURL: 'https://api-na.myconnectwise.net/v4_6_release/apis/3.0', headers: { 'Authorization': `Basic ${auth}`, 'clientId': process.env.CW_CLIENT_ID, }, });

Making API requests

Now that we're authenticated, let's make a simple GET request:

async function getCompanyInfo() { try { const response = await api.get('/company/info'); console.log(response.data); } catch (error) { console.error('Error:', error.response ? error.response.data : error.message); } } getCompanyInfo();

CRUD operations

Let's run through the CRUD gamut:

Create (POST)

async function createTicket(summary, description) { try { const response = await api.post('/service/tickets', { summary, description, board: { id: 1 }, // Adjust as needed }); console.log('Ticket created:', response.data); } catch (error) { console.error('Error creating ticket:', error.response ? error.response.data : error.message); } }

Read (GET)

async function getTickets(pageSize = 25, page = 1) { try { const response = await api.get('/service/tickets', { params: { pageSize, page }, }); console.log('Tickets:', response.data); } catch (error) { console.error('Error fetching tickets:', error.response ? error.response.data : error.message); } }

Update (PUT)

async function updateTicket(ticketId, updates) { try { const response = await api.put(`/service/tickets/${ticketId}`, updates); console.log('Ticket updated:', response.data); } catch (error) { console.error('Error updating ticket:', error.response ? error.response.data : error.message); } }

Delete (DELETE)

async function deleteTicket(ticketId) { try { await api.delete(`/service/tickets/${ticketId}`); console.log('Ticket deleted successfully'); } catch (error) { console.error('Error deleting ticket:', error.response ? error.response.data : error.message); } }

Pagination and filtering

ConnectWise API supports pagination and filtering. Here's how to use them:

async function getFilteredTickets(status, pageSize = 25, page = 1) { try { const response = await api.get('/service/tickets', { params: { 'conditions': `status/name="${status}"`, pageSize, page, }, }); console.log('Filtered Tickets:', response.data); } catch (error) { console.error('Error fetching filtered tickets:', error.response ? error.response.data : error.message); } }

Error handling and best practices

Always wrap your API calls in try-catch blocks and respect rate limits. ConnectWise has a default rate limit of 60 requests per minute, so keep an eye on that!

Testing the integration

For testing, I recommend using Jest. Here's a quick example:

const { getCompanyInfo } = require('./index'); test('getCompanyInfo returns data', async () => { const data = await getCompanyInfo(); expect(data).toBeDefined(); });

Deployment considerations

When deploying, remember to:

  1. Use environment variables for all sensitive info.
  2. Never commit your .env file to version control.
  3. Consider using a secrets manager in production.

Conclusion

And there you have it! You've just built a ConnectWise Manage API integration in JavaScript. Pretty cool, right? Remember, this is just scratching the surface. The ConnectWise API has a ton of endpoints to explore, so don't be afraid to dig deeper.

For more info, check out the official ConnectWise API docs. Happy coding!