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!
Before we jump in, make sure you've got:
Let's get this show on the road:
mkdir connectwise-integration cd connectwise-integration npm init -y npm install axios dotenv
First things first, let's get you authenticated:
.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
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, }, });
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();
Let's run through the CRUD gamut:
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); } }
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); } }
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); } }
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); } }
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); } }
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!
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(); });
When deploying, remember to:
.env
file to version control.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!