OBLYTECH Logo
OBLYTECH Logo

Scripted REST APIs in ServiceNow: Build, Secure and Version

A scripted REST API is a custom web service you define inside ServiceNow. You create the API record, add a resource for each endpoint with its HTTP method and processing script, attach access control lists at the API or resource level, and add versions so existing integrations keep working.

How the parts of a scripted REST API fit together

A scripted REST API groups related endpoints under one service record. The service holds shared settings such as the API ID, default access control lists and request and response formats. Each endpoint is a resource with its own HTTP method and script. For the design side of that contract, see scripted REST API design contracts.

A request reaches a resource through a path made of the instance, a name space, an optional version, the API ID and the resource’s relative path.

Query parameters declare which values a caller may send. Scripts read them through queryParams. The API can set its valid content types, and each resource can override them.

Building the service and its first resource

Create the service record

Go to All > System Web Services > Scripted REST APIs and select New. You need the web_service_admin role. The API ID fills in from the name, and you can edit it. Choose the Default ACLs that should guard the resources, then submit.

Add a resource

Open the saved service, select New in the Resources list and enter a name. The name becomes part of the request URI, so use the noun the endpoint returns. Pick the HTTP method, write the processing script in the Script field, and add a short description on the Documentation tab for the REST API Explorer.

Securing the API with access control lists

Requests still respect platform ACLs, so table permissions decide which instance data a caller can reach. For the underlying model, see how access control lists work. Scripted REST security adds a layer on top. Choose ACLs whose type is REST_Endpoint, and a caller must satisfy at least one of them. Two checkboxes, Requires authentication and Requires ACL authorization, set three patterns:

Goal Requires authentication Requires ACL authorization Where the ACL is set
Public endpoint, no sign-in No No Not needed
Sign-in required, no ACL check Yes No Not needed
Sign-in plus ACL check Yes Yes Default ACLs on the service, or on the resource

Integrations that touch real records usually need the third row. The security and access design guide covers the wider decisions behind that choice.

Require an ACL on one resource

Resources inherit their service’s security. To tighten a single endpoint, open the resource’s Security tab, keep Requires authentication checked, tick Requires ACL authorization and choose the REST_Endpoint ACLs. A resource ACL replaces the parent’s ACLs, and an empty field falls back to them. Clearing Requires authentication makes the resource public, even when the parent requires an ACL, so treat that box as a deliberate choice. Resource ACLs are checked for signed-in users.

Path-based ACLs for read-only and guest APIs

A path-based ACL is a rule keyed to a resource path and stored in the sys_security_acl table with the type REST Endpoint. It helps when you cannot edit a read-only API, when you want guest access with a controlled check, or when you need roles the original API does not define. Path-based ACLs are checked on each request to the matching path and work alongside operation-referenced ACLs, so a request must pass each applicable one. They cannot bypass a restrictive ACL the API owner has already set.

A user with the security_admin or admin role opens sys_security_acl, selects New, sets the type to REST Endpoint, picks the HTTP operation and pastes the Resource path value from the SysWS operation record. Add the condition or script and submit.

Versioning without breaking existing integrations

Callers place the version in the URI, such as v1, and a request without one reaches the default version. For the design side of version changes, see API contract versioning.

Turn versioning on

The API must already exist. Select Enable versioning under Related Links. The dialog ticks Make version v1 default unless you clear it. Clearing it leaves the API with no default, so callers must name a version. A Versioning list then appears on the form, where you add versions and choose the default.

Add a version and copy resources forward

Select Add new version under Related Links. You can make it the default in the same step, and copy all resources from an existing version as a starting point. Change the new version and leave the old one alone.

Two habits follow from this. Create a new version before you change behavior that clients depend on. For a small addition, add an optional query parameter to the existing version instead.

Writing scripts for input, output and errors

Reading inputs

The request object exposes queryParams, pathParams, individual headers through getHeader and the body. A string POST body arrives in request.body.dataString, and a binary body arrives in request.body.data. Wrap the script in a function that takes request and response and call it with both. Move logic several resources share into a script include.

Streaming or object serialization

Use object serialization for a simple JSON or XML object with a predictable size. The platform sets the status and Content-Type for you, but the whole object is built before anything is sent, which can delay a large response. Use streaming for other formats or variable sizes. It starts sooner and can help instance performance, but you format the output and set the status and Content-Type yourself. To page a large table in a streamed response, the scoped GlideRecord method chooseWindow takes a first row, a last row and a Boolean that forces a count.

Returning errors

Error objects answer an incoming request with a standard HTTP error. The preset objects are NotAcceptableError, BadRequestError, ConflictError, NotFoundError and UnsupportedMediaTypeError. ServiceError lets you set your own status, message and detail. Use them for problems with the request, not for failures in your own code.

Status codes a scripted REST API returns

Code Meaning Typical cause
200 Success, with a body Read or update that returned data
201 Created, with a body New record created by a POST
204 Success, no body Record deleted successfully
400 Bad request Malformed request or invalid headers
401 Unauthorized Caller is not authorized to use the API
403 Forbidden Permission check failed for this operation
404 Not found Record does not exist
406 Not acceptable Accept header asks for a format the endpoint does not return
415 Unsupported media type Request body format is not accepted
500 Internal server error Unexpected failure on the server

For a wider set of failure patterns, see troubleshooting ServiceNow REST and SOAP integrations.

Good practices and testing before release

REST conventions give each method a job. GET should never modify data. POST creates records without changing existing ones, PUT and PATCH change existing records, and DELETE removes them.

Protect the data as well as the endpoint. In scripts that read records for a caller, use the GlideRecordSecure API instead of GlideRecord, so the standard read and write ACLs on the underlying data apply to that user, although query ACLs need an explicit opt-in. Require a stricter ACL on PUT, POST and DELETE resources than on GET.

Test authentication and authorization before release. Check response codes, headers and bodies, and include error responses. A REST client with automated testing, such as Postman, can repeat these checks. The Automated Test Framework’s Inbound REST test steps help confirm that a change keeps earlier behavior intact. The testing and acceptance checklist gives release gates to pair with these tests.

Generating request definitions from sample traffic

Sampling depends on the system property glide.rest_sampling.enabled, which must be true. Because sampling can affect performance, enable it on non-production instances. Create a request sampling configuration under Scripted REST Sampling Configurations, select the resource and set Max samples between 1 and 1000. When sampling finishes, review the staging schema and select Commit. Headers become request header associations, query parameters become query parameter associations, and the body becomes a request schema, generated only for PUT and POST. The committed definitions then appear in the OpenAPI export from the REST API Explorer, which the OpenAPI guide for scripted REST APIs covers.

Frequently asked questions

Does a caller need the web_service_admin role to use an endpoint?

No. That role manages the API and its resources. Callers reach an endpoint through its authentication and ACL settings.

Can two versions of one API run at the same time?

Yes. Each version carries its own resources.

What happens when a caller omits the version and no default is set?

Such a call then needs a version in the URI. Set a default later from the Versioning list if you want unversioned calls to reach one.

Can I attach any access control list to an endpoint?

Choose ACLs whose type is REST_Endpoint. You can pick one or more on the API or on the resource, and a caller who satisfies at least one of them gets through.