Apple Business Manager (ABM) and Apple School Manager (ASM) have an API for device management tasks. AppleBusinessANDSchoolManagerAPI is my set of Python scripts for it. They export device inventory, look up AppleCare coverage, list MDM servers and assign or unassign devices, all from Terminal and with CSV output.
What the scripts do
| Script | What it does |
|---|---|
AxM_OAuth.py |
Shared helper. Builds the client assertion, gets an access token from Apple, then encrypts and caches it |
AxM_OrgDevices_To_CSV.py |
Downloads all organisation devices to a CSV |
AxM_GetDeviceInfo_FromList.py |
Gets device details for each serial number in serialnumbers.txt |
AxM_GetAppleCareCoverage_FromList.py |
Gets AppleCare coverage for the listed devices |
AxM_MdmServers_To_CSV.py |
Lists all MDM servers (device management services) |
AxM_GetAssignedServer_FromList.py |
Gets the assigned MDM server for each listed device |
AxM_MdmServerDevices_To_CSV.py |
Exports the device IDs linked to one MDM server |
AxM_AssignUnassign_MdmServers.py |
Assigns or unassigns the listed devices and downloads the activity log |
The AppleCare lookup handles Limited Warranty, AppleCare+ and AppleCare for Business Essentials.
Authentication
The scripts follow Apple’s guide, Implementing OAuth for the Apple School Manager and Apple Business API. In short:
- You create an API account in ABM or ASM and download its EC private key.
- A client assertion (a JWT signed with ES256) is built from your client ID and key ID. Apple allows it to be valid for up to 180 days.
- The assertion is exchanged for an access token with the scope
business.apiorschool.api. Apple’s guide says the access token expires after one hour.
You do not run any of this by hand. Every script imports AxM_OAuth.py, which generates the assertion,
requests the token, encrypts it with Fernet, caches it and reuses it until it expires. A 401 response
makes it generate a new token once.
Setup
You need Python 3.7 or later (3.10 or later is recommended) and these packages:
pip3 install requests authlib pycryptodome cryptography python-dotenv
Create AxM_Variables.env next to the scripts:
APPLE_CLIENT_ID=BUSINESSAPI.xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
APPLE_KEY_ID=YOUR_KEY_ID
APPLE_SCOPE=business.api
APPLE_PRIVATE_KEY_PATH=private-key.pem
AXM_FERNET_KEY=YOUR_FERNET_KEY_HERE
Use school.api as the scope for Apple School Manager. Generate the Fernet key, which encrypts the cached
token, with:
python3 -c "from cryptography.fernet import Fernet; print(Fernet.generate_key().decode())"
Put your private key in the same folder, and for the scripts that work from a list, create
serialnumbers.txt with one serial number per line.
Examples
AppleCare coverage for a list of devices:
python3 AxM_GetAppleCareCoverage_FromList.py
This writes appleCareCoverage_details.csv and prints a summary of devices with and without coverage.
Assigning devices to an MDM server: set the mode and server at the top of
AxM_AssignUnassign_MdmServers.py, then run it.
MODE = "ASSIGN" # or "UNASSIGN"
MDM_SERVER_ID = "YOUR_MDM_SERVER_ID"
The script creates an orgDeviceActivity in Apple (ASSIGN_DEVICES or UNASSIGN_DEVICES), waits 30
seconds, checks the activity status, and downloads the result CSV if one is available. You can get the server
ID from the output of AxM_MdmServers_To_CSV.py.
Error handling
- 401 Unauthorized: the cached token is thrown away and regenerated once. If it still fails, check the
values in
AxM_Variables.env. - 429 Too Many Requests: the script waits for the time in the
Retry-Afterheader, or 60 seconds, and retries once. - 404 Not Found: the device is not in your ABM or ASM organisation.
Access tokens are never printed.
Before you use it
Test the scripts in a safe environment first, check the CSV output, and be especially careful with the assign and unassign script, because it changes which MDM server your devices belong to.
If you want AppleCare coverage written into Jamf Pro instead of a CSV, that is what AxM Jamf Sync does.