logo

NJP

Python integration to ServiceNow

Import · Mar 04, 2021 · article

############Python script to create incident##############

#Need to install requests package for python

easy_install requests

import requests

# Set the request parameters

url = 'https://devXXXX.service-now.com/api/now/table/incident'

# Eg. User name="admin", Password="admin" for this code sample.

user = ‘generic’

pwd = ‘******’

# 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="{\"short_description\":\"test\"}")

# 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)

Python script to read data

Install ServiceNow Python library : servicenow · PyPI

pip install servicenow

#########Python script to read data

Endpoint url =

REST API to read ServiceNow data

Need to install requests package for python

easy_install requests

import requests

import json

def buildUrl (instanceName, api, table, query, limit):

url = 'https://'+ instanceName + '.service-now.com' + api + table + "?" + "sysparm_query=" + query + "&sysparm_limit=" + limit

return url

def readServiceNowData(url,username,password):

# Set proper headers

headers = {"Content-Type":"application/json","Accept":"application/json"}

print (url + " " + username + " " + password)

# Do the HTTP request

response = requests.get(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()

result = {}

#responseJSON = response.json()['result']

responseJSON = response.json()['result']

return responseJSON

# Set the request parameters

url = 'https://dev63486.service-now.com/api/now/table/incident?sysparm\_limit=10'

instanceName = 'dev63486'

api = '/api/now/table/'

table = 'incident'

query = 'active%3Dtrue%5Estate%3D2'

query = 'active=truestate=2'

limit = '100'

url = buildUrl(instanceName,api,table,query,limit)

print(url)

# Eg. User name="admin", Password="admin" for this code sample.

user = 'admin'

pwd = '****'

responseJSON = readServiceNowData(url,user,pwd)

for item in responseJSON:

print(str(item['number']))

View original source

https://www.servicenow.com/community/now-platform-articles/python-integration-to-servicenow/ta-p/2295437