Go beyond the console with this API command to create automatic app updates for your blueprint. In this guide, we'll walk you through a Python script to upload Enterprise apps to a blueprint and converge that blueprint to Android devices.
This guide might work for you if:
- You have an understanding of APIs and Python.
- Have an Esper API key.
- Use Enterprise apps for Android devices.
- You're on the Blueprints experience.
In this article:
Upload an Enterprise App to a Blueprint Python Example
Normally, enterprise apps must be uploaded to the console or blueprint. However, you can write a script to upload new app versions automatically with the API.
import requests
# Set your API Key
bearer_token = "API_KEY"
#Step 1: Upload the app
def upload_app(tenant_name, enterprise_id, file_path):
url = f"https://{tenant_name}-api.esper.cloud/api/enterprise/{enterprise_id}/application/upload/"
files = {'app_file': open(file_path, 'rb')}
headers = {'Authorization': f'Bearer {bearer_token}'}
try:
response = requests.post(url, files=files, headers=headers)
response.raise_for_status()
return response.json()
except requests.exceptions.HTTPError as error:
if 'App Upload failed as this version already exists' in response.text:
raise Exception("App version already exists in Esper.")
else:
raise error
except requests.exceptions.RequestException as err:
raise Exception(f"Error during app upload: {err}")
# Step 2: Get the target Blueprint
def get_blueprints(tenant_name):
url = f"https://{tenant_name}-api.esper.cloud/api/v2/blueprints/"
headers = {'Authorization': f'Bearer {bearer_token}'}
response = requests.get(url, headers=headers)
return response.json()
# Step 3: Parse the results by name
def parse_blueprint_by_name(blueprints, target_name):
for blueprint in blueprints['content']['results']:
if blueprint['name'] == target_name:
blueprint_id = blueprint['id']
android_settings = blueprint['settings']['android']
return blueprint_id, android_settings
return None, None
# Step 4: Create a new body
def create_new_body(blueprint_id, android_settings):
url = f"https://{tenant_name}-api.esper.cloud/api/v2/blueprints/{blueprint_id}/"
new_body = {
"comments": "",
"publish": True, # Set to True to apply changes immediately, False otherwise
"settings": {
"android": android_settings
}
}
return url, new_body
# Step 5: Add the new app version
def add_app_version(android_settings, app_info):
apps_section = android_settings.get('apps', [])
new_app = {
"app_id": app_info['application']['id'],
"app_version_id": app_info['application']['versions'][0]['id'],
"package_name": app_info['application']['package_name'],
"installation_rule": "DURING PROVISIONING",
"state": "SHOW",
"managed_config": {},
"type": "ENTERPRISE"
}
apps_section.append(new_app)
android_settings['apps'] = apps_section
return android_settings
# Step 6: Apply the changes to your blueprint
def apply_changes(url, new_body):
headers = {'Authorization': f'Bearer {bearer_token}', 'Content-Type': 'application/json'}
response = requests.put(url, json=new_body, headers=headers)
return response.json()
# Step 7: Converge the blueprint to a device
def converge_blueprint(tenant_name, device_id):
url = f"https://{tenant_name}-api.esper.cloud/api/commands/v0/commands/"
data = {
"command_type": "DEVICE",
"devices": [device_id],
"device_type": "all",
"command": "CONVERGE"
}
headers = {'Authorization': f'Bearer {bearer_token}', 'Content-Type': 'application/json'}
response = requests.post(url, json=data, headers=headers)
return response.json()
#Required Fields
target_blueprint_name = ""
tenant_name = ""
enterprise_id = ""
#apk path here
file_path = ""
device_id = ""
# Get blueprints and parse by name
blueprints = get_blueprints(tenant_name)
blueprint_id, android_settings = parse_blueprint_by_name(blueprints, target_blueprint_name)
if blueprint_id is not None:
url, new_body = create_new_body(blueprint_id, android_settings)
app_info = upload_app(tenant_name, enterprise_id, file_path)
android_settings = add_app_version(android_settings, app_info)
apply_changes(url, new_body)
converge_blueprint(tenant_name, device_id)
else:
print(f"Blueprint with name '{target_blueprint_name}' not found.")
Step Breakdown
Step 1: Upload the app version
Once you have a new app version, you’ll want to upload that version to the Epser cloud.
URI
POST https://{tenant_name}-api.esper.cloud/api/enterprise/{enterprise_id}/application/upload/
Body
Form-data
See how to upload files in Postman.
- Key: app_file
- Value: file
Send the request and save the response.
{
"application": {
"id": "",
"versions": [
{
"id": "",
"version_code":”",
"build_number": "",
"hash_string": "",
"download_url":”",
"icon_url": ,
"min_sdk_version": "",
"target_sdk_version": ”",
"release_name": "",
"release_comments":
}
],
"application_name": "",
"package_name": "",
"developer":,
"category":,
"content_rating": "",
"compatibility":,
"created_on": "",
"updated_on": "",
"is_active":,
"is_hidden":,
"auto_update_app":,
"enterprise": ""
}
}
Step 2: Get blueprints
Next, get a list of blueprints and their details.
URI
GET https://{tenant_name}-api.esper.cloud/api/v2/blueprints/
Step 3: Retrieve the blueprint ID
Using the response from the blueprint API, parse the following fields:
- “id”
- “settings” : {“android”:{{}}}
Step 4: Build the new body
Create the following URI using the “id” field from Step 3.
POST https://{tenant_name}-api.esper.cloud/api/v2/blueprints/{id}/
Create the following body using the "settings": { "android": {{... }}} field from Step 3.
{
"comments": "",
"publish": true/false,
"settings": {
"android": {
...
}
}
}
Step 5: Add the new app version
Modify the apps section located under "settings": { "android": {{“apps”:[{...}] }}} with your new app version. Use the app_id, app_version_id, and “package_name” fields from Step 1.
Sample body
"apps": [
{
"app_id": "",
"app_version_id": "",
"package_name": "",
"installation_rule": "DURING PROVISIONING",
"state": "SHOW",
"managed_config": {},
"type": "ENTERPRISE"
}
]
Step 6: Save changes to the blueprint
Save your changes with the following PUT command using the new JSON body.
- Comments: Optional. String. Write a comment for this blueprint version.
- Publish: Required. Boolean. True: publish the blueprint. False: blueprint is not published but instead remains as a draft. Only published blueprints can be converged to devices.
PUT https://{tenant_name}-api.esper.cloud/api/v2/blueprints/{id}/
{
"comments": "",
"publish": true,
"settings": {
"android": {
…
}
}
}
Step 7: Converge the blueprint
Converge the blueprint to the devices.
POST https://{tenant_name}-api.esper.cloud/api/commands/v0/commands/
Converge on devices
{
"command_type": "DEVICE",
"devices": [
"{device_id}"
],
"device_type": "all",
"command": "CONVERGE"
}
Converge on groups
{
"command_type": "GROUP",
"groups": [
“{group_id}”
],
"device_type": "all",
"command": "CONVERGE"
}
Automate app version uploads and speed up your app deployment process. Would you rather schedule an update? See our other Scheduling API Commands guide.