🠸 Back to Overview

Code Execution

When a shortcut is executed, it is possible to run JavaScript code snippets before or after execution. You'll find the corresponding text input area in the shortcut editor in the "Pre-Request Actions" and "Post-Request Actions" sections. This is a feature newly added in version 1.24, and as such it is still a bit experimental.

Using these code blocks, there are a number of things you can achieve. See the examples below for inspiration.

Handling the Response

You can access the response of your HTTP request via the response object.

Getting the Response Body

The response body is available as a string via response.body.

const myBody = response.body;

Getting Response Headers

The response headers are available as an object (key-value-pairs) via response.headers.

const contentType = response.headers['Content-Type'];

Getting the Status Code

The response's HTTP status code is available as an integer via response.statusCode.

const isNotFound = response.statusCode == 404;

Getting Cookies

The response's cookies are available as an object (key-value-pairs) via response.cookies.

const myCookieValue = response.cookies['MyCookieName'];

Errors

Please note that the response object will be null if there was no response from the server, i.e., in case of a network error. In that case, you can inspect the networkError to get a string describing the error.

Getting Information about the Shortcut

You can get the name of the current shortcut using shortcut.name and its description using shortcut.description.

Reading & Writing Variables

In the app you can create variables to be used in your shortcuts, e.g. to dynamically insert values for a URL, a query parameter or part of the request body. To do so use the variable editor found by clicking the "{}" button in the main screen of the app.

This section explains how you can interact with these variables from a script.

getVariable

You can access the value of any of your variables via the getVariable() function. Simply pass the variable's name or ID as a parameter.

const myValue = getVariable('myVariable');

Please note that the returned value will always be a string, or null if no such variable exists.

setVariable

You can store a value as a string into a variable via the setVariable() function. Simply pass the variable's name or ID as the first parameter and the value you want to store as the second parameter.

Please note that the setVariable function can only store values into variables of type 'Constant', and that there is a size limit of 3000 characters.

setVariable('myVariable', 'Hello World');

Built-in Actions

This section lists all the built-in functions and explains how they can be used.

showToast

With this action you can display a toast message on the screen. Simply pass your message as the first parameter.

showToast('Hello World');

showDialog

With this action you can display a dialog window on the screen. Simply pass your message as the first parameter, and optionally a title for the dialog as the second paramter. The dialog will be displayed until its "OK" button is pressed.

showDialog('My Message', 'My Title');

copyToClipboard

With this action you can copy a value to the device's clipboard. Simply pass the value you want to copy as the first parameter.

copyToClipboard('Hello World');

vibrate

With this action you can cause the device to vibrate (if supported). As an optional first parameter, you can pass the number of the vibration pattern you want to use, and as an optional second paramter you can pass a boolean denoting whether the execution should wait for the vibration pattern to finish or not.

Vibration patterns:

vibrate(2, true);

triggerShortcut

With this action you can trigger a shortcut to execute after the current one. Simply pass the name or ID of a shortcut as the first parameter.

triggerShortcut('My Other Shortcut');

renameShortcut

With this action you can rename a shortcut. Simply pass the name or ID of a shortcut as the first parameter and the new name as the second one.

renameShortcut('Old Name', 'New Name');

Examples

This section lists some examples for typical use-cases of the code execution feature.

Generate values for use in the request

Sometimes you need to generate a value, for example the current date, or a random number, to be used in your request. You can do this by generating the value and then storing it into a variable that you then use in your request. Here's an example:

const randomNumber = Math.floor(Math.random() * 10);
setVariable('myNumber', randomNumber);

const currentDate = (new Date()).toDateString();
setVariable('today', currentDate);

Parse a JSON response

Here's an example on how to parse the response of your shortcut, and display the result or store it into a variable for later user.

const temperature = JSON.parse(response.body).temperature;
setVariable('temperature', temperature);
showDialog('The current temperature is '+temperature+'°C', 'Temperature');