Outbound REST (RESTMessageV2)
Quick answer
Call external APIs from ServiceNow: REST messages, methods, authentication, error handling and MID server routing.
Key takeaways
- Keep endpoints and credentials in configuration, not in code
- Always set a timeout and handle non 200 responses
- Use connection and credential aliases so environments differ safely
- MID server routing reaches internal endpoints without opening the firewall
Configuration first
Create a REST message with the base endpoint, then a method per operation with its HTTP verb and path. Variables in the endpoint use the ${var} syntax and are set at runtime, which keeps the script readable and the endpoint configurable.
Calling it
Instantiate from the record so configuration stays outside the code.
var r = new sn_ws.RESTMessageV2('Acme CMDB', 'getAsset');
r.setStringParameterNoEscape('assetTag', current.asset_tag.toString());
r.setHttpTimeout(15000);
var resp = r.execute();
var code = resp.getStatusCode();
if (code != 200) {
gs.error('Acme CMDB call failed: ' + code + ' ' + resp.getErrorMessage());
} else {
var body = JSON.parse(resp.getBody());
}Production concerns
External calls fail, so plan for it.
- Always set a timeout, the default is generous and blocks a worker
- Log the status code and correlation id, never the full body with secrets
- Use executeAsync for long running calls from a business rule
- Route through a MID server when the endpoint is internal only
- Store credentials in a credential record or connection alias, not in script
Want to learn this properly?
Our live, instructor-led ServiceNow Training covers this hands-on, with real projects and a certification path.
Check your understanding
Which class sends an outbound REST call?
- A. GlideHTTPRequest
- B. sn_ws.RESTMessageV2
- C. RESTMessage
- D. sn_http.Client
Show answer
B. sn_ws.RESTMessageV2
RESTMessageV2 in the sn_ws scope is the supported API.
What reaches an endpoint that is only available inside the corporate network?
- A. A scheduled job
- B. A MID server
- C. An import set
- D. A UI action
Show answer
B. A MID server
The MID server executes the call from inside the network.