ServiceNow HTTP Request Client

24.0.3 · active · verified Wed Apr 22

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.

Common errors

Warnings

Install

Imports

Quickstart

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.

import { snHttpFactory } from 'sn-http-request';

const SN_INSTANCE_URL = process.env.SN_INSTANCE_URL ?? 'https://yourinstance.service-now.com';
const SN_USERNAME = process.env.SN_USERNAME ?? 'admin';
const SN_PASSWORD = process.env.SN_PASSWORD ?? 'password';

const snHttp = snHttpFactory({
  baseURL: SN_INSTANCE_URL + '/api/now/table/',
  auth: {
    username: SN_USERNAME,
    password: SN_PASSWORD,
  },
  // Max 2 concurrent requests by default, set to 5 for demonstration
  maxConcurrent: 5,
  // Disable batching for this client instance for immediate requests
  batching: false,
  // Example interceptor to log requests
  interceptors: {
    request: [
      (config) => {
        console.log(`[Request Interceptor] Making ${config.method?.toUpperCase()} request to ${config.url}`);
        return config;
      },
    ],
    response: [
      (response) => {
        console.log(`[Response Interceptor] Received ${response.status} from ${response.config.url}`);
        return response;
      },
    ],
  },
});

async function fetchIncident(sysId: string) {
  try {
    console.log(`
Fetching incident with sys_id: ${sysId}...`);
    const response = await snHttp.get(`incident/${sysId}`);
    console.log('Incident Data:', JSON.stringify(response.data, null, 2));
  } catch (error: any) {
    console.error('Error fetching incident:', error.message || error);
    if (error.response) {
      console.error('Response status:', error.response.status);
      console.error('Response data:', error.response.data);
    }
  }
}

// Example: Replace with a valid incident sys_id from your ServiceNow instance
fetchIncident('a_valid_incident_sys_id_here');

// Example: Creating a new incident (requires POST permissions and appropriate payload)
async function createIncident() {
  try {
    console.log('\nCreating a new incident...');
    const newIncidentData = {
      short_description: 'Test incident from sn-http-request client',
      impact: '3',
      urgency: '3',
    };
    const response = await snHttp.post('incident', { data: newIncidentData });
    console.log('New Incident Created:', JSON.stringify(response.data, null, 2));
  } catch (error: any) {
    console.error('Error creating incident:', error.message || error);
    if (error.response) {
      console.error('Response status:', error.response.status);
      console.error('Response data:', error.response.data);
    }
  }
}

// Uncomment to run the create incident example
// createIncident();

view raw JSON →