You are currently viewing Create Request via REST API -Service Catalog API Demo

Create Request via REST API -Service Catalog API Demo

Using the OOB ServiceNow APIs, you can create a ServiceNow request easily. This we can do via ServiceNow’s REST API Explorer.

In this post, I will demonstrate the process of creating or submitting a request via the Service Catalog API

[youtube-feed feed=1][youtube-feed feed=3][youtube-feed feed=4]
My ServiceNow YouTube Channels

Steps To Create RITM Using API

  • Go to System Web Service -> REST API Explorer
  • In API Explorer, set
    • Namespace=sn_sc
    • API Name: Service Catalog API
    • API Version=Latest
  • Scroll down and click on Buy Item (POST) method
  • Now under Prepare Request->Path parameters, provide the sys_id of the catalog item to be ordered.
  • Set the value of Authorization as Send as me in case you are using your admin account to authenticate
  • Otherwise, add the authorization header values by clicking the Add Header button
  • Now provide the Request Body values as Raw and add the below values according to your catalogue item and its variables
    • sysparm_id– sysID of catalogue item
    • sysparm_quantity– number of items to be ordered
    • variables -provide values to variables comma separated one by one
{
        'sysparm_id': '186d917a6fab7980575967ddbb3ee4f2',
        'sysparm_quantity': '1',
        'variables':{  
        'new_email': 'him@op.com' 
        }
}
  • Click On Send.
  • The request was Successfully Submitted and received a response of 200 (OK)
  • The response body has details about the newly created request.

Sample Script To Use Service Catalog API In ServiceNow

Use the provided code samples to send this request from commonly used languages.

//Request
var request = new sn_ws.RESTMessageV2();
request.setEndpoint('https://dev95794.service-now.com/api/sn_sc/servicecatalog/items/186d917a6fab7980575967ddbb3ee4f2/order_now');
request.setHttpMethod('POST');
// Authentication
var user = 'admin';
var password = 'your password';
request.setBasicAuth(user,password);
request.setRequestHeader("Accept","application/json");
request.setRequestHeader('Content-Type','application/json');
request.setRequestBody("{'sysparm_id': '186d917a6fab7980575967ddbb3ee4f2','sysparm_quantity': '1','variables':{'new_email': 'him@op.com'}}");
//Response
var response = request.execute();
gs.print("body - "+response.getBody());

Sample Script To Use Service Catalog API In Python

#Need to install requests package for python
#easy_install requests
import requests
# Set the request parameters
url = 'https://dev95794.service-now.com/api/sn_sc/servicecatalog/items/186d917a6fab7980575967ddbb3ee4f2/order_now'
# Eg. User name="admin", Password="admin" for this code sample.
user = 'admin'
pwd = 'admin'
# Set proper headers
headers = {"Content-Type":"application/json","Accept":"application/json"}
# Do the HTTP request
response = requests.post(url, auth=(user, pwd), headers=headers ,data="{
        'sysparm_id': '186d917a6fab7980575967ddbb3ee4f2',
        'sysparm_quantity': '1',
        'variables':{
        'new_email': 'him@op.com'
        }
}")
# Check for HTTP codes other than 200
if response.status_code != 200: 
    print('Status:', response.status_code, 'Headers:', response.headers, 'Error Response:',response.json())
    exit()
# Decode the JSON response into a dictionary and use the data
data = response.json()
print(data)

Video Tutorial

Leave a Reply