TeamViewer – API get list of old devices

Spread the love

If you are facing the limit of the maximum number of devices under your TeamViewer license, it is very helpful to get a list of all devices that have not been connected for a long time.

1. Prepare the API Token

  • Log in to the TeamViewer Management Console using your user account (note: you cannot use the organization admin account for this).
  • Click on your profile icon in the top right corner.
  • Select Edit Profile.
  • Under the Applications section, select Create Script Token.
  • The only permission you need is Computers – Read Records.
  • After creating the token, click on it and save your token.

2. Python Script

Replace TEAMVIEWER_API_TOKEN with your actual token. After you run the script, it will produce a list of devices, their IDs, and the last seen dates in the console.

import requests
from datetime import datetime, timedelta

# Replace with your token
TEAMVIEWER_API_TOKEN = '#################################'
BASE_URL = "https://webapi.teamviewer.com/api/v1"

HEADERS = {
    "Authorization": f"Bearer {TEAMVIEWER_API_TOKEN}"
}

def get_managed_devices():
    url = f"{BASE_URL}/devices?full_list=true"
    devices = []
    while url:
        response = requests.get(url, headers=HEADERS)
        if response.status_code != 200:
            raise Exception(f"Failed to fetch devices: {response.status_code} - {response.text}")
        data = response.json()
        #print(data)
        devices.extend(data.get("devices", []))
        url = data.get("next")  # Pagination
    return devices

def filter_offline_3_months(devices):
    three_months_ago = datetime.utcnow() - timedelta(days=90)
    old_offline = []
    for device in devices:
        last_seen_str = device.get("last_seen")
        if last_seen_str:
            last_seen = datetime.strptime(last_seen_str, "%Y-%m-%dT%H:%M:%SZ")
            if last_seen < three_months_ago:
                old_offline.append({
                    "alias": device.get("alias"),
                    "teamviewer_id": device.get("teamviewer_id"),
                    "last_seen": last_seen_str
                })
    return old_offline

if __name__ == "__main__":
    print("Fetching managed devices...")
    devices = get_managed_devices()
    print(f"Total devices fetched: {len(devices)}")

    print("Filtering devices offline for more than 3 months...")
    offline_devices = filter_offline_3_months(devices)

    print(f"\nDevices offline > 3 months: {len(offline_devices)}")
    for d in offline_devices:
        print(f"{d['alias']} (ID: {d['teamviewer_id']}) - Last seen: {d['last_seen']}")

Buy Me a Coffee