Create a Zendesk Support app in JavaScript to help your team troubleshoot customer device connectivity issues using the emnify REST API. This app enables your team to:
Only Administrators can create application tokens in the emnify Portal. See Roles and permissions for more information.
All the necessary code is provided, but understanding JavaScript helps you customize the app.
Safari doesn’t support mixed content and cannot run Zendesk apps locally.
Additionally, create a custom field for your SIM identifier in Zendesk Support as outlined in the next section.
This app relies on a unique SIM identifier to retrieve data from the emnify REST API. Create a custom field in Zendesk Support to store and manage this identifier. Your team should be familiar with this field and use it regularly. This identifier must be:
Devices are referred to as “endpoints” in the emnify REST API.
For example, you can use:
The emnify Portal doesn’t allows for duplicate device names.
If you choose this option, you need to ensure that the device name is unique and follows a consistent format.
For example, device-12345, D12345, 12345, etc.
This guide uses the ICCID without the final Luhn checksum digit.
Here are the steps to create a custom field in Zendesk Support:
Follow the Zendesk Help guide to add a new custom ticket field named ICCID. Select Number (without decimals) as the field type.
Add a Display name to clarify the field’s purpose for your team.
Optionally, add a Description for admins to provide more context. If the field is visible to customers, you can include a separate description for them.
Example description for ICCID:
Complete the configuration and save the custom field by following the Adding custom ticket fields to your tickets and forms Zendesk guide.
After saving, the field appears in the Admin Center under Objects and rules > Tickets > Fields.
Note down the Field ID for the custom field. You need this ID to fetch the ICCID value.
The Zendesk Apps Framework (ZAF) allows you to customize and extend your agent’s interface. For more information, see Apps in the Zendesk developer documentation.
Follow these steps to set up a new Zendesk app:
Create the app files. Use the following values when prompted:
After completing this step, the Zendesk Command Line Interface (ZCLI) generates starter files in the emnify-support-app folder.
Run the app locally to test it in your Zendesk Support account before deployment.
A Zendesk app runs inside an iframe.
Static and dynamic elements are defined in the iframe.html file located in emnify-support-app/assets/iframe.html.
Make sure this script tag is placed after the one for the ZAF SDK.
The body of iframe.html should now look as follows:
Add a footer with a Report bugs link:
This guide uses Bootstrap CSS for styling.
Refer to Changing the logo in the Zendesk developer documentation for details on replacing the default logo with your own.
The authentication token entered during app installation must be refreshed regularly to maintain functionality.
If the token becomes invalid, the app displays an error.
To resolve this, update the app settings in the Zendesk Admin Center and reenter the authToken.
This approach ensures that the authentication token isn’t exposed to the browser, which is especially important for organizations where access to the emnify Portal is restricted to specific users. However, this is a temporary solution designed for demonstration purposes and isn’t ideal for production use.
For a robust implementation, consider building a server-side Zendesk app that uses the application token to generate and refresh the auth_token dynamically.
If you want to read a guide for creating a server-side Zendesk apps and integrating with the emnify REST API, let the team know via Canny.
To access the emnify REST API, you need to authenticate your app.
The first step is to use your application token and the Retrieve Authentication Token call to generate a JSON Web Token (JWT) auth_token that authenticates further requests to the emnify REST API.
Follow Authenticate with application tokens to generate your auth_token.
This API path has a rate limit of 100 requests per IP in a 5-minute window. For more information, see Rate limits.
Next, define the app’s installation settings to use the auth_token:
Define settings using the parameters property in the manifest file.
Add the following code:
For other supported properties, see Setting properties in the Zendesk developer documentation.
The framework generates an HTML settings page based on the settings defined in the manifest file.
This is where an agent enters the authToken when installing the app or edit it later.
Then, you use the {{setting.authToken}} placeholder for the secure setting’s value in your app’s request call.
To help your team understand the condition of a device and SIM card, you can configure your app to fetch their current statuses from the emnify REST API and display them in the app.
This section includes:
You can use ZAF to access Zendesk resources from your app. To retrieve the ICCID from the custom field you created earlier, use Zendesk’s Ticket API.
Zendesk apps uses a JavaScript library called the ZAF software development kit (SDK) to interact with the Zendesk API.
The SDK includes a client that provides an interface between your app and Zendesk Support, enabling various methods to interact with the Apps framework.
Use the client to access the ticket’s custom fields and extract the ICCID.
Use Handlebars.js to display the fetched details in a structured format.
Import the Handlebars library in your app by adding the following script to iframe.html (before the script importing your main.js file):
If you reload the app, nothing happens because you haven’t inserted the template in the HTML yet.
Only one template can be rendered at a time.
Add the following function to your main.js file to render the template at runtime:
This function is generic and can be reused to render any Handlebars template in this guide.
Next, identify which device the SIM card is linked to in the emnify platform using the List Endpoints API call.
Filter results using the q query parameter in the <field>:<criteria> format with the ICCID value.
Pass the ICCID to the showDeviceDetails and concatenate it to the request URL.
Start by defining the function before renderTemplate.
It should accept the ICCID as an input parameter because the ICCID uniquely identifies the SIM card and is used in the API request.
Since this function makes a network request, it can fail for reasons like network issues or incorrect data.
Use a try-catch block to handle both success and failure scenarios.
Inside the try block, use the client.request method to call the emnify REST API.
Pass the ICCID as part of the query parameter to filter the results and include authentication headers.
q=iccid:${iccid} filters results to find a specific SIM.Authorization header with the JWT token.You need to mark secure as true to ensure sensitive information isn’t exposed in the browser.
For more information, see Zendesk’s secure setting property documentation.
The API response contains the details you need, such as the device name, ID, SIM status, etc. Extract these details from the response.
response[0]).Pass the deviceDetails object to a Handlebars template to render the information in the app.
Use the renderTemplate function you created earlier.
Putting it all together, the function looks like this:
Insert this Handlebars template in iframe.html after your details template:
The JavaScript logic for error handling is already included in the showDeviceDetails function.
You can format the date and time to display to be more human-readable.
Now that you have the device details and defined the deviceId, you can fetch the device’s connectivity status from emnify using the Endpoint Connectivity Status API call.
Before writing the JavaScript logic, you must define the Handlebars template in your HTML. This template renders the connectivity data dynamically at runtime.
Begin by defining the function in main.js.
It accepts deviceId as a parameter, which is the unique identifier for the device obtained from the emnify REST API.
As with showDeviceDetails, network requests in this function may fail.
Use a try-catch block to handle both success and error scenarios.
Inside the try block, use client.request to call the emnify REST API for the device’s connectivity status.
Authorization header with the JWT token.The response contains the device’s connectivity status. Extract the status description and format the current date to show when the information was last updated.
formatDate function (defined earlier) to format the date.Pass the connectivityData object to the renderTemplate function to populate and display the data in the app.
Here’s the completed showConnectivityStatus function:
This section explains how to add and implement buttons under each information block, allowing agents to manually refresh the device and connectivity statuses.
Incorporate buttons into your HTML structure in iframe.html under each information block:
Event listeners connect the buttons to the respective functions, ensuring that clicking them triggers data fetching and updates.
Add the following code to your main.js file:
How it works:
refresh-connectivity and refresh-device-details).showConnectivityStatus function is invoked using the stored deviceId.showDeviceDetails function, passing the ICCID (iccidFromCustomField).This section explains how to add and implement a button under the Device Connectivity Status block, allowing agents to reset connectivity for a device using emnify’s Reset Endpoint Connectivity API call. The button displays status updates during the process and refresh the connectivity status once the reset is complete.
Add the “Reset Connectivity” button and a status span for feedback in your iframe.html file:
Begin by defining the resetConnectivity function.
By the end, this function:
PATCH request to the connectivity reset API entrypoint.Inside the function, access the status span element (reset-connectivity-status) to provide feedback to the agent.
Update the text to “Processing…” and style it with a warning class to visually indicate that the reset process is ongoing.
The emnify REST API requires a payload with location and pdp_context.
For this guide, set these values to null.
Serialize this data into JSON format using JSON.stringify.
Next, prepare the headers for the API request. Include:
Authorization header with the bearer token for authentication.Content-Type header specifying that the payload is JSON.By storing these headers in a variable, you maintain compatibility with the Zendesk app framework.
Use the client.request method to send a PATCH request to the emnify REST API.
The deviceId is passed in the URL to specify the device to be reset.
If the request is successful:
setTimeout function to fetch the new connectivity status after two minutes.
When reset connectivity is triggered, the device detaches from the network and waits for the modem to connect again. Depending on the device and network conditions, this process can take a few seconds to a few minutes. The connectivity status fetched may not be accurate unless you add a delay.
Here’s how the complete resetConnectivity function looks:
If you haven’t already, install ZCLI then follow the steps from the Zendesk developer documentation to Install the private app in Zendesk Support.