Android Device API
HeadSpin provides a convenient REST API interface for the Android Debug Bridge (ADB) to perform actions on your Android devices in HeadSpin from your client machine or CI/CD server. ADB is a CLI tool created by Google for interacting and managing Android devices. HeadSpin allows you to run ADB commands through an API interface so you can easily target and perform actions on your Android devices.
The prerequisite to using the HeadSpin APIs is an API Token. You can create an API token from your Settings.
Base URL
All URLs referenced in the API documentation have the following base URL structure:
Available parameters:
- timeout: wait for devices until they come back online or timeout is reached (unit: second / default: 0.0)
Example
Where your API token is passed in in the request header as the Authorization Bearer. Here's an example of this expressed in Python, which you can use as a reference to adapt to your language of choice:
To ensure data privacy, our REST APIs are served over HTTPS. For On-Premise Deployments your Base URL may vary based on your environment. Please reach out to your HeadSpin contact for your exact Base URL details.
Note: The examples in this guide will be expressed as cURL commands to make it easy to follow cross-platform and independent of programming language. For cURL commands including an API token, we use the -H option as such:
API Guide
This section will introduce how to use the HeadSpin Android Device API through several examples:
- Take a screenshot
- Retrieve the Android Logcat
- Install an app
- Pull a file from an Android Device
- Send ADB Commands through the API
The examples cover popular use cases and explain how to use the APIs. The subsequent section will be a full reference of available Android APIs.
Take a screenshot
You can take a screenshot of an Android device by sending a GET request to the Android Screenshot API. You can also specify an output path where the image can be stored on your client machine.
For example:
Retrieve the Android Logcat
Logcat is an ADB command for retrieving the log file data from an Android device. It allows you to view & filter through your app’s logs and system events, such as Garbage collection or Radio states. Since there can be numerous log events on a given device, HeadSpin provides a convenience query string parameter to retrieve a certain number of lines.
For example:
Install an app
API
Using our App Management API, you can install an app on a device using either an app ID or a designated metadata selector. Metadata selectors are descriptors of unique app builds compiled by the HeadSpin system and the user, which can be used to query and filter builds for various actions. For more information and additional API command sets, including how to use metadata selectors for installation, please review the App Management API documentation. The App Management API is consistent across all platforms, so this command will work for XAPK as well as APK and IPA files.
For example, installing a previously uploaded app by app ID:
Pull a File From An Android Device
To pull a file from an Android Device you will specify the file path to the resource in the request.
For example:
Send ADB Commands through the API
You can send ADB commands directly through an Android Device by passing in the ADB command or shell script in the POST body of the request.
For example:
API Reference
List apps installed on a device
Install an app on a device by app ID
Uninstall an app from a device by app ID
Save a screenshot to the cloud
Using Pintap to solve PIN entry screen
List all Android devices
Example
Response
JSON object, each key is the address of a device. The address is in the form of <code class="dcode">{the iOS device's UDID (Unique Device Identifier)}@{host}</code>.
Upload a file to a device
Request Body
The request's body should include the binary of your file. To include the binary in your <code class="dcode">curl</code> request, use the <code class="dcode">--data-binary <file_name></code> flag, for example <code class="dcode">--data-binary image.jpg</code> or <code class="dcode">--data-binary "@/Users/headspin/Downloads/image.jpg"</code>
Example
Response
A JSON object.
- <code class="dcode">{ "_headspin_progress": ".", "status": 0, "returncode": 0, "stdout": "[100%] <remote_path_to_file>: 1 file pushed. 0.0 MB/s (11 bytes in 0.020s)\n", "summary": "ok"}</code> if the push is successful.
- <code class="dcode">{ "_headspin_progress": ".", "status": 0, "returncode": 1, "stdout": "[100%] /storage/image.jpg\nadb: error: failed to copy 'image.jpg' to '/storage/image.jpg': remote couldn't create file: Permission denied\nimage.jpg: 0 files pushed. 0.0 MB/s (11 bytes in 0.006s)\n", "summary": "ok"}</code> if the push is not successful.
Download a file from a device
Example
Response
- The response is the binary of the file. To save the response as a file on your local system, use the <code class="dcode">-o <filename></code> flag.
Execute a script on a device
Request Body
The request's body should include your command or the binary of your script.
- To include the shell command, use the <code class="dcode">-d <command></code> flag, for example <code class="dcode">-d "pm list packages"</code>.
- To include a file to your script, use the <code class="dcode">--data-binary "@<path_to_your_script>"</code>, for example <code class="dcode">--data-binary "@/Users/username/script.sh"</code>.
Example
To send a single command:
To send a file to your script:
Response
A JSON object, with <code class="dcode">status</code>, <code class="dcode">returncode</code>, <code class="dcode">stdout</code> and <code class="dcode">summary</code>. For example:
Install an app on a device by app ID
Note
This command is a part of our App Management API. For more information and additional API command sets, please navigate to the link above. App Management API is consistent across all platforms, so this command will work for XAPK as well as APK and IPA files.
Optional Parameters
- <code class="dcode">/v1/app/{app_id}/install/{device_id}?sign=false</code>: disable automated signing. iOS only, see idevice-api for signing documentation.
Example
Install a previously uploaded app:
List apps installed on a device
Example
Optional Parameters
- <code class="dcode">format</code> can be specified to set the response format. accepts <code class="dcode">text</code>, <code class="dcode">json</code>, or <code class="dcode">xml</code>. defaults to <code class="dcode">text</code>.
- <code class="dcode">owner</code> can be specified to filter which types of packages to return. accepts <code class="dcode">user</code>, <code class="dcode">system</code>, or <code class="dcode">all</code>. defaults to <code class="dcode">user</code>.
Example
Response
The response is a plaintext document that looks like the following:
If the optional ?format=json parameter is used, the response is a JSON object that looks like the following:
If the optional ?format=xml parameter is used, the response is instead an XML object that looks like the following:
Uninstall an app from a device by app ID
Note
This command is a part of our App Management API. For more information and additional API command sets, please navigate to the link above. App Management API is consistent across all platforms, so this command will work for XAPK as well as APK and IPA files.
Example
Upgrade an app
Request Body
The request's body should include the binary of your app. To include the binary in your curl request, use the --data-binary flag, for example --data-binary "@App.apk" as shown below.
Example
Uninstall an XAPK using its xapk_id
Example
Response
A JSON object:
- <code class="dcode">{ "_headspin_progress": ".", "status": 0, "returncode": 0, "stdout": "Success\n\n", "summary": "ok"}</code> if uninstall is successful.
- <code class="dcode">{ "_headspin_progress": ".", "status": 0, "returncode": 0, "stdout": <error>, "summary": "failed to run"}</code> if uninstall is not successful.
Lock a device
● Optional Parameters
<code class="dcode">/v0/adb/{device_id}/lock/?timeout={timeout seconds}</code>: If the locking attempt fails, the server will keep retrying until the <code class="dcode">timeout seconds</code> have elapsed. If there's no <code class="dcode">?timeout</code> parameter, or <code class="dcode">timeout seconds</code> is <code class="dcode">0</code>, then this returns immediately after the first attempt to lock <code class="dcode">/v0/adb/{device_id}/lock/?idleTimeout={timeout seconds}</code>: Unlock automatically if the device is inactive for <code class="dcode">timeout seconds</code>.
Example
Response
JSON object:
- <code class="dcode">{"status": 0, "message": "<device_address> locked."}</code> if the lock attempt was successful.
- <code class="dcode">{"status": 1, "message": "Did not lock."}</code> if the lock attempt was unsuccessful. The device might be locked by another user.
Unlock a device
Unlocks the device in the platform, if the lock is owned by the requesting user.
Example
Response
JSON object:
- <code class="dcode">{"status": 0, "message": "<device_address> unlocked."}</code> if the unlock attempt was successful.
- <code class="dcode">{"status": 1, "message": "Did not unlock."}</code> if the unlock attempt was unsuccessful. The device might not be locked by the requesting user.
Reboot a device
Example
Response
A JSON object.
- <code class="dcode">{ "_headspin_progress": ".", "status": 0, "returncode": 0, "stdout": "", "summary": "ok"}</code> if the device was restarted successfully.
Retrieve a device's logcat
Stream the content of the connected device's system log.
Optional Parameters
- <code class="dcode">/v0/adb/{device_id}/logcat?n={lines back}</code>: Instead of streaming, retrieve only the last n lines from the device's syslog. For example, <code class="dcode">/syslog?n=30</code> retrieves the last 30 lines.
Example
Response
The device's syslog is sent to the requesting user in <code class="dcode">text/plain</code>. The content is streamed and sent in chunks.
Take a device's screenshot
Stream the content of the connected device's system log.
Optional Parameters
- To save the captured screenshot as an image, append <code class="dcode">> {local path}/{filename}.png</code> to the end of the command.
Example
Response
An <code class="dcode">image/png</code> object.
Save a screenshot to the cloud
Similar to the <code class="dcode">.../screenshot</code> route explained above, the <code class="dcode">.../screenshot_url</code> route will take a screenshot in PNG format, but additionally the screenshot will be uploaded to the HeadSpin services. The result of the <code class="dcode">.../screenshot_url</code> route is another URL that can be used to retrieve the uploaded screenshot. Note that the URL returned from this API is only valid for 24 hours.
Example
Response
A URL
Using Pintap to solve PIN entry screen
Uses Pintap to enter a code to solve the PIN entry screen. This performs the same actions as Pintap in Remote Control UI.
Optional Parameters
The PIN code can be specified by including a JSON object as the request body.
Example
Response
The server will reply after Pintap has finished execution, which may take several seconds, or when there is an issue.
- <code class="dcode">HTTP 200</code> when Pintap finishes execution.
- <code class="dcode">HTTP 400</code> if there was a problem with the request.
- <code class="dcode">HTTP 404</code> if no eligible device matches the requested device address.
- <code class="dcode">HTTP 500</code> if Pintap encounters an error.
Multiple Android Device Access
HeadSpin also provides a set of APIs that allow you to run ADB commands across multiple devices in parallel. These APIs accept an address pattern called a selector. The selector syntax runs on each device object and matches the fields in the device object.
The POST requests return 200 and a consistent JSON shape <code class="dcode">{devices}</code>, where the keys are the same as <code class="dcode">/adb/devices</code>, and each device has <code class="dcode">{returncode, summary, stdout}</code> . The output is frames per line as
For example:
The emtpy selector matches all devices.