Open the REST API Explorer, select the scripted REST API, and choose the YAML or JSON export link below its list of methods. The file includes committed request and response schemas and response headers, and imports into tools such as Postman or Insomnia. Creating schemas requires the web service administrator role.
How a scripted REST API is put together
A scripted REST API is defined in the Scripted REST Service form. It holds one or more resources, and each resource is one endpoint with its own HTTP method, processing script and optional overrides of the API-level settings. Because each resource has its own relative path, one HTTP method can serve several endpoints, such as a single GET on an identifier path or separate GET resources for user and message records.
Each address follows the same pattern: the instance host, an api segment, a namespace, an optional version, the API ID and the relative path. The namespace is the company code property for global-scope APIs, or the scope name inside a scoped application. The API ID is generated from the service name and can be edited.
| Component | Where it is set | What it controls |
|---|---|---|
| API ID | Scripted REST Service form | The address segment that identifies the API |
| Resource | Resources related list | HTTP method, processing script, relative path, overrides |
| Schema | Schemas related list or REST Schema form | Request and response body structure in OpenAPI 3.0.1 |
| Response header | Response Headers related list | Header names in a response |
Versions decide which endpoint a call reaches
A version number in the address, such as v1, selects which version of an endpoint runs. Enable versioning from the related links on the API record. The Make version v1 default option is selected by default, and if you clear it, the API answers only on its version-specific address.
A call without a version number reaches the version marked default. If none is marked, the latest version answers. Adding a version lets you make it the default at once and copy the resources of an existing version into it, so the new version starts with the same endpoints. For the design side, see ServiceNow Scripted REST API Design: Define the Contract First.
Exporting the OpenAPI file from REST API Explorer
Navigate to All, then System Web Services, then REST, then REST API Explorer. The export requires one of three roles: web_service_admin, rest_api_explorer or admin. Select the namespace, API name and version in the drop-downs to find the API, for example the Table API in the now namespace at the latest version.
- Select the API. A typical example is the Table API in the now namespace at the latest version.
- Below the list of methods, select the YAML or JSON export link.
- In the save dialog, choose a location and file name, then save.
The system downloads the API in the format you chose. Next, import the file into a web services tool such as Postman or Insomnia.
Writing the schemas the specification carries
A schema record has a name, a body in OpenAPI 3.0.1 format, an OpenAPI version selection and a description. Give each property an explicit type, as the OpenAPI 3.0.1 format expects. An object with one property called firstName, of type string, describes a request that carries a single text field.
Add a schema from the Schemas related list on the API record, or open the REST Schema form and select the API. Either route requires the web_service_admin role. For a request, create a REST Request record. Starting from a resource’s Request Schema tab pre-fills most of the fields; opening the form directly means setting the API, API resource, version, HTTP method, schema and description. The resource and at least one schema must already exist.
Responses work the same way through a REST Response record, whose HTTP status field sets the status the schema covers. Response headers each need a name and an example value, and they appear in the specification as string-typed headers. Committed request and response schemas are included in the exportable OpenAPI specification.
Generating request definitions from sampled traffic
Automatic generation builds request header associations, query parameter associations and a request schema from sample calls. An instance admin must first set the system property glide.rest_sampling.enabled to true. Enable it only on non-production instances, which limits the chance of performance problems. The role required is web_service_admin.
- Create a sampling configuration from a resource’s Request Sampling Configuration tab, or from the Scripted REST Sampling Configurations list.
- Set the Rest resource and Max samples. Max samples accepts 1 through 1000.
- Select Submit. Calls are sampled until the maximum is reached. Complete Sampling ends the run early.
- When sampling ends, the configuration moves to the analyzed state and the staging schemas become available. Review the Headers, Body and Query parameters fields.
- Select Commit.
Body structure is generated only for PUT and POST methods, so write the body schema by hand for any other method. On commit, Headers become request header associations on the service’s Request Headers tab, Body becomes the request schema on the Schemas tab, and Query parameters become associations on the Query Parameters tab. The committed staging schema and the sample requests are deleted, so copy out anything you want to keep first.
Manual request headers take a name, a short description and an example value. Setting Is required to true documents the requirement but does not enforce it; enforcement needs logic in the resource.
Securing endpoints and reading their errors
Default ACLs on the API apply to all of its endpoints, and a resource can override them. A caller must satisfy at least one selected ACL, not all of them. ACLs used here must have the type REST_Endpoint. Keep the Requires authentication check box selected on a resource that needs an ACL, because clearing it makes the resource public. Authentication for the API itself is set through the API access policy feature.
Calling an endpoint does not require the web_service_admin role. Requests still respect platform ACLs, so a caller must also meet table ACL requirements to reach instance data. Scripts that read records can use GlideRecordSecure so that data access controls apply to the requesting user. Write operations such as PUT, POST and DELETE should sit behind a stricter ACL than GET.
Error objects answer incoming requests with a standard HTTP error. They are not meant for catching exceptions inside server code. The numeric status code goes in the response header, and the body holds an error object with message and detail fields, plus a top-level status of failure. If the Accept header asks for an unsupported format, such as image/jpeg, the error body comes back as JSON.
| Status | Meaning for a scripted REST API |
|---|---|
| 200 / 201 / 204 | Success, with a body, a created body, or no body |
| 400 | The URI matches no API, the operation failed, or a header is invalid |
| 401 | The caller is not authorized to use the API |
| 403 | Not permitted, for example through an ACL, business rule or data policy |
| 404 | The resource does not exist, or an ACL constraint applies |
| 405 | The HTTP method is not allowed for this API |
| 406 / 415 | Unsupported response format in Accept, or unsupported request body format |
| 500 | An unexpected error occurred, and the response carries more detail |
Content types and request size
By default, all resources accept and return application/json, application/xml and text/xml. Custom types with a json or xml subtype are also treated as JSON or XML. The Default supported request and response formats fields on the API set the defaults, and a resource can override them. The resource-level Supported request formats field appears only for PUT, POST and PATCH.
Request size is capped by glide.rest.max_content_length, which defaults to 10 MB and can be raised to a maximum of 25. It applies whether or not the body is gzipped. Two narrower properties set plain and gzipped limits, defaulting to 10 MB and 1 MB, but the overall cap still applies. Reading the body through data, dataString, nextEntry or hasNext returns a 400 when the body is too large. The dataStream member is not subject to the limit.
Design habits that suit a published spec
A GET should only query data and should never change it. POST creates records without changing existing ones, PUT and PATCH modify existing records, and DELETE removes them. Introduce a new version before a significant change that would break integrations already on a published version. Leaving the API without a default version forces callers to name one.
Test authentication, authorization, response codes, headers and bodies before release. The Automated Test Framework includes Inbound REST test steps for custom APIs.
Frequently asked questions
How do I export a scripted REST API to an OpenAPI file?
Open REST API Explorer, select the API, and choose the YAML or JSON export link below its list of methods. Pick a location and file name when the browser asks.
Which OpenAPI version do the schemas use?
Schemas use OpenAPI 3.0.1. The schema record asks you to select an OpenAPI version and to add a description of its structure.
Do consumers of the spec need the web service administrator role?
No. That role is for building and changing scripted REST APIs. Calling an endpoint is governed by the ACLs on the API or resource and by the API access policy.
Should request definitions be generated on a production instance?
No. Enable automatic generation only on non-production instances. There, set glide.rest_sampling.enabled to true, choose Max samples between 1 and 1000, let sampling finish, and commit the definitions.
Why does a required query parameter still reach my script?
Setting Is required to true documents the parameter but does not enforce it. Enforcement requires logic in the resource that rejects a request without it.
What does a caller receive when a resource returns an error?
The status code goes in the response header. The body contains an error object with message and detail fields, and a top-level status of failure.