{"revision":"c10276e953456835cdbb1f3a60d58ae1980be353","document":{"id":765,"path":"api-reference/server-api-reference/FetchAPI.md","title":"Fetch - Scoped, Global","bundle":null,"doc_type":null,"release":"australia","body":"\n# Fetch - Scoped, Global\n\nThe Fetch API provides methods for fetching resources using request and response objects.\n\nYou can use the Fetch API in a background script and wherever you can make HTTP calls \\(like a REST endpoint\\). The Fetch API allows you to make both simple and more complicated fetch requests by setting headers, body options, and so on.\n\nThis API is part of a set of Fetch APIs, which provide various actions for fetching resources from external websites. The full Fetch API collection includes:\n\n-   [Fetch - fetch\\(String resource, Object options\\)](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/FetchAPI.md): Starts the process of fetching a resource from the network.\n-   [Fetch Headers - Scoped, Global](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/Fetch.HeadersAPI.md): Retrieve and modify request and response headers.\n-   [Fetch Request - Scoped, Global](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/Fetch.RequestAPI.md): Create a new request object.\n-   [Fetch RequestInit - Scoped, Global](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/Fetch.RequestInitAPI.md): Set options to configure a fetch request.\n-   [Fetch Response - Scoped,Global](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/Fetch.ResponseAPI.md): Create a new response object.\n\nTo support fetch actions, the system property, `glide.hosts.allowlist`, allows controls over what hosts a fetch method can access. It applies to HTTP APIs like RestMessageV2 and those mentioned above. For more information about `glide.hosts.allowlist`, see [Available system properties](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/platform-administration/r_AvailableSystemProperties.md).\n\n**Parent Topic:**[Server API reference](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/api-server.md)\n\n## Fetch - fetch\\(String resource, Object options\\)\n\nStarts the process of fetching a resource from the network and returns a promise that is fulfilled once the response is available.\n\n<table id=\"table_arq_npb_jdc\" 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\nresource\n\n</td><td>\n\nString or Object\n\n</td><td>\n\nRequired. The resource to fetch. Accepted values:\n\n-   A string or an object with a stringifier, including a URL object, that provides the URL of the resource you want to fetch. The URL may be relative to the base URL which is the document's baseURI.\n-   A [Fetch Request - Request\\(\\)](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/Fetch.RequestAPI.md) object.\n\n</td></tr><tr><td>\n\noptions\n\n</td><td>\n\nObject\n\n</td><td>\n\nOptional. A [Fetch RequestInit - Scoped, Global](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/Fetch.RequestInitAPI.md) object containing custom settings to apply to the request.Default: The request is passed without any customization settings.\n\n</td></tr></tbody>\n</table><table id=\"table_brq_npb_jdc\" class=\"returns\"><thead><tr><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\nObject\n\n</td><td>\n\nA promise that resolves to a response object.A fetch\\(\\) promise only rejects when the request fails, for example, because of a badly-formed request URL or a network error.\n\n**Note:** A fetch\\(\\) promise is not considered as rejected if the server responds with HTTP status codes that indicate errors like 404 or 504. In this instance, use a then\\(\\) handler to check the ok and status [Fetch Response - Scoped,Global](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/Fetch.ResponseAPI.md)properties.\n\n</td></tr></tbody>\n</table>The following example demonstrates how to form a new Request object using [Fetch Request - Request\\(\\)](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/Fetch.RequestAPI.md) and then fetch\\(\\) to retrieve its data. Here is a simple explanation of how each part of the code is used:\n\n-   request\\(\\): Configures the API request with URL, method, and headers.\n-   fetch\\(\\): Makes the fetch request.\n-   response.json\\(\\): Parses the JSON response body into a JavaScript object. \\(See [Fetch Response - json\\(\\)](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/Fetch.ResponseAPI.md) for more information.\\)\n-   console.log\\(\\): Outputs the incidents \\(data.result\\) using the Console API \\(See [Console - log\\(Object val1...val2, String msg, String subst1...subst2\\)](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/ConsoleAPI.md)\\).\n\n```\nasync function fetchIncidents() {\n    const url = 'https://your-instance.service-now.com/api/now/table/incident';\n    const username = 'your_username';\n    const password = 'your_password';\n\n    // Create a Request object\n    const request = new Request(url, {\n        method: 'GET',\n        headers: {\n            'Content-Type': 'application/json',\n            Authorization: 'Basic ' + btoa(`${username}:${password}`)\n        }\n    });\n\n    // Fetch data and use response.json() to process it\n    const response = await fetch(request);\n    const data = await response.json(); // Parse the response to JSON\n    console.log(data.result); // Log the incidents data to system logs\n}\n\nfetchIncidents();\n```\n\nOutput:\n\n```\n[\n    { \"number\": \"INC0001\", \"short_description\": \"System outage\" },\n    { \"number\": \"INC0002\", \"short_description\": \"Password reset request\" }\n]\n```\n\n"}}