ServiceNow® API

See how easily Chime can provide enterprise click to chat leveraging ServiceNow®.

Request a Demo » Start your free trial »

Getting Started with the ServiceNow® REST API

Note: The following steps can be downloaded here » ServiceNowIntegrationDoc.docx

Before we get started...Check your references!

The following examples use these references. These examples use Newtonsoft Json library to serialize the objects we send to the ServiceNow API in json.

                    
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Xml.Linq;
                    
                

1. Authenticate with ServiceNow (create a basic authentication header for subsequent HTTP requests)

This code snippet shows you how to use your ServiceNow credentials to construct an authentication header for making basically authenticated HTTP requests to the ServiceNow REST API. The ServiceNow REST API supports basic authentication and OAuth. You can then use a dummy query to ServiceNow to verify that your authentication header is valid. The dummy query in this case asks for the first ten records from the Incident table, this is a default ServiceNow table.

                    
authHeaders = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.UTF8.GetBytes(username + ":" + password)));
    public void ConnectToServiceNow()
    {
        using (var client = new HttpClient())
        {
            client.DefaultRequestHeaders.Authorization = authHeaders;

            string connectURL = instance_url + @"incident?sysparm_limit=10";
            var response = client.GetAsync(connectURL).Result;
            if (response.IsSuccessStatusCode)
            {
                isConnectedToServiceNow = true;
                Console.WriteLine("Success connecting");
            }
            else
            {
                isConnectedToServiceNow = false;
            }
        }
    }
                    
                

2. Create a ticket in ServiceNow

This example shows you how to create an incident record (ticket) in ServiceNow via their REST API, populate some fields, and then construct a URL to the newly created ticket. We use the Newtonsoft Json library to serialize a local Incident object for making the Post request. For Post requests, ServiceNow requires the accept header and the content-type header to be “application/json”.

                    
public class Incident
{
    public string short_description;
    public string work_notes;
}
    public void CreateTicket(string email, string question)
    {
        string createIncidentURL = instance_url + @"incident";
        Incident newIncident = new Incident() { short_description = question, work_notes = "Incident reported by " + email};
        var jsonString = JsonConvert.SerializeObject(newIncident);
        using (var client = new HttpClient())
        {
            client.DefaultRequestHeaders.Authorization = authHeaders;
            client.DefaultRequestHeaders.Add("Accept", "application/json");
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, createIncidentURL);
            request.Content = new StringContent(jsonString, Encoding.UTF8, "application/json");
            var response = client.SendAsync(request).Result;
            if (response.IsSuccessStatusCode)
            {
                var newIncidentURL = response.Headers.Location.OriginalString;
            }
        }
    }
                    
                

3. Update a ServiceNow ticket

This example shows how you can update a record in ServiceNow, given that record’s sys_id. This sample will update the comment field of an Incident record, given that record’s sys_id.

                    
public class UpdateIncident
{
    public string comments;
}

    public void UpdateTicket(string comment, string sys_id)
    {
        UpdateIncident updateIncident = new UpdateIncident(){ comments = comment};
        var jsonString = JsonConvert.SerializeObject(updateIncident);
        string updateIncidentURL = instance_url + @"incident/" + sys_id + "?sysparm_exclude_ref_link=true";
        using (var client = new HttpClient())
        {
            client.DefaultRequestHeaders.Authorization = authHeaders;
            client.DefaultRequestHeaders.Add("Accept", "application/json");
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Put, updateIncidentURL);
            request.Content = new StringContent(jsonString, Encoding.UTF8, "application/json");
            var response = client.SendAsync(request).Result;
            if (response.IsSuccessStatusCode)
            {
                Console.WriteLine("Success updating ticket");
            }
            else
            {
                Console.WriteLine("Unsuccessfully updated ticket");
            }
        }
    }
                    
                

Questions?

Have a question about Chime integration with ServiceNow®? Ask one of our developers at development@instant-tech.com.