how to use service now rest api in code for authentication, ticket raising, and gettting status back
To use the ServiceNow REST API in code for authentication, ticket raising, and getting status back, you would typically follow these steps:
Authentication: Before you can interact with the ServiceNow API, you need authenticate. This generally involves providing a username and password, and possibly other security credentials.
const sn = require('servicenow-rest-api'); const ServiceNow = sn('INSTANCE', 'USERID', 'PASSWORD'); ServiceNow.Authenticate().then(function(response){ console.log(response.status); // Authentication status });
Replace 'INSTANCE'
, 'USERID'
, and 'PASSWORD'
with your ServiceNow instance name and your credentials.
Raising a Ticket (Creating an Incident): Once authenticated, you can create a ticket (incident) by making a POST to the table API.
// Define the data for the new incident const incidentData = { short_description: 'Short description of the issue', urgency: '1', // 1 - High, 2 - Medium, 3 - Low : '1', // 1 - High, 2 - Medium, 3 - Low description: 'Full description of the issue', // ... You can other fields as required }; // Function to create a new incident ServiceNow.createNewTask('incident', incidentData, function(result){ console.log(result); // Log the response which includes details of the created incident });
Getting the Status of a Ticket: To get the of a ticket, you can make a GET request, either to list all tickets or to retrieve a specific ticket by its sys_id.
For a specific ticket:
const sysId = 'YOUR_TICKET_SYS_ID'; // Replace with your ticket's sys_id // Function to get the data a specific incident using sys_id ServiceNow.getTableData('incident', sysId, function(result){ console.log(result); // Log the status of the incident });
`
To retrieve all incidents:
Function to get data from the 'incident' table ServiceNow.getTableData('incident', '',(result){ console.log(result); // Log all the incidents });
Notes:
npm install servicenow-rest-api
getTableData
method generally requires additional parameters to filter and specify the result set you to retrieve; make sure you consult the ServiceNow API documentation for details on the query parameters you can use.github.com
manageengine.com
support.sap.com
provide this code in python
Here is an example of how you might use Python to work with the ServiceNow REST API for the tasks of authentication, ticket raising (creating an incident), and getting the status of the ticket. This example uses the requests
library for HTTP requests, which you need to install using pip install requests
if you haven't already.
Please replace your_instance
, your_username
, and your_password
with your actual ServiceNow instance and credentials.
requests from requests.auth import HTTPBasicAuth # Replace these variables with your ServiceNow instance and credentials instance = 'your_instance' user = 'your_username' pwd = 'your_password' # Set the request parameters url = f'https://{instance}.service-now.com/api/now/table/incident' # Set proper headers headers = { "Content-Type": "application/json", "Accept": "application/json" } # Do the HTTP request for authentication response = requests.get(url, auth=HTTPBasicAuth(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) # Example payload for a new ticket new_incident_data = { "short_description": "This is a test incident", "category": "software", } # Making a POST request to create a new incident response = requests.post(url, auth=HTTPBasicAuth(user, pwd), headers=headers, json=new_incident_data) if response.status_code != 201: print('Status:', response.status_code, 'Headers:', response.headers, 'Error Response:', response.json()) exit() # Decode the JSON response for the new incident new_incident = response.json() print(new_incident) incident_sys_id = new_incident['result']['sys_id'] # Getting the status of the created incident using its sys_id status_url = f'https://{instance}.service-now.com/api/now/table/incident/{incident_sys_id}' response = requests.get(status_url, auth=HTTPBasicAuth(user, pwd), headers=headers) if response.status_code != 200: print('Status:', response.status_code, 'Headers:', response.headers, 'Error Response:', response.json()) exit() incident_status = response.json() print(incident_status)
This script does the following:
short_description
and category
. You would need to modify this payload based on what fields are mandatory and used in your ServiceNow environment.sys_id
.Make sure your account has the necessary permissions to perform these actions, and for production code, ensure you handle exceptions and errors appropriately.
docs.datadoghq.com
merge.dev
manageengine.com