In this blog post we will explore how to order items from a ServiceNow catalog using the ServiceNow REST API. There are a few examples out there that use “REST API Explorer” inside ServiceNow. I will focus here on how to do it from an external tool. In particular I am going to provide instructions for Postman. This post is the stepping stone for this follow up post where I try to solve an interesting use case of providing multi-cloud with governance through ServiceNow. You can get the Postman collection I am using from this GitHub repo.
This is the latest addition to a series of ServiceNow related posts:
There is also a follow up post that uses what I am covering here to allow developers to provision multi-cloud IT resources while observing any governance that might have been implemented in ServiceNow.
In the post about creating Incidents we covered the “REST API Explorer
” tool in more detail. Please refer to that one if you are new to it. In this post we will use the “Service Catalog API
“. In this screenshot you can see it is a very extensive API. It provides 47 API calls as of time of this writing.
Postman
Now let’s move to Postman. Feel free to use any other REST API developer tool you are used to. If you want to follow along you can retrieve my Postman collection from this GitHub repo. The Postman collection provides 3 folders. In this post we will use the “Catalog Request” folder. The other 2 folders contain the API calls that were used in 2 previous posts in this series mentioned above.
The process of creating a request in a ServiceNow catalog is a 2-step process:
- add an item to the cart
- submit the cart
Adding an item to the cart is done with the following API call. Please replace {instance}
with your particular ServiceNow instance.
POST https://{instance}.service-now.com/api/sn_sc/servicecatalog/items/{sys_id}/add_to_cart
There are 3 things you need to include:
- “
sys_id
“. This is the “sys_id” of the item itself. As you can see it needs to be included in the URL - “
sysparm_quantity
“. This is the quantity of this item you want to add to the cart. It is included in the “Body” parameter and it is required - “
variables
“. This is also in the “Body” and needs to include all the information that you would normally provide through the GUI when creating the request. In my case this catalog entry allows me to request the installation of an application on a virtual machine. At a minimum you will need to include all variables that are defined as “mandatory” in your catalog. In my example below I am going to request the installation of an application on an existing virtual machine. You can see I am requesting the installation of Apache web server on a VM called “albtest3_01” which lives in my “sin” datacenter
If all goes well, you should get a status code of 200.
You can find out the “sys_id” of the catalog item by browsing to the item itself in your catalog. It will be included in the URL itself as seen here
Alternatively you can use other calls in the “Service Catalog API” to get it programmatically. One example could be:
GET https://{instance}.service-now.com/api/sn_sc/servicecatalog/items
You can also find out programmatically what variables you need to include with this API call. It returns information about every parameter in that item’s catalog. Use the “name
” fields as the keys in your “Body” parameter. Also pay attention to whether variables show as “mandatory: true
“.
GET https://{instance}.service-now.com/api/sn_sc/servicecatalog/items/{sys_id}/variables
At this point you have an item in the cart. You could potentially add other items to the cart but if you need things to be ordered in a certain sequence order 1 item at a time and orchestrate them all in your Infrastructure as Code tool of choice. The final step is to “submit the order”. This is done with the following API call
POST https://{instance}.service-now.com/api/sn_sc/servicecatalog/cart/submit_order
This API call is also part of the Postman collection you downloaded. As you can see in the screenshot below, even though this is a POST call, it doesn’t require a “Body” payload. If successful you should get a status code of 200 and the “Body” of the response will contain the “requests number”.
Next steps
Postman allows you to generate code in your language of choice, ex: Python, Golang, etc. You can use this to include what we have covered into a larger script that runs additional tasks.
Alternatively, you might want to use configuration management or provisioning tools like Ansible or Terraform to order items from the ServiceNow catalog.
- Ansible provides the URI module to interact with web services. As discussed above make sure the URL includes the “sys_id” and that the “Body” includes the quantity and the variables that are mandatory in that catalog
- In Terraform you could use the “local-exec” provisioner to send REST API calls to ServiceNow using “cURL” or a more specialized provider like this one
Categories: DellEMC
1 reply »