{"revision":"c10276e953456835cdbb1f3a60d58ae1980be353","document":{"id":2466,"path":"application-development/servicenow-sdk/scripted-rest-api-api-now-ts.md","title":"Scripted REST API - ServiceNow Fluent","bundle":null,"doc_type":null,"release":"australia","body":"\n# Scripted REST API - ServiceNow Fluent\n\nThe Scripted REST API defines the endpoints, query parameters, and headers for a scripted REST service \\[sys\\_ws\\_definition\\].\n\n**Note:** For the latest ServiceNow Fluent API documentation and examples, see the [ServiceNow Fluent API reference](https://servicenow.github.io/sdk/category/api-reference) and [ServiceNow SDK examples repository](https://github.com/ServiceNow/sdk-examples) on GitHub.\n\nFor general information about scripted REST services, see [Scripted REST APIs](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/api-reference/c_CustomWebServices.md).\n\n**Parent Topic:**[ServiceNow Fluent API reference](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/application-development/servicenow-sdk/servicenow-fluent-api-reference.md)\n\n**Related topics**  \n\n\n[ServiceNow Fluent](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/application-development/servicenow-fluent.md)\n\n## RestApi object\n\nCreate a scripted REST API \\[sys\\_ws\\_definition\\] to define web service endpoints.\n\n<table id=\"table_vy2_525_jq\" class=\"parameters\"><thead><tr><th>\n\nName\n\n</th><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\n$id\n\n</td><td>\n\nString or Number\n\n</td><td>\n\nRequired. A unique ID for the metadata object. When you build the application, this ID is hashed into a unique sys\\_id. For more information, see [ServiceNow Fluent language constructs](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/application-development/servicenow-sdk/fluent-constructs.md).Format: `Now.ID['String' or Number]`\n\n</td></tr><tr><td>\n\nname\n\n</td><td>\n\nString\n\n</td><td>\n\nRequired. The name of the API, which is used in the API documentation.\n\n</td></tr><tr><td>\n\nserviceId\n\n</td><td>\n\nString\n\n</td><td>\n\nRequired. The API identifier used to distinguish this API in URI paths. It must be unique within the API namespace.\n\n</td></tr><tr><td>\n\nactive\n\n</td><td>\n\nBoolean\n\n</td><td>\n\nFlag that indicates whether the API can serve requests.Valid values:\n\n-   true: The API can serve requests.\n-   false: The API can't serve requests.\n\nDefault: true\n\n</td></tr><tr><td>\n\nshortDescription\n\n</td><td>\n\nString\n\n</td><td>\n\nA brief description of the API, which is used in the API documentation.\n\n</td></tr><tr><td>\n\nconsumes\n\n</td><td>\n\nString\n\n</td><td>\n\nA list of media types that resources of the API can consume.Default: application/json,application/xml,text/xml\n\n</td></tr><tr><td>\n\ndocLink\n\n</td><td>\n\nString\n\n</td><td>\n\nA URL that links to static documentation about the API.\n\n</td></tr><tr><td>\n\nenforceAcl\n\n</td><td>\n\nArray\n\n</td><td>\n\nA list of variable identifiers of ACL objects or sys\\_ids of ACLs to enforce when accessing resources \\[sys\\_security\\_acl\\]. For more information, see [Access Control List API - ServiceNow Fluent](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/application-development/servicenow-sdk/acl-api-now-ts.md).To not enforce ACLs, set this property to an empty array \\(`[]`\\).\n\nDefault: Scripted REST External Default\n\n</td></tr><tr><td>\n\nproduces\n\n</td><td>\n\nString\n\n</td><td>\n\nA list of media types that resources of the API can produce.Default: application/json,application/xml,text/xml\n\n</td></tr><tr><td>\n\nroutes\n\n</td><td>\n\nArray\n\n</td><td>\n\nThe resources \\[sys\\_ws\\_operation\\] for the API. For more information, see [routes object](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/application-development/servicenow-sdk/scripted-rest-api-api-now-ts.md).\n\n</td></tr><tr><td>\n\npolicy\n\n</td><td>\n\nString\n\n</td><td>\n\nThe policy for how application files are protected when downloaded or installed.Valid values:\n\n-   read: Files are viewable only.\n-   protected: Users with password permissions can edit the files.\n\n</td></tr><tr><td>\n\nversions\n\n</td><td>\n\nArray\n\n</td><td>\n\nA list of versions \\[sys\\_ws\\_version\\] for the API. For more information, see [versions object](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/application-development/servicenow-sdk/scripted-rest-api-api-now-ts.md).Specifying versions allows you to manage different versions of an API and their statuses, such as whether they are active, the default version, or deprecated.\n\n</td></tr><tr><td>\n\n$meta\n\n</td><td>\n\nObject\n\n</td><td>\n\nMetadata for the application metadata.With the installMethod property, you can map the application metadata to an output directory that loads only in specific circumstances.\n\n```javascript\n$meta: {\n      installMethod: 'String'\n}\n```\n\nValid values for installMethod:\n\n-   demo: Outputs the application metadata to the `metadata/unload.demo` directory to be installed with the application when the **Load demo data** option is selected.\n-   first install: Outputs the application metadata to the `metadata/unload` directory to be installed only the first time an application is installed on an instance.\n\n</td></tr></tbody>\n</table>```javascript\nimport { RestApi } from '@servicenow/sdk/core'\nimport { process } from '../server/handler.js'\n\nRestApi({\n    $id: Now.ID['rest1'],\n    name: 'customAPI',\n    serviceId: 'custom_api',\n    consumes: 'application/json',\n    routes: [\n        {\n            $id: Now.ID['route1'],\n            path: '/home/{id}',\n            script: process,\n            parameters: [{ $id: Now.ID['param1'],  name: 'n_param' }],\n            headers: [{ $id: Now.ID['header1'],  name: 'n_token' }],\n            enforceAcl: [acl],\n            version: 1,\n        },\n    ],\n    enforceAcl: [acl],\n    versions: [\n        {\n            $id: Now.ID['v1'],\n            version: 1,\n        },\n    ],\n})\n\n```\n\nThe ACL referenced is defined using the ACL object:\n\n```javascript\nimport { Acl } from \"@servicenow/sdk/core\";\n\nconst acl = Acl({\n    name: 'My random ACL',\n    type: 'rest_endpoint',\n    script: `answer = (Math.random() > 0.5)`,\n    active: true,\n    adminOverrides: false,\n    operations: ['execute'],\n})\n```\n\n## routes object\n\nCreate a scripted REST resource \\[sys\\_ws\\_operation\\] to define the HTTP method, the processing script, and to override settings from the parent service.\n\nUse the routes object within the RestApi object.\n\n<table id=\"table_vy2_525_jq\" class=\"parameters\"><thead><tr><th>\n\nName\n\n</th><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\n$id\n\n</td><td>\n\nString or Number\n\n</td><td>\n\nRequired. A unique ID for the metadata object. When you build the application, this ID is hashed into a unique sys\\_id. For more information, see [ServiceNow Fluent language constructs](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/application-development/servicenow-sdk/fluent-constructs.md).Format: `Now.ID['String' or Number]`\n\n</td></tr><tr><td>\n\nname\n\n</td><td>\n\nString\n\n</td><td>\n\nThe name of the API resource, which is used in the API documentation.Default: the value of the path property\n\n</td></tr><tr><td>\n\nscript\n\n</td><td>\n\nScript\n\n</td><td>\n\nRequired. The custom script defines how the operation parses and responds to requests. This property supports a function from a JavaScript module, a reference to another file in the application that contains a script, or inline JavaScript.Format:\n\n-   For functions, use the name of a function, function expression, or default function exported from a JavaScript module and import it into the `.now.ts` file. For information about JavaScript modules, see [JavaScript modules and third-party libraries](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/application-development/javascript-modules-third-party-libraries.md).\n-   To use text content from another file, refer to a file in the application using the following format: `Now.include('path/to/file')`. For more information, see [ServiceNow Fluent language constructs](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/application-development/servicenow-sdk/fluent-constructs.md).\n-   To provide an inline script, use string literals or template literals for multiple lines of code: `'Script' or `Script``.\n\n</td></tr><tr><td>\n\nparameters\n\n</td><td>\n\nArray\n\n</td><td>\n\nA list of query parameters \\[sys\\_ws\\_query\\_parameter\\] for the route. For more information, see [parameters and headers objects](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/application-development/servicenow-sdk/scripted-rest-api-api-now-ts.md).\n\n</td></tr><tr><td>\n\nheaders\n\n</td><td>\n\nArray\n\n</td><td>\n\nA list of headers \\[sys\\_ws\\_header\\] for the route. For more information, see [parameters and headers objects](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/application-development/servicenow-sdk/scripted-rest-api-api-now-ts.md).\n\n</td></tr><tr><td>\n\nactive\n\n</td><td>\n\nBoolean\n\n</td><td>\n\nFlag that indicates whether the resource is used.Valid values:\n\n-   true: The resource is used.\n-   false: The resource isn't used.\n\nDefault: true\n\n</td></tr><tr><td>\n\npath\n\n</td><td>\n\nString\n\n</td><td>\n\nThe path of the resource relative to the base API path. The relative URI can contain path parameters such as `'/abc/{id}'`.Default: /\n\n</td></tr><tr><td>\n\nshortDescription\n\n</td><td>\n\nString\n\n</td><td>\n\nA brief description of the resource, which is used in the API documentation.\n\n</td></tr><tr><td>\n\nconsumes\n\n</td><td>\n\nString\n\n</td><td>\n\nA list of media types that the resource can consume.This property can be overridden with the PUT, PATCH, or POST methods.\n\nDefault: The value of the consumes property in the RestApi object\n\n</td></tr><tr><td>\n\nenforceAcl\n\n</td><td>\n\nArray\n\n</td><td>\n\nA list of variable identifiers of ACL objects or sys\\_ids of ACLs to enforce when accessing resources \\[sys\\_security\\_acl\\]. For more information, see [Access Control List API - ServiceNow Fluent](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/application-development/servicenow-sdk/acl-api-now-ts.md).To not enforce ACLs, set this property to an empty array \\(`[]`\\).\n\nDefault: Scripted REST External Default\n\n</td></tr><tr><td>\n\nproduces\n\n</td><td>\n\nString\n\n</td><td>\n\nA list of media types that the resource can produce.Default: The value of the produces property in the RestApi object\n\n</td></tr><tr><td>\n\nrequestExample\n\n</td><td>\n\nString\n\n</td><td>\n\nA valid sample request body payload for the resource, which is used in the API documentation.\n\n</td></tr><tr><td>\n\nmethod\n\n</td><td>\n\nString\n\n</td><td>\n\nThe HTTP method that the resource implements.Valid values: GET, POST, PUT, PATCH, DELETE\n\nDefault: GET\n\n</td></tr><tr><td>\n\nauthentication\n\n</td><td>\n\nBoolean\n\n</td><td>\n\nFlag that indicates whether users must be authenticated to access the resource.Valid values:\n\n-   true: Users must be authenticated to access the resource.\n-   false: Authentication isn't required to access the resource.\n\nDefault: true\n\n</td></tr><tr><td>\n\nauthorization\n\n</td><td>\n\nBoolean\n\n</td><td>\n\nFlag that indicates whether ACLs are enforced when accessing the resource.Valid values:\n\n-   true: ACLs are enforced when accessing the resource.\n-   false: ACLs aren't enforced when accessing the resource.\n\nDefault: true\n\n</td></tr><tr><td>\n\ninternalRole\n\n</td><td>\n\nBoolean\n\n</td><td>\n\nFlag that indicates whether the route requires the snc\\_internal role.This property is supported only if the Explicit Roles plugin \\(com.glide.explicit\\_roles\\) is enabled.\n\nValid values:\n\n-   true: The route requires the snc\\_internal role.\n-   false: The route doesn't require the snc\\_internal role.\n\nDefault: true\n\n</td></tr><tr><td>\n\npolicy\n\n</td><td>\n\nString\n\n</td><td>\n\nThe policy for how application files are protected when downloaded or installed.Valid values:\n\n-   read: Files are viewable only.\n-   protected: Users with password permissions can edit the files.\n\n</td></tr><tr><td>\n\nversion\n\n</td><td>\n\nNumber\n\n</td><td>\n\nThe version of the API.This property is required if the versions property is used in the RestApi object.\n\nThe version specified with this property is used to automatically generate a URI with a version, such as `/api/management/v1/table/{tableName}`. Version numbers identify the endpoint version that a URI accesses. By specifying a version number, you can test and deploy changes without impacting existing integrations.\n\n</td></tr><tr><td>\n\n$meta\n\n</td><td>\n\nObject\n\n</td><td>\n\nMetadata for the application metadata.With the installMethod property, you can map the application metadata to an output directory that loads only in specific circumstances.\n\n```javascript\n$meta: {\n      installMethod: 'String'\n}\n```\n\nValid values for installMethod:\n\n-   demo: Outputs the application metadata to the `metadata/unload.demo` directory to be installed with the application when the **Load demo data** option is selected.\n-   first install: Outputs the application metadata to the `metadata/unload` directory to be installed only the first time an application is installed on an instance.\n\n</td></tr></tbody>\n</table>```javascript\nroutes: [\n   {\n      $id: Now.ID['route1'],\n      path: '/home/{id}',\n      script: process,\n      parameters: [{ $id: Now.ID['param1'],  name: 'n_param' }],\n      headers: [{ $id: Now.ID['header1'],  name: 'n_token' }],\n      enforceAcl: [acl],\n      version: 1,\n   },\n],\n```\n\n### parameters and headers objects\n\nCreate query parameters \\[sys\\_ws\\_query\\_parameter\\] and headers \\[sys\\_ws\\_header\\] for routes in a scripted REST API. Query parameters control what values a requesting user can pass in the request URI. Headers specify what the API accepts and can respond with.\n\nUse the parameters and headers objects within the routes object.\n\n<table id=\"table_vy2_525_jq\" class=\"parameters\"><thead><tr><th>\n\nName\n\n</th><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\n$id\n\n</td><td>\n\nString or Number\n\n</td><td>\n\nRequired. A unique ID for the metadata object. When you build the application, this ID is hashed into a unique sys\\_id. For more information, see [ServiceNow Fluent language constructs](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/application-development/servicenow-sdk/fluent-constructs.md).Format: `Now.ID['String' or Number]`\n\n</td></tr><tr><td>\n\nname\n\n</td><td>\n\nString\n\n</td><td>\n\nRequired. The name of the parameter or header, which is used in the API documentation.\n\n</td></tr><tr><td>\n\nrequired\n\n</td><td>\n\nBoolean\n\n</td><td>\n\nFlag that indicates whether the parameter or header is required.Valid values:\n\n-   true: The parameter or header is required.\n-   false: The parameter or header isn't required.\n\nDefault: false\n\n</td></tr><tr><td>\n\nexampleValue\n\n</td><td>\n\nString\n\n</td><td>\n\nAn example of a valid value for the parameter or header, which is used in the API documentation.\n\n</td></tr><tr><td>\n\nshortDescription\n\n</td><td>\n\nString\n\n</td><td>\n\nA brief description of the parameter or header, which is used in the API documentation.\n\n</td></tr><tr><td>\n\n$meta\n\n</td><td>\n\nObject\n\n</td><td>\n\nMetadata for the application metadata.With the installMethod property, you can map the application metadata to an output directory that loads only in specific circumstances.\n\n```javascript\n$meta: {\n      installMethod: 'String'\n}\n```\n\nValid values for installMethod:\n\n-   demo: Outputs the application metadata to the `metadata/unload.demo` directory to be installed with the application when the **Load demo data** option is selected.\n-   first install: Outputs the application metadata to the `metadata/unload` directory to be installed only the first time an application is installed on an instance.\n\n</td></tr></tbody>\n</table>```javascript\nparameters: [{ $id: Now.ID['param1'],  name: 'n_param' }],\nheaders: [{ $id: Now.ID['header1'],  name: 'n_token' }],\n```\n\n## versions object\n\nCreate versions for a scripted REST API \\[sys\\_ws\\_version\\] to define web service endpoints.\n\nUse the versions object within the RestApi object.\n\n<table id=\"table_vy2_525_jq\" class=\"parameters\"><thead><tr><th>\n\nName\n\n</th><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\n$id\n\n</td><td>\n\nString or Number\n\n</td><td>\n\nRequired. A unique ID for the metadata object. When you build the application, this ID is hashed into a unique sys\\_id. For more information, see [ServiceNow Fluent language constructs](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/application-development/servicenow-sdk/fluent-constructs.md).Format: `Now.ID['String' or Number]`\n\n</td></tr><tr><td>\n\nversion\n\n</td><td>\n\nNumber\n\n</td><td>\n\nRequired. A version of the REST API.\n\n</td></tr><tr><td>\n\nactive\n\n</td><td>\n\nBoolean\n\n</td><td>\n\nFlag that indicates whether the version of the REST API can serve requests.Valid values:\n\n-   true: The version of the API can serve requests.\n-   false: The version of the API can't serve requests.\n\nDefault: true\n\n</td></tr><tr><td>\n\ndeprecated\n\n</td><td>\n\nBoolean\n\n</td><td>\n\nFlag that indicates whether the version of the REST API is deprecated. Resources belonging to deprecated versions can serve requests, but are identified as deprecated in documentation.Valid values:\n\n-   true: The version of the API is identified as deprecated.\n-   false: The version of the API isn't identified as deprecated.\n\nDefault: false\n\n</td></tr><tr><td>\n\nshortDescription\n\n</td><td>\n\nString\n\n</td><td>\n\nA brief description of the version of the REST API, which appears in the API documentation.\n\n</td></tr><tr><td>\n\nisDefault\n\n</td><td>\n\nBoolean\n\n</td><td>\n\nFlag that indicates whether the version of the REST API is the default version. Clients can access the default version using either the versioned or non-versioned URI path.Valid values:\n\n-   true: The version of the API is the default version.\n-   false: The version of the API isn't the default version.\n\nDefault: false\n\n</td></tr><tr><td>\n\n$meta\n\n</td><td>\n\nObject\n\n</td><td>\n\nMetadata for the application metadata.With the installMethod property, you can map the application metadata to an output directory that loads only in specific circumstances.\n\n```javascript\n$meta: {\n      installMethod: 'String'\n}\n```\n\nValid values for installMethod:\n\n-   demo: Outputs the application metadata to the `metadata/unload.demo` directory to be installed with the application when the **Load demo data** option is selected.\n-   first install: Outputs the application metadata to the `metadata/unload` directory to be installed only the first time an application is installed on an instance.\n\n</td></tr></tbody>\n</table>```javascript\nversions: [\n {\n   $id: Now.ID['v1'],\n   version: 1,\n },\n],\n```\n\n"}}