pysnow: ServiceNow HTTP Client Library

0.7.17 · maintenance · verified Thu Apr 16

pysnow is a Python library for interacting with the ServiceNow REST API, emphasizing ease of use, simple code, and elegant syntax. It supports both Python 2 and 3. As of version 0.7.17, the library is in a stable maintenance mode, meaning essential fixes are applied, but new feature development has shifted to `aiosnow`, an asynchronous counterpart.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the `pysnow` client, create a resource for the incident table, and fetch the first record matching a specific query. It includes basic error handling for common `pysnow` exceptions.

import os
import pysnow

# Configure ServiceNow instance details from environment variables or provide directly
instance = os.environ.get('SNOW_INSTANCE', 'your_instance_name') # e.g., 'dev12345'
user = os.environ.get('SNOW_USER', 'your_username')
password = os.environ.get('SNOW_PASSWORD', 'your_password')

if not all([instance, user, password]):
    print("Please set SNOW_INSTANCE, SNOW_USER, and SNOW_PASSWORD environment variables or provide them directly.")
else:
    try:
        # Create a client object
        client = pysnow.Client(instance=instance, user=user, password=password)

        # Define a resource, e.g., the incident table API
        incident = client.resource(api_path='/table/incident')

        # Query for incidents with state 1 (e.g., New) and print the first one
        response = incident.get(query={'state': 1})
        first_incident = response.first_or_none()

        if first_incident:
            print(f"Found incident: {first_incident['number']} - {first_incident['short_description']}")
        else:
            print("No incidents found with state 1.")

    except pysnow.exceptions.InvalidUsage as e:
        print(f"Client initialization error: {e}")
    except pysnow.exceptions.NoResults:
        print("No results returned for the query (this is expected if raise_on_empty is True and no results).")
    except pysnow.exceptions.ResponseError as e:
        print(f"ServiceNow API error: {e.error['message']} - {e.error['detail']}")
    except Exception as e:
        print(f"An unexpected error occurred: {e}")

view raw JSON →