Skip to content

Calling REST Services

Server scripts call REST services through $Rest. This page shows the request patterns most integrations need: reading and sending JSON, authenticating with a configured service, handling status codes, transferring files, and limiting the wait for slow services. For SOAP services, see Web Services.

Sending a Request

Create a client for the base URL, create a request for a resource, and execute it with the HTTP method:

1
2
3
4
5
6
7
var customer = $Rest.Create('https://api.example.com/')
    .Request('customers/{id}')
    .AddUrlParameter('id', $Xml.Evaluate('Customer/Id'))
    .Get()
    .ToJson();

$Xml.SetValue('Customer/Name', customer['name']);

AddUrlParameter fills a {name} segment of the resource and encodes the value. Use it instead of concatenating values into the URL. Use AddQueryParameter for query string values and AddHeader for request headers.

To send a body, pass it as the second argument of Request. The method becomes POST. An object is sent as JSON; an Xml node, or a string that starts with < and ends with >, is sent as XML:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
var ticket = $Rest.Create('https://helpdesk.example.com/api/')
    .Request('tickets', {
        title : $Xml.Evaluate('Subject'),
        description : $Xml.Evaluate('Description')
    })
    .Expect('Created')
    .Post()
    .ToJson();

$Xml.SetValue('TicketId', ticket['id']);

Authenticating

Do not write user names, passwords, API keys or tokens in scripts. Scripts are part of the process definition and are visible to everyone who can edit or export it. Configure the service credentials in the domain settings instead, and refer to the service by name:

1
2
// 'CRM' is an integrated service configured in the domain settings
var client = $Rest.Create('https://crm.example.com/api/', 'CRM');

When the service acts on behalf of a user, pass the scope and the identity. The call succeeds only if the service is configured for server-to-server authentication or the user has already granted access:

1
2
3
4
5
var client = $Rest.Create('https://calendar.example.com/api/');

if (!client.Authenticate('Calendar', 'calendar.read', $Initiator)) {
    throw new Error('The initiator has not granted calendar access.');
}

Authenticate returns false when authentication fails; EnsureAuthenticated throws instead. See Integrated Services for configuring services.

Handling Status Codes

Get, Post and the other method helpers check the response status. They accept the usual success codes of the method, such as OK (200) and NoContent (204), and throw an error that contains the response content for any other status. Without a try/catch, the error stops the script.

When another status is a normal outcome, list every accepted status with Expect. Expect replaces the default list, so include the success status too:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
var response = $Rest.Create('https://api.example.com/')
    .Request('customers/{id}')
    .AddUrlParameter('id', $Xml.Evaluate('Customer/Id'))
    .Expect('OK')
    .Expect('NotFound')
    .Get();

if (response.StatusCode === 'NotFound') {
    $Xml.SetValue('Customer/Exists', false);
} else {
    $Xml.SetValue('Customer/Name', response.ToJson()['name']);
}

Execute does not check the status; read StatusCode yourself, or call Verify.

Transferring Files

Send a stored file as a multipart form field with AddFile, or as the whole request body with AddFileBody:

1
2
3
4
5
$Rest.Create('https://archive.example.com/api/')
    .Request('documents')
    .AddParameter('title', $Xml.Evaluate('Title'))
    .AddFile('file', $Xml.Evaluate('Attachment'))
    .Post();

To store a file returned by a service, read the response as Base64 and add it with $Files.AddBase64:

1
2
3
4
5
6
7
var response = $Rest.Create('https://reports.example.com/')
    .Request('reports/{id}/pdf')
    .AddUrlParameter('id', $Xml.Evaluate('ReportId'))
    .Get();

var file = $Files.AddBase64(response.ToBase64(), 'Report.pdf', 'application/pdf');
$Xml.SetValue('ReportFile', file.Id);

Limiting the Wait

A slow service delays the work item that calls it. Wrap calls whose result is optional in Script.Timeout. It returns null when the call fails or exceeds the limit:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
var rates = Script.Timeout(function () {
    return $Rest.Create('https://rates.example.com/')
        .Request('daily')
        .Get()
        .ToJson();
}, 5000);

if (rates === null) {
    console.warn('Exchange rates are not available; the previous rates are kept.');
} else {
    $Xml.SetValue('UsdRate', rates['USD']);
}

Methods that end with Async, such as GetAsync, return a Promise. Read the result with .then(function (response) { ... }).