Skip to content

Instantly share code, notes, and snippets.

@bersfo
Last active October 13, 2022 17:00
Show Gist options
  • Save bersfo/12efa79de156aa6f96d80ff14a060822 to your computer and use it in GitHub Desktop.
Save bersfo/12efa79de156aa6f96d80ff14a060822 to your computer and use it in GitHub Desktop.
How to create a Connectwise Manage API Token in Python and make your first request?

In ConnectWise Manage:

  1. Go to System => Members
  2. Go to the API Members tab.
  3. Create a new API member that gives the API access to the areas that you need it, such as corporate/territorial levels. Note the username, amd that you cannot specify a password. Complete all the mandatory fields (owner ID, system default, group, approvers, etc).
  4. When you have made this user go to the API keys tab. Enter a description for your new set of keys, and save it. When you do this you'll see the public and private keys once (and once only). Note them down.

Generate Authorisation Header in Python:

>>> import base64

>>> cwToken = base64.b64encode("a+b:c")

...where a is your ConnectWise company name (what you type in in the login box), b is your public key, and c is your private key. This will return something which will be what you use to access the system.

Do your first Connectwise Manage API request in Python:

>>> cwUrl = "https://<replace with your Connectwise Manage FQDN>/v4_6_release/apis/3.0/"

>>> cwHeaders = {"Authorization": "Basic " + cwToken, "Content-Type": "application/json"}

>>> r = requests.get(cwUrl + "company/companies?pageSize=1000&conditions=type/id==1", headers=cwHeaders)

Example function to get the first 1000 companies iin JSON format:

# get a list of all known companies and return as JSON
def cw_get_companies():
    try:
        r = requests.get(cwUrl + "company/companies?pageSize=1000&conditions=type/id==1", headers=cwHeaders)
        # request has been made
        r.raise_for_status()
    except:
        print(r.text)
        raise
    return r.json()

Reference

Many thanks to Daniel Dainty for his great answer on StackOverflow!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment