Python SNC (ServiceNow) API

1.2.1 · active · verified Thu Apr 16

PySNC is a Python interface for the ServiceNow REST and Table APIs, designed to mimic the familiar GlideRecord interface with Pythonic support. It provides robust features for interacting with ServiceNow instances, including synchronous and asynchronous operations, OAuth 2.0 Client Credentials Grant Flow, mTLS support, and built-in retry mechanisms for common error codes. The library is actively maintained with a positive release cadence.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the `ServiceNowClient` using environment variables for credentials, create a `GlideRecord` object, add a query, execute it, and iterate through the results. It also shows how to fetch a single record by its `sys_id`.

import os
from pysnc import ServiceNowClient

# Configure environment variables for secure access
SN_INSTANCE = os.environ.get('SN_INSTANCE', 'your_instance.service-now.com')
SN_USERNAME = os.environ.get('SN_USERNAME', 'your_username')
SN_PASSWORD = os.environ.get('SN_PASSWORD', 'your_password')

if not all([SN_INSTANCE, SN_USERNAME, SN_PASSWORD]):
    print("Please set SN_INSTANCE, SN_USERNAME, and SN_PASSWORD environment variables.")
    exit(1)

try:
    # Initialize the ServiceNow client with basic authentication
    client = ServiceNowClient(SN_INSTANCE, (SN_USERNAME, SN_PASSWORD))

    # Create a GlideRecord object for the 'incident' table
    gr = client.GlideRecord('incident')

    # Add a query condition (e.g., active incidents)
    gr.add_query('active', 'true')
    gr.add_limit(5) # Limit results for demonstration

    # Execute the query
    gr.query()

    # Iterate through the results (Pythonic iteration is recommended)
    print(f"Found {gr.get_row_count()} active incidents:")
    for record in gr:
        print(f"  Incident Number: {record.number}, Short Description: {record.short_description}")

    # Example of getting a single record by sys_id
    if gr.get_row_count() > 0:
        first_sys_id = gr.next().sys_id # Using .next() here for specific record access
        single_record = client.GlideRecord('incident')
        if single_record.get(first_sys_id):
            print(f"\nRetrieved single incident: {single_record.number}")

except Exception as e:
    print(f"An error occurred: {e}")

view raw JSON →