A Node.JS TypeScript module for interacting with the ConnectWise Manage and Automate REST APIs. This module provides bindings for ease of development against the ConnectWise REST APIs as well as pagination, automatic retries and logging.
Version 1.0 has been completely re-written and is automatically generated, some function names have changed as well as removal of subsections from version 0.x. Pagination API has been changed for easier usage. VSCode, JetBrains, etc editors will automatically pick up the new type definitions.
See this package's full documentation here
// ESM
import ManageAPI > from 'connectwise-rest';
// CommonJS
const ManageAPI > = require('connectwise-rest');
const cwm = new ManageAPI(
companyId: 'company',
companyUrl: 'your.connectwise.com',
publicKey: '',
privateKey: '',
clientId: '',
entryPoint: 'v4_6_release', // optional, defaults to 'v4_6_release'
apiVersion: '3.0.0', // optional, defaults to '3.0.0'
timeout: 20000, // optional, request connection timeout in ms, defaults to 20000
retry: false, // optional, defaults to false
retryOptions: // optional, override retry behavior, defaults as shown
retries: 4, // maximum number of retries
minTimeout: 50, // number of ms to wait between retries
maxTimeout: 20000, // maximum number of ms between retries
randomize: true, // randomize delay between retries on timeouts
>,
debug: false, // optional, enable debug logging
logger: (level, text, meta) => // optional, pass in logging function
>);
cwm.ServiceDeskAPI.Tickets.getTicketById(1234)
.then((ticket) =>
//do something with results
>)
.catch((error) =>
//handle errors
>);
// ESM
import AutomateAPI > from 'connectwise-rest';
// CommonJS
const AutomateAPI > = require('connectwise-rest');
const cwa = new AutomateAPI(
companyId: 'company',
serverUrl: 'your.connectwise.com',
clientId: '',
// One of the following: integrator username and password or username, password and two-factor code
// integrator username/password:
username: '',
password: '',
// also pass in two factor passcode if not using an integrator account
// this is useful for command line utilities
twoFactorPasscode: '',
timeout: 20000, // optional, request connection timeout in ms, defaults to 20000
retry: false, // optional, defaults to false
retryOptions: // optional, override retry behavior, defaults as shown
retries: 4, // maximum number of retries
minTimeout: 50, // number of ms to wait between retries
maxTimeout: 20000, // maximum number of ms between retries
randomize: true, // randomize delay between retries on timeouts
>,
debug: false, // optional, enable debug logging
logger: (level, text, meta) => // optional, pass in logging function
>);
cwa.ComputersAPI.getComputerList()
.then((computers) =>
//do something with results
>)
.catch((error) =>
//handle errors
>);
Use the pagination function to automatically fetch all records in order that match the request.
Note: the last argument to the pagination function must be an object, or an error will be thrown.
const cwa = new ManageAPI()
const cwm = new AutomateAPI()
// use the instantiated ManageAPI or AutomateAPI
cwm.paginate(
cwm.ServiceAPI.getServiceTickets, // pass in the api function to be paginated
startPage: 10, pageSize: 500>, // pagination options, defaults to startPage 1, pageSize 1000
<> // additional arguments to the api function as needed
)
.then(results => /*. */ >)
.catch(error => /*. */ >)
You can also manually access the API without typings:
const ManageAPI, AutomateAPI > = require('connectwise-rest');
const cwm = new ManageAPI(CWMOptions);
const cwa = new AutomateAPI(CWAOptions);
// use cwa.request or cwm.request
cwm.request(
path: '/path/to/api',
method: 'POST',
params:
'queryParam1': 'val1',
'queryParam2': 'val2'
>,
data:
'dataValue': 'val1',
>)
.then((result) =>
//do something with results
>)
.catch((error) =>
//handle errors
>);
To access cloud-hosted ConnectWise, use the companyUrl of api-na.myconnectwise.net and override the default entryPoint .
Region | URL |
---|---|
North America | api-na.myconnectwise.net |
Europe | api-eu.myconnectwise.net |
Australia | api-aus.myconnectwise.net |
Demo | api-staging.connectwisedev.com |
options =
companyId: 'company',
companyUrl: 'api-na.myconnectwise.net',
entryPoint: 'v2022.1', // change to the current hosted version
publicKey: '',
privateKey: ''
>
See the sample project here. This is a simple node.js express based API.
Get ticket 1234 and print ticket number, summary and status.
cwm.ServiceAPI.getServiceTicketsById(1234)
.then((ticket) => console.log(ticket.id, ticket.summary, ticket.status.name); >)
.catch((err) => console.log(err); >);
Create new ticket on service board, then print the returned ticket number, or any errors
cwm.ServiceAPI.postServiceTickets(
summary: "This is the summary",
board:
name: "Service Board"
>,
company:
identifier: "ABC" //company ID
>,
initialDescription: "ticket description",
recordType: "ServiceTicket"
//can also pass in any other Ticket object settings as needed
>)
.then((ticket) => console.log(ticket.id); >)
.catch((err) => console.log(err); >);
Change the status of a ticket
cwa.ServiceAPI.patchServiceTicketsById(1234, [
op: 'replace',
path: 'status',
value: id: 123> //id of the status to change to, find with boards.getBoards and status.getStatuses
>,
//second or more operations
>])
.then((res) =>
//do something with returned ticket
>)
.catch((err) =>
//do something with errors
>);
Valid example conditions string:
const conditions = '(contact/name like "Fred%" and closedFlag = false) and dateEntered > [2015-12-23T05:53:27Z] or summary contains "test" AND summary != "Some Summary"'
Error message returned from server when invalid conditions are passed in:
Expected a boolean value, not a numeric. String values should be enclosed with double or single quotes; DateTime values should be enclosed in square brackets; numbers should consist of only digits, and optionally a decimal point and a negation sign; boolean values should be indicated with the keywords true or false; null values should be indicated by the keyword null.
This library includes an express style callback middleware that will parse and verify the payload signature.
const utils> = require('connectwise-rest')
const CWCallback = utils.Callback;
router.post('/your/callback', CWCallback.middleware((err, req, res, verified, payload) =>
if (err)
//handle error, parsing, malformed object, etc
res.status(500).end();
> else if (!verified)
// send 403 on verification failure,
// or handle some other way
res.status(403).end();
> else
res.status(200).end()
>
const action, id> = req.query;
// do something with the payload
>));