{"library":"sn-http-request","title":"ServiceNow HTTP Request Client","type":"library","description":"sn-http-request is a specialized JavaScript HTTP client designed specifically for interacting with ServiceNow instances. It aims to simplify common HTTP operations within the ServiceNow ecosystem by providing features such as automatic user token refresh, asynchronous request queuing, request cancellation, request/response interceptors, and efficient request batching (defaulting to batch requests within a 50ms window). The current stable version is 24.0.3, though its public release cadence might be slower as it's primarily intended for internal ServiceNow development. Its key differentiators compared to generic HTTP clients like Axios or the Fetch API include built-in XSRF token handling and request compression, making it optimized for ServiceNow's API landscape and reducing boilerplate for common tasks in that environment.","language":"javascript","status":"active","last_verified":"Wed Apr 22","install":{"commands":["npm install sn-http-request"],"cli":null},"imports":["import { snHttpFactory } from 'sn-http-request';","const { snHttpFactory } = require('sn-http-request');","import type { SnHttpClientInstance } from 'sn-http-request';"],"auth":{"required":false,"env_vars":[]},"links":{"homepage":null,"github":null,"docs":null,"changelog":null,"pypi":null,"npm":"https://www.npmjs.com/package/sn-http-request","openapi_spec":null,"status_page":null,"smithery":null},"quickstart":{"code":"import { snHttpFactory } from 'sn-http-request';\n\nconst SN_INSTANCE_URL = process.env.SN_INSTANCE_URL ?? 'https://yourinstance.service-now.com';\nconst SN_USERNAME = process.env.SN_USERNAME ?? 'admin';\nconst SN_PASSWORD = process.env.SN_PASSWORD ?? 'password';\n\nconst snHttp = snHttpFactory({\n  baseURL: SN_INSTANCE_URL + '/api/now/table/',\n  auth: {\n    username: SN_USERNAME,\n    password: SN_PASSWORD,\n  },\n  // Max 2 concurrent requests by default, set to 5 for demonstration\n  maxConcurrent: 5,\n  // Disable batching for this client instance for immediate requests\n  batching: false,\n  // Example interceptor to log requests\n  interceptors: {\n    request: [\n      (config) => {\n        console.log(`[Request Interceptor] Making ${config.method?.toUpperCase()} request to ${config.url}`);\n        return config;\n      },\n    ],\n    response: [\n      (response) => {\n        console.log(`[Response Interceptor] Received ${response.status} from ${response.config.url}`);\n        return response;\n      },\n    ],\n  },\n});\n\nasync function fetchIncident(sysId: string) {\n  try {\n    console.log(`\nFetching incident with sys_id: ${sysId}...`);\n    const response = await snHttp.get(`incident/${sysId}`);\n    console.log('Incident Data:', JSON.stringify(response.data, null, 2));\n  } catch (error: any) {\n    console.error('Error fetching incident:', error.message || error);\n    if (error.response) {\n      console.error('Response status:', error.response.status);\n      console.error('Response data:', error.response.data);\n    }\n  }\n}\n\n// Example: Replace with a valid incident sys_id from your ServiceNow instance\nfetchIncident('a_valid_incident_sys_id_here');\n\n// Example: Creating a new incident (requires POST permissions and appropriate payload)\nasync function createIncident() {\n  try {\n    console.log('\\nCreating a new incident...');\n    const newIncidentData = {\n      short_description: 'Test incident from sn-http-request client',\n      impact: '3',\n      urgency: '3',\n    };\n    const response = await snHttp.post('incident', { data: newIncidentData });\n    console.log('New Incident Created:', JSON.stringify(response.data, null, 2));\n  } catch (error: any) {\n    console.error('Error creating incident:', error.message || error);\n    if (error.response) {\n      console.error('Response status:', error.response.status);\n      console.error('Response data:', error.response.data);\n    }\n  }\n}\n\n// Uncomment to run the create incident example\n// createIncident();","lang":"typescript","description":"This quickstart demonstrates how to create an `sn-http-request` client instance, configure it with base URL, authentication, and interceptors, and then perform a GET request to fetch a ServiceNow incident. It also includes an example for creating a new incident via a POST request.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}