servicenowpy

PyPI package License Downloads

servicenowpy is a library that helps you consume the ServiceNow Table API.

Github Repository | Contributing

User guide

Installation

Install servicenowpy using pip:

pip install servicenowpy

servicenowpy completely depends on the great Requests library.

Examples

Basic usage

The simplest piece of code you can write with servicenowpy:

from servicenowpy import Client

sn_client = Client('instance.service-now.com', 'user', 'password')
inc_table = sn_client.table('incident')
inc_records = inc_table.get()

Getting large datasets

You can get massive datasets using servicenowpy with threads, for example:

from concurrent.futures import ThreadPoolExecutor
from servicenowpy import Client

sn_client = Client('instance.service-now.com', 'user', 'password')

def get_table_records(table_name):
        table_obj = sn_client.table(table_name)
        return table_obj.get()

with ThreadPoolExecutor() as executor:
    inc_future = executor.submit(get_table_records, 'incident')
    ritm_future = executor.submit(get_table_records, 'sc_req_item')

    inc_records = inc_future.result()
    ritm_records = ritm_future.result()

If you are using pandas, for example:

import pandas as pd

inc_df = pd.DataFrame(inc_records)

Query params

Query parameters must be passed as follows:

records = inc_table.get(
    sysparm_fields="number,short_description",
    sysparm_display_value="true"
)

API Reference

Client objects

class servicenowpy.servicenow.Client

Represents a ServiceNow instance.

Parameters
  • instance_url – ServiceNow instance URL.

  • user – Instance user.

  • pwd – Instance password.

make_api_url(instance_url)

Returns instance URL with ‘/api/now/’ appended.

table(table)

Returns servicenowpy.Table object.

Parameters

table – The table name.

Return type

servicenowpy.Table

Table objects

class servicenowpy.servicenow.Table

Represents ServiceNow’s Table API.

Parameters
  • table – The table name.

  • instance_url – ServiceNow instance URL”.

  • credentials – Tuple containing user and password, respectively.

get(api_version=None, headers={'Accept': 'application/json'}, verbose=False, **kwargs)

Sends a GET request to the instance table.

Parameters
  • api_version – API version, if API versioning is enabled.

  • headers – Request headers.

  • verbose – If set to True, prints the full URL before it sends the request.

  • **kwargs

    All query parameters to the URL.

Return type

list

get_record(sys_id: str, api_version=None, headers={'Accept': 'application/json'}, verbose=False, **kwargs)

Sends a GET request to the instance table. Returns only one record, with the given sys_id.

Parameters
  • sys_id – Record unique ID.

  • api_version – API version, if API versioning is enabled.

  • headers – Request headers.

  • verbose – If set to True, prints the full URL before it sends the request.

  • **kwargs

    All query parameters to the URL.

Return type

dict

get_record_by_number(number: str, api_version=None, headers={'Accept': 'application/json'}, verbose=False, **kwargs)

Sends a GET request to the instance table. Adds ‘number’ to the query parameters.

Parameters
  • number – Record number.

  • api_version – API version, if API versioning is enabled.

  • headers – Request headers.

  • verbose – If set to True, prints the full URL before it sends the request.

  • **kwargs

    Other query parameters to the URL.

Return type

list

patch(sys_id: str, data: dict, api_version=None, headers={'Accept': 'application/json'}, verbose=False, **kwargs)

Sends a PATCH request to the instance table.

Parameters
  • sys_id – Record unique ID.

  • data – Fields and values to update in the record.

  • api_version – API version (if API versioning is enabled).

  • headers – Request headers.

  • verbose – If set to True, prints the full URL before it sends the request.

  • **kwargs

    All query parameters to the URL.

Return type

dict

post(data: dict, api_version=None, headers={'Accept': 'application/json'}, verbose=False, **kwargs)

Sends a POST request to the instance table.

Parameters
  • data – Fields and values to the new record.

  • api_version – API version (if API versioning is enabled).

  • headers – Request headers.

  • verbose – If set to True, prints the full URL before it sends the request.

  • **kwargs

    All query parameters to the URL.

Return type

dict

put(sys_id: str, data: dict, api_version=None, headers={'Accept': 'application/json'}, verbose=False, **kwargs)

Sends a PUT request to the instance table.

Parameters
  • sys_id – Record unique ID.

  • data – New values to the record.

  • api_version – API version (if API versioning is enabled).

  • headers – Request headers.

  • verbose – If set to True, prints the full URL before it sends the request.

  • **kwargs

    All query parameters to the URL.

Return type

dict

delete(sys_id: str, api_version=None, headers={'Accept': 'application/json'}, verbose=False, **kwargs)

Sends a DELETE request to the instance table.

Parameters
  • sys_id – Record unique ID.

  • api_version – API version (if API versioning is enabled).

  • headers – Request headers.

  • verbose – If set to True, prints the full URL before it sends the request.

  • **kwargs

    All query parameters to the URL.

get_session(headers)

Returns a requests.Session object.

Parameters

headers – Request headers.

check_status_code(response: requests.models.Response, expected=200)

Checks if the given response status code is as expected. Raises a StatusCodeError if it is not.

Parameters
  • response – Response object.

  • expected – Expected status code.

make_url(api_version=None, sys_id=None, **kwargs)

Returns a complete url to send in the request.

Return type

str