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 PHP. ConnectWise Manage's API is a powerful tool that'll let you automate tasks, sync data, and extend functionality. Let's get started!
Before we jump in, make sure you've got:
Got those? Great! Let's move on.
First things first, let's get our environment ready:
Install Guzzle (trust me, it'll make HTTP requests a breeze):
composer require guzzlehttp/guzzle
Set up your API authentication. Create a config.php
file:
<?php define('CW_COMPANY_ID', 'your_company_id'); define('CW_PUBLIC_KEY', 'your_public_key'); define('CW_PRIVATE_KEY', 'your_private_key'); define('CW_API_URL', 'https://api-na.myconnectwise.net/v4_6_release/apis/3.0/');
Let's start with a simple GET request to fetch companies:
<?php require 'vendor/autoload.php'; require 'config.php'; use GuzzleHttp\Client; $client = new Client([ 'base_uri' => CW_API_URL, 'headers' => [ 'Authorization' => 'Basic ' . base64_encode(CW_COMPANY_ID . '+' . CW_PUBLIC_KEY . ':' . CW_PRIVATE_KEY), 'Content-Type' => 'application/json', ], ]); $response = $client->get('company/companies'); $companies = json_decode($response->getBody(), true); print_r($companies);
Run this, and you should see a list of companies. Exciting, right?
Now that we've got our feet wet, let's dive into CRUD operations:
$newCompany = [ 'name' => 'Acme Corp', 'identifier' => 'AcmeCorp', ]; $response = $client->post('company/companies', ['json' => $newCompany]); $createdCompany = json_decode($response->getBody(), true);
We've already done this, but here's a more specific example:
$response = $client->get('company/companies/123'); // Replace 123 with an actual ID $company = json_decode($response->getBody(), true);
$updatedData = ['name' => 'Acme Corporation']; $response = $client->patch('company/companies/123', ['json' => $updatedData]);
$response = $client->delete('company/companies/123');
ConnectWise Manage has various modules. Here's how to work with a few:
// Create a ticket $newTicket = [ 'summary' => 'New feature request', 'board' => ['id' => 1], 'company' => ['id' => 123], ]; $response = $client->post('service/tickets', ['json' => $newTicket]); // Get all tickets $response = $client->get('service/tickets'); $tickets = json_decode($response->getBody(), true);
// Create a time entry $newTimeEntry = [ 'chargeToId' => 123, // Ticket ID 'chargeToType' => 'ServiceTicket', 'timeStart' => '2023-05-01T09:00:00Z', 'timeEnd' => '2023-05-01T10:00:00Z', 'notes' => 'Worked on feature implementation', ]; $response = $client->post('time/entries', ['json' => $newTimeEntry]);
Always wrap your API calls in try-catch blocks:
try { $response = $client->get('company/companies'); } catch (\GuzzleHttp\Exception\RequestException $e) { if ($e->hasResponse()) { $errorBody = json_decode($e->getResponse()->getBody(), true); echo "API Error: " . $errorBody['message']; } else { echo "Network Error: " . $e->getMessage(); } }
For rate limits, implement exponential backoff:
function makeRequestWithRetry($client, $method, $endpoint, $options = [], $maxRetries = 3) { for ($i = 0; $i < $maxRetries; $i++) { try { return $client->$method($endpoint, $options); } catch (\GuzzleHttp\Exception\ClientException $e) { if ($e->getResponse()->getStatusCode() == 429) { $waitTime = pow(2, $i); sleep($waitTime); } else { throw $e; } } } throw new \Exception("Max retries reached"); }
ConnectWise uses page and pageSize parameters:
$response = $client->get('company/companies', [ 'query' => ['page' => 1, 'pageSize' => 50] ]);
Use the conditions parameter:
$response = $client->get('company/companies', [ 'query' => ['conditions' => 'name like "Acme%"'] ]);
To receive updates, set up a webhook endpoint in your application and configure it in ConnectWise Manage.
Always write unit tests for your integration. Here's a simple example using PHPUnit:
use PHPUnit\Framework\TestCase; class ConnectWiseIntegrationTest extends TestCase { public function testGetCompanies() { // Your test code here } }
Congratulations! You've just built a ConnectWise Manage API integration in PHP. Remember, this is just the beginning. There's so much more you can do with this powerful API. Keep exploring, keep coding, and most importantly, have fun!
Now go forth and integrate! You've got this! 🚀