Classic API Sample Code
Below are some code samples for common use cases to help get you started integrating with Jamf Pro.
Replace YOUR_JAMF_PRO_URL with the correct URL of your Jamf Pro instance and YOUR_CREDENTIALS with the Base64 encoded username and password of an appropriate user account. The credentials should be colon delimited (e.g. username:password).
Written inBash
printf "username:password" | iconv -t ISO-8859-1 | base64 -i -
Python 3.0 or later is required for the Python code samples included below. For use with earlier versions of Python, replace the http.client
library with the earlier version httplib
.
Get Inventory Information from a Single Computer
The following code samples demonstrate how to retrieve inventory information on a single computer using the ID of the computer.
CurlRequest
curl -X GET \
https://YOUR_JAMF_PRO_URL/JSSResource/computers/id/8 \
--header 'authorization: Basic YOUR_CREDENTIALS'
Written inPython
import http.client
conn = http.client.HTTPSConnection("YOUR_JAMF_PRO_URL")
headers = {
'authorization': "Basic YOUR_CREDENTIALS"
}
conn.request("GET", "/JSSResource/computers/id/8", headers=headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
Deploy (Scope) an App to a Mobile Device
The following code samples demonstrate how to deploy (scope) a mobile device application to a single mobile device using the ID of the mobile device.
CurlRequest
curl -X PUT \
https://YOUR_JAMF_PRO_URL/JSSResource/mobiledeviceapplications/id/7 \
--header 'content-type: application/xml' \
--header 'authorization: Basic YOUR_CREDENTIALS' \
--data '<mobile_device_application>
<scope>
<mobile_devices>
<mobile_device>
<id>2</id>
</mobile_device>
</mobile_devices>
</scope>
</mobile_device_application>'
Written inPython
import http.client
conn = http.client.HTTPSConnection("YOUR_JAMF_PRO_URL")
payload = "<mobile_device_application><scope><mobile_devices><mobile_device><id>2</id></mobile_device></mobile_devices></scope></mobile_device_application>"
headers = {
'content-type': "application/xml",
'authorization': "Basic YOUR_CREDENTIALS"
}
conn.request("PUT", "/JSSResource/mobiledeviceapplications/id/7", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
Deploy (Scope) an App to a Computer
The following code samples demonstrate how to deploy (scope) an app to an individual computer by adding the computer to the scope of an existing policy using the ID of the computer.
CurlRequest
curl -X PUT \
https://YOUR_JAMF_PRO_URL/JSSResource/policies/id/15 \
--header 'authorization: Basic YOUR_CREDENTIALS' \
--header 'content-type: application/xml' \
--data '<policy>
<scope>
<computers>
<computer>
<id>9</id>
</computer>
</computers>
</scope>
</policy>'
Written inPython
import http.client
conn = http.client.HTTPSConnection("YOUR_JAMF_PRO_URL")
payload = "<policy><scope><computers><computer><id>9</id></computer></computers></scope></policy>"
headers = {
'content-type': "application/xml",
'authorization': "Basic YOUR_CREDENTIALS"
}
conn.request("PUT", "/JSSResource/policies/id/15", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
Issue Remote Command to a Computer
The following code samples demonstrate how to issue a device command to a single computer.
CurlRequest
curl -X POST \
https://YOUR_JAMF_PRO_URL/JSSResource/computercommands/command/DeviceLock \
--header 'authorization: Basic YOUR_CREDENTIALS' \
--header 'content-type: application/xml' \
--data '<computer_command>
<general>
<command>DeviceLock</command>
<passcode>123456</passcode>
</general>
<computers>
<computer>
<id>8</id>
</computer>
</computers>
</computer_command>'
Written inPython
import http.client
conn = http.client.HTTPSConnection("YOUR_JAMF_PRO_URL")
payload = "<computer_command><general><command>DeviceLock</command><passcode>123456</passcode></general><computers><computer><id>8</id></computer></computers></computer_command>"
headers = {
'content-type': "application/xml",
'authorization': "Basic YOUR_CREDENTIALS"
}
conn.request("POST", "/JSSResource/computercommands/command/DeviceLock", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
Issue Remote Command to a Mobile Device
The following code samples demonstrate how to issue a single command to a single mobile device.
CurlRequest
curl -X POST \
https://YOUR_JAMF_PRO_URL/JSSResource/mobiledevicecommands/command \
--header 'authorization: Basic YOUR_CREDENTIALS' \
--header 'content-type: application/xml' \
--data '<mobile_device_command>
<general>
<command>DeviceLock</command>
</general>
<mobile_devices>
<mobile_device>
<id>2</id>
</mobile_device>
</mobile_devices>
</mobile_device_command>'
Written inPython
import http.client
conn = http.client.HTTPSConnection("YOUR_JAMF_PRO_URL")
payload = "<mobile_device_command><general><command>DeviceLock</command></general><mobile_devices><mobile_device><id>2</id></mobile_device></mobile_devices></mobile_device_command>"
headers = {
'content-type': "application/xml",
'authorization': "Basic YOUR_CREDENTIALS"
}
conn.request("POST", "/JSSResource/mobiledevicecommands/command", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
Update the Value of a Computer Extension Attribute on a Single Computer
The following code samples demonstrate how to update the value of a computer extension attribute on a single computer.
CurlRequest
curl -X PUT \
https://YOUR_JAMF_PRO_URL/JSSResource/computers/id/8 \
--header 'authorization: Basic YOUR_CREDENTIALS' \
--header 'content-type: application/xml' \
--data '<computer>
<extension_attributes>
<extension_attribute>
<id>1</id>
<value>TEST2</value>
</extension_attribute>
</extension_attributes>
</computer>'
Written inPython
import http.client
conn = http.client.HTTPSConnection("YOUR_JAMF_PRO_URL")
payload = "<computer><extension_attributes><extension_attribute><id>1</id><value>TEST2</value></extension_attribute></extension_attributes></computer>"
headers = {
'content-type': "application/xml",
'authorization': "Basic YOUR_CREDENTIALS"
}
conn.request("PUT", "/JSSResource/computers/id/8", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))