Using Python With ServiceNow
You can use Python to integrate directly with ServiceNow.
So you can do things like create incidents, delete change requests, get all your SLA’s, etc.
It should be known that you don’t write Python “in” ServiceNow, as that’s not supported.
While you can technically have a Powershell script write to your MID Server, which can then execute a python script, that’s a lot more technical and complicated and not what we’re covering in this article.
But if you’re using your computer, you can install Python and then from you computer, you can run all sorts of API methods to your ServiceNow instance (GET, PUT, POST, DELETE, etc.)
If you come from a Python background, this article is geared towards yourself.
ServiceNow has a ton of JavaScript related support, so it’s great to see more people using other scripting languages to integrate directly with ServiceNow.
NOTE: The only pre-requisite here would be that you already have Python installed on your computer.
Let’s talk a little bit about why you should consider using Python with ServiceNow, and then we’ll cover 2 examples, a GET request and a PUT request.
Benefits of Using Python With ServiceNow
There are several benefits to using Python to make API calls to ServiceNow, including the following:
- Python is a versatile and powerful programming language that is widely used for data analysis, scientific computing, and web development. This means that it is well-suited for working with the data and functionality exposed by ServiceNow APIs.
- Python has a large and active community, with many libraries and frameworks available for working with APIs, including the popular
requests
module used in the example above. This makes it easy to find resources and support for working with ServiceNow APIs in Python. - Python is easy to learn and use, even for those who are new to programming. Its simple, readable syntax and rich set of libraries and frameworks make it a good choice for quickly and easily building applications that integrate with ServiceNow.
- Python is cross-platform and can be run on any operating system, including Windows, Linux, and MacOS. This means that you can use it to develop applications that can be deployed on a wide range of platforms and environments.
Overall, using Python to make API calls to ServiceNow can provide a number of benefits, including the ability to easily integrate with ServiceNow, access to a rich ecosystem of tools and libraries, and the ability to develop and deploy applications on a wide range of platforms.
You’ll have to substitute some data in the below scripts, like your instance name and user name and password, which you should never share.
Make sure that you never post your user name and password anywhere online, and that you take the proper precautions when using the below scripts.
Get An Incident – Python Example
This is the simpler of the 2, as you’re not modifying any data – you’re just printing it out.
To make an API call to ServiceNow using Python, you will need to use the requests
module to send an HTTP request to the ServiceNow API endpoint. The endpoint will be specific to the information you are trying to retrieve or modify, and the request method (e.g. GET, POST, PUT) will depend on the type of operation you want to perform.
Here is an example of how you might make a GET request to the ServiceNow API to retrieve a list of records:
import requests
# Set the request parameters
api_url = 'https://your_instance.service-now.com/api/now/table/incident'
user = 'your_username'
pwd = 'your_password'
# Set proper headers
headers = {"Content-Type":"application/json","Accept":"application/json"}
# Do the HTTP request
response = requests.get(api_url, auth=(user, pwd), headers=headers )
# 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)
In this example, we are using the requests.get()
method to send a GET request to the incident
table in ServiceNow to retrieve a list of records. The auth
parameter is used to provide the username and password for authenticating the API call, and the headers
parameter is used to specify the content type and accept type for the request.
This is a super simple example and because we’re using a GET request, it’s a low risk way to connect to your instance, as we’re not modifying any data, we’re just retrieving it.
Before plugging this into your system and trying it, we’d first request that you do this in a sub-production instance and that you have a working knowledge of Python.
If the request is successful, the response will be a JSON object containing the data from the ServiceNow table. You can then use this data in your Python code as needed.
Update An Incident – Python Example
To update an incident in ServiceNow using a Python script, you will need to use the requests
module to send an HTTP PUT request to the ServiceNow API endpoint for the incident table. The request will include the updated field values for the incident, encoded as JSON in the request body.
Here is an example of how you might update an incident in ServiceNow using a Python script:
import requests
# Set the request parameters
api_url = 'https://your_instance.service-now.com/api/now/table/incident/{incident_id}'
user = 'your_username'
pwd = 'your_password'
# Set the updated field values for the incident
incident_data = {
'short_description': 'Updated short description',
'description': 'Updated description',
'priority': '1',
}
# Set proper headers
headers = {"Content-Type":"application/json","Accept":"application/json"}
# Do the HTTP PUT request
response = requests.put(api_url, auth=(user, pwd), headers=headers, json=incident_data)
# 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)
Remember that you’ll have to update some of the code above so it actually works.
In this example, we are using the requests.put()
method to send a PUT request to the incident
table in ServiceNow, specifying the ID of the incident to be updated. The auth
parameter is used to provide the username and password for authenticating the API call, the headers
parameter is used to specify the content type and accept type for the request, and the json
parameter is used to encode the updated field values for the incident as JSON in the request body.
If the request is successful, the response will be a JSON object containing the updated data for the incident. You can then use this data in your Python code as needed.
Since this example is actually updating data, we’d recommend that you do it in a PDI and never directly into your Production instance.
What sort of setup do you have locally on your work computer to integrate with ServiceNow?
Do you use Python, JavaScript or another language?