REST.call Function Reference
The scripting environment allows you to make external HTTP requests to interact with third-party APIs or services using the REST.call function. This is the current and recommended version for making REST API calls.
Syntax
REST.call(URL, method, headers, body, attachmentsInfo);
Parameters
The REST.call function takes the following parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
| URL | String | Yes | The URL endpoint for the REST API call. |
| method | String | Yes | The HTTP method to use (e.g., GET, POST, PUT, DELETE, etc.). |
| headers | Object | Yes | An object containing key-value pairs for the request headers (e.g., { 'Content-Type': 'application/json', 'Authorization': 'Bearer ...' }). |
| body | Any | Yes | The body of the request. The expected type depends on the method and the Content-Type header. For GET requests, the body is typically null or an empty object. For POST/PUT requests, it could be a JSON object, string, form data, etc. |
| attachmentsInfo | Array | No | If desired, attachments stored in the document that the script is executed on can be sent as part of a request. In such a case, this 5th parameter should be provided. It should be an array of objects with the attributes described below: |
| attachmentsInfo.valuePath | String | Yes | The path within the server document pointing to the attachment field. |
| attachmentsInfo.fileName | String | Yes (if fileId not provided) | Specific file name within the document. |
| attachmentsInfo.fileId | String | Yes (if fileName not provided) | Specific file id within the document. |
Return Value
Different types of return values can be given. Once a GET request is successfully done, the attributes can be shown by using console.log to see the return value. Make sure to remove this log once the return value is checked, as it is not a relevant log to keep inside a script.
Example: Making a GET Request
This example shows how to make a simple GET request to fetch data from an API endpoint using REST.call.
let response = REST.call('https://www.google.com', 'GET', {}, {});
if (response.headers) {
console.log('Data fetched successfully:', response);
} else {
console.log('Failed to fetch data. Status:', response);
}
Example: POSTing an attachment
This example shows how to send a document attachment to an API endpoint using REST.call.
The example assumes that the document the script is executed on contains an attachment field named 'picture', and that it contains at least 2 attachments, as the second one (index 1) is sent. The attachment will be included in the request under the header name 'file' (the expected name is generally specified in the target API's docs).
const fieldFileId = serverDocument.picture[1]._id;
REST.call(
'https://webhook.site/yourStubPersonalIdHere', // Access https://webhook.site/ to get your own URL for testing
'POST',
{},
{},
[{ valuePath: 'picture', requestHeaderName: 'file', fileId: fieldFileId }]
);