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 Python. We'll cover everything from authentication to handling webhooks, so buckle up!
Before we jump in, make sure you've got:
requests
library installed (pip install requests
)If you're all set, let's get cracking!
First things first, let's get you authenticated:
headers = { 'Authorization': 'Basic ' + base64.b64encode(f'{company_id}+{public_key}:{private_key}'.encode()).decode(), 'clientId': 'your_client_id', 'Content-Type': 'application/json' }
Now that you're authenticated, let's make some requests:
import requests base_url = 'https://api-na.myconnectwise.net/v4_6_release/apis/3.0' # GET request response = requests.get(f'{base_url}/service/tickets', headers=headers) # POST request new_ticket = {'summary': 'Test ticket', 'board': {'id': 1}} response = requests.post(f'{base_url}/service/tickets', headers=headers, json=new_ticket)
Pro tip: Don't forget to handle pagination for GET requests that return multiple items!
Always expect the unexpected. Wrap your API calls in try-except blocks:
try: response = requests.get(f'{base_url}/service/tickets', headers=headers) response.raise_for_status() except requests.exceptions.RequestException as e: print(f"Oops! Something went wrong: {e}")
ConnectWise loves JSON. Here's how to parse it:
data = response.json() for ticket in data: print(f"Ticket ID: {ticket['id']}, Summary: {ticket['summary']}")
Let's put it all together with some core functions:
def get_companies(): response = requests.get(f'{base_url}/company/companies', headers=headers) return response.json() def create_ticket(summary, board_id): ticket_data = {'summary': summary, 'board': {'id': board_id}} response = requests.post(f'{base_url}/service/tickets', headers=headers, json=ticket_data) return response.json() def update_time_entry(time_entry_id, notes): update_data = {'notes': notes} response = requests.patch(f'{base_url}/time/entries/{time_entry_id}', headers=headers, json=update_data) return response.json()
Want to get real-time updates? Set up a webhook listener:
from flask import Flask, request app = Flask(__name__) @app.route('/webhook', methods=['POST']) def handle_webhook(): payload = request.json # Process the webhook payload print(f"Received webhook: {payload}") return '', 200 if __name__ == '__main__': app.run(port=5000)
Remember to:
Always test your functions:
def test_get_companies(): companies = get_companies() assert len(companies) > 0, "No companies returned" # Run your tests test_get_companies()
And don't forget to log, log, log!
Congratulations! You've just built a ConnectWise Manage API integration in Python. From here, you can expand on this foundation to create more complex integrations. The sky's the limit!
Now go forth and integrate! And remember, if you get stuck, the ConnectWise community is always here to help. Happy coding!