ZenPy: Python Wrapper for Zendesk API

2.0.57 · active · verified Thu Apr 09

ZenPy is an actively maintained Python wrapper for the Zendesk Support, Chat, and Help Center APIs. It simplifies programmatic interaction with Zendesk, offering a Pythonic interface, object caching, and lazy attribute evaluation to minimize API calls. The library is currently at version 2.0.57 and sees frequent updates, typically on a monthly or bi-monthly basis.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the ZenPy client using API token authentication (recommended) and then create and retrieve a basic Zendesk ticket. Ensure `ZENDESK_EMAIL`, `ZENDESK_API_TOKEN`, and `ZENDESK_SUBDOMAIN` environment variables are set.

import os
from zenpy import Zenpy
from zenpy.lib.api_objects import Ticket

# Authenticate using environment variables (API Token is recommended)
creds = {
    'email': os.environ.get('ZENDESK_EMAIL', 'your_email@example.com'),
    'token': os.environ.get('ZENDESK_API_TOKEN', 'your_zendesk_api_token'),
    'subdomain': os.environ.get('ZENDESK_SUBDOMAIN', 'your_subdomain')
}

# Create a Zenpy instance
try:
    zenpy_client = Zenpy(**creds)
    print("Successfully connected to Zendesk.")

    # Create a new ticket
    new_ticket = zenpy_client.tickets.create(
        Ticket(
            subject="Urgent: Coffee Machine Broken!",
            description="The office coffee machine is completely out of order."
        )
    )
    print(f"Created ticket with ID: {new_ticket.id}")

    # Retrieve and print a ticket's subject
    retrieved_ticket = zenpy_client.tickets(id=new_ticket.id)
    print(f"Retrieved ticket subject: {retrieved_ticket.subject}")

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

view raw JSON →