zenpy 2.0.56


pip install zenpy

  Latest version

Released: Nov 21, 2024

Project Links

Meta
Author: Face Toe

Classifiers

Development Status
  • 5 - Production/Stable

Intended Audience
  • Developers
  • Information Technology
  • System Administrators

Programming Language
  • Python :: 2
  • Python :: 2.7
  • Python :: 3
  • Python :: 3.5
  • Python :: 3.6
  • Python :: 3.7
  • Python :: 3.8
  • Python :: 3.9

License
  • OSI Approved :: GNU General Public License v3 (GPLv3)

Natural Language
  • English

Build Status

Zenpy

Zenpy is a Python wrapper for the Zendesk, Chat and HelpCentre APIs. The goal of the project is to make it easy to write clean, fast, Pythonic code when interacting with Zendesk progmatically. The wrapper tries to keep API calls to a minimum. Wherever it makes sense objects are cached, and attributes of objects that would trigger an API call are evaluated lazily.

Zenpy supports both Python2 and Python3.

Please report bugs!

Quickstart

from zenpy import Zenpy
from zenpy.lib.api_objects import Ticket
# Create a Zenpy instance
zenpy_client = Zenpy(**credentials)

# Create a new ticket
zenpy_client.tickets.create(Ticket(subject="Important", description="Thing"))

# Perform a simple search
for ticket in zenpy_client.search('PC LOAD LETTER', type='ticket', assignee='facetoe'):
    # No need to mess around with ids, linked objects can be accessed directly.
    print(ticket.requester.name)

    # All objects can be converted to a Python dict.
    print(ticket.to_dict())

    # Or to JSON.
    print(ticket.to_json())

Examples

Searching open and pending tickets for a specific user and sort them by descending
zenpy_client.search(type='ticket', status_less_than='closed', assignee='foo@mail.foo', sort_order='desc')
Searching only opened tickets
zenpy_client.search(type='ticket', status='open')
Exporting all tickets matching the query

By default, Search API has a limit of 1000 results in total. Search Export API allows exporting unlimited number of results, so if you'd like to export all results, use this method instead:

for ticket in zenpy_client.search_export(type='ticket', status='open'):
    print(ticket)

Read more about these limitations:

Search results limits

Search Export API release notes

Creating a ticket with a different requester
from zenpy.lib.api_objects import Ticket, User

zenpy_client.tickets.create(
    Ticket(description='Some description',
           requester=User(name='bob', email='bob@example.com'))
)
Commenting on a ticket
from zenpy.lib.api_objects import Comment

ticket = zenpy_client.tickets(id=some_ticket_id)
ticket.comment = Comment(body="Important private comment", public=False)
zenpy_client.tickets.update(ticket)
Adding a HTML comment to a ticket
from zenpy.lib.api_objects import Ticket, Comment

zenpy_client.tickets.create(Ticket(
    subject='Html comment example',
    comment=Comment(body='The smoke is very colorful',
                    html_body='<h2>The smoke is <i>very</i> colourful</h2>'))
)
Appending tags to a ticket
from zenpy.lib.api_objects import Ticket

ticket = zenpy_client.tickets(id=some_ticket_id)
ticket.tags.extend(['onetag', 'twotag', 'threetag', 'four'])
zenpy_client.tickets.update(ticket)
Uploading an attachment
from zenpy.lib.api_objects import Comment

# Upload the file (or file-like object) to Zendesk and obtain an Upload instance
upload_instance = zenpy_client.attachments.upload('/tmp/awesome_file.txt')

ticket = zenpy_client.tickets(id=some_ticket_id)
ticket.comment = Comment(body='This comment has my file attached', uploads=[upload_instance.token])
zenpy_client.tickets.update(ticket)
Creating a comment attachment and then redacting it
# Upload the attachment
upload_instance = zenpy_client.attachments.upload('/tmp/awesome_file.txt')
comment = Comment(body='Some comment')
# Set the comment attachment affinity to this token.
comment.uploads = upload_instance.token

# Create the ticket, with that comment with that attachment affinity.  Can just as easily be a new comment on existing ticket.
ticket = Ticket(subject='example ticket', comment=comment)
ticket_audit = zenpy_client.tickets.create(ticket)
ticket = ticket_audit.ticket

# Get the last comment we just uploaded on that ticket.
the_commentresult = zenpy_client.tickets.comments(ticket)

# Redact the comment now that we just associated it with an attachment.
the_comment = the_commentresult.values[0]  
attachment =  zenpy_client.attachments.redact(ticket, the_comment, the_comment.attachments[0].id)

# Barring no errors, the attachment is an Attachment object with the same id as was passed in!
Creating a ticket with a custom field set
from zenpy.lib.api_objects import CustomField, Ticket

ticket_audit = zenpy_client.tickets.create(Ticket(
    subject='Has custom field',
    description="Wow, such field",
    custom_fields=[CustomField(id=43528467, value=1337)]
))
Updating a custom field on a ticket
from zenpy.lib.api_objects import CustomField
ticket = zenpy_client.tickets(id=some_ticket_id)
ticket.custom_fields.append(CustomField(id=43528467, value=1337))
zenpy_client.tickets.update(ticket)
Applying a Macro to a ticket
# Execute the show_macro_effect() method which returns what the macro *would* do.
# The method accepts either Zenpy objects or ids.
macro_result = zenpy_client.tickets.show_macro_effect(ticket_id_or_object, macro_id_or_object)

# Update the ticket to actually change the ticket.
zenpy_client.tickets.update(macro_result.ticket)
Adding a photo to a user
user = zenpy_client.users(id=user_id)
user.remote_photo_url = 'http://domain/example_photo.jpg'
zenpy_client.users.update(user)
List all categories from help center
categories = zenpy_client.help_center.categories()
for category in categories:
    pass
List all help center articles
articles = zenpy_client.help_center.articles(section=section)
for article in articles:
    pass
List all help center articles in a section
section = zenpy_client.help_center.categories.sections(category_id=category.id)
articles = zenpy_client.help_center.sections.articles(section=section)
for article in articles:
    pass
Create new category in help center
from zenpy import Zenpy
from zenpy.lib.api_objects.help_centre_objects import Category
new_category = zenpy_client.help_center.categories.create(
            Category(
                name="Category name",
                description="Category description",
                locale="en-us",
                created_at=datetime.now(),
                updated_at=datetime.now()
            )
        )
print(new_category.to_dict(serialize=True))
Create new section in help center
from zenpy import Zenpy
from zenpy.lib.api_objects.help_centre_objects import Section
new_section = zenpy_client.help_center.sections.create(
            Section(
                name="Section name",
                description="Section description",
                category_id=new_category.id,
                locale="en-us",
                created_at=datetime.now(),
                updated_at=datetime.now()
            )
        )
print(new_section.to_dict(serialize=True))
Create new article in help center
from zenpy import Zenpy
from zenpy.lib.api_objects.help_centre_objects import Article
new_article = zenpy_client.help_center.articles.create(
                    section=new_section.id,
                    article=Article(
                        name="Article Name",
                        body="<p>Article html content body</p>",
                        locale="en-us",
                        title="Article title",
                        section_id=new_section.id,
                        created_at=datetime.now(),
                        updated_at=datetime.now()
                    ),
                )
print(new_article.to_dict(serialize=True))
Working with webhooks
Show a webhook
webhook = zenpy_client.webhooks(id=WEBHOOK_ID) 
List webhooks
# Just list all the webhooks
for webhook in zenpy_client.webhooks.list():
    pass # Do something with it

# Filter the webhooks by a string in the name
for webhook in zenpy_client.webhooks.list(filter='some string'):
    pass # Do something with it

# Using sorting and pagination according to https://developer.zendesk.com/api-reference/event-connectors/webhooks/webhooks/#list-webhooks
zenpy_client.webhooks.list(sort='name')
zenpy_client.webhooks.list(page_before=X, page_size=Y)
zenpy_client.webhooks.list(page_after=N, page_size=Y)
Creating a webhook that uses basic authentication
from zenpy.lib.api_objects import Webhook

new_webhook = Webhook(
    authentication={
        "add_position": "header",
        "data": {
            "password": "hello_123",
            "username": "john_smith"
        },
        "type": "basic_auth"
    },
    endpoint="https://example.com/status/200",
    http_method="GET",
    name="Example Webhook",
    description="Webhook description",
    request_format="json",
    status="active",
    subscriptions=["conditional_ticket_events"],
) 
zenpy_client.webhooks.create(new_webhook)
Creating a webhook that uses no authentication
new_webhook = Webhook(
    endpoint="https://example.com/status/200",
    http_method="GET",
    name="Example Webhook",
    description="Webhook description",
    request_format="json",
    status="active",
    subscriptions=["conditional_ticket_events"],
) 
zenpy_client.webhooks.create(new_webhook)
Creating a webhook that uses bearer token authentication
new_webhook = Webhook(
    authentication={
        "add_position": "header",
        "data": {
            "token": "{{token}}"
        },
        "type": "bearer_token"
    },
    # other fields
) 
zenpy_client.webhooks.create(new_webhook)
Updating a webhook
from zenpy.lib.api_objects import Webhook

webhook = zenpy_client.webhooks(id=WEBHOOK_ID) 

# Note: We need a brand new object because of API specific requirements for 'update'
# https://developer.zendesk.com/api-reference/event-connectors/webhooks/webhooks/#update-webhook

new_webhook = Webhook(
                    name="New name",
                    request_format="json",
                    http_method="GET",
                    endpoint="https://example.com/status/200",
                    status="active",
                    authentication={
                      "add_position": "header",
                      "data": {
                          "password": "hello_123",     # As we can't get it back we need to pass it again from scratch
                          "username": "john_smith"
                      },
                      "type": "basic_auth"
                  },
)
response = zenpy_client.webhooks.update(webhook.id, new_webhook)
Partially updating (patching) a webhook
webhook = zenpy_client.webhooks(id=WEBHOOK_ID)
webhook.name = 'A new name'
response = zenpy_client.webhooks.patch(webhook)
Cloning a webhook
from zenpy.lib.api_objects import Webhook

an_existing_webhook = zenpy_client.webhooks(id=WEBHOOK_ID) 
new_webhook = zenpy_client.webhooks.clone(an_existing_webhook)

# Or just
new_webhook = zenpy_client.webhooks.clone(WEBHOOK_ID)
Working with secrets
secret = zenpy_client.webhooks.show_secret(webhook)
print(secret.secret)

secret = zenpy_client.webhooks.reset_secret(webhook)
print(secret.secret)
Testing webhooks
# Testing an existing webhook "as is""
response = zenpy_client.webhooks.test(webhook)

# Testing an existing webhook with modifications 
response = zenpy_client.webhooks.test(
                    webhook, 
                    request=dict(
                      endpoint='https://example.org/'
                    )
)

# Sending a test request without creating a webhook
response = zenpy_client.webhooks.test(
                    request=dict(
                        endpoint="https://example.org",
                        request_format="json",
                        http_method="GET",
                    )
                )
Getting a webhook invocations

API documentation

wh_filters = {
    'filter[from_ts]': '2023-12-04T12:00:00Z',
    'filter[to_ts]': '2023-12-04T16:00:00Z',
    'filter[status]': 'success',
}

for invocations in zenpy.webhooks.invocations(webhook_id, **wh_filters):
    pass
Pagination

Please refer to the official documentation to get details. Also check this article: Which endpoints are supported?

# An old style offset pagination, not recommended. Since August 15, 2023, is limited to 100 pages.
fields = zenpy_client.ticket_fields()
# Or
fields = zenpy_client.ticket_fields(cursor_pagination=False)

# A new cursor offset pagination
fields = zenpy_client.ticket_fields(cursor_pagination=True) # is equal to 100 results per page
# Or
fields = zenpy_client.ticket_fields(cursor_pagination=50) # 50 results per page

Documentation

Check out the documentation for more info.

Contributions

Contributions are very welcome. I've written an explanation of the core ideas of the wrapper in the Contributors Guide.

2.0.56 Nov 21, 2024
2.0.55 Nov 08, 2024
2.0.54 Oct 16, 2024
2.0.53 Sep 12, 2024
2.0.52 Sep 09, 2024
2.0.51 Sep 06, 2024
2.0.50 Aug 20, 2024
2.0.49 May 13, 2024
2.0.48 Apr 30, 2024
2.0.47 Feb 20, 2024
2.0.46 Jan 17, 2024
2.0.45 Dec 22, 2023
2.0.44 Dec 21, 2023
2.0.43 Dec 14, 2023
2.0.42 Dec 05, 2023
2.0.41 Oct 10, 2023
2.0.40 Sep 19, 2023
2.0.39 Sep 19, 2023
2.0.38 Sep 14, 2023
2.0.37 Sep 14, 2023
2.0.36 Sep 08, 2023
2.0.35 Sep 01, 2023
2.0.34 Aug 28, 2023
2.0.33 Aug 28, 2023
2.0.32 Aug 25, 2023
2.0.31 Aug 24, 2023
2.0.30 Aug 18, 2023
2.0.29 Aug 18, 2023
2.0.28 Aug 18, 2023
2.0.27 Aug 18, 2023
2.0.25 Aug 21, 2022
2.0.24 Feb 08, 2021
2.0.23 Jan 30, 2021
2.0.22 Sep 28, 2020
2.0.21 Aug 08, 2020
2.0.20 May 31, 2020
2.0.19 Feb 22, 2020
2.0.18 Jan 19, 2020
2.0.17 Jan 12, 2020
2.0.16 Dec 13, 2019
2.0.15 Dec 03, 2019
2.0.14 Dec 02, 2019
2.0.13 Nov 30, 2019
2.0.12 May 25, 2019
2.0.11 Apr 11, 2019
2.0.10 Mar 12, 2019
2.0.9 Mar 12, 2019
2.0.8 Jan 21, 2019
2.0.7 Sep 15, 2018
2.0.6 Jul 17, 2018
2.0.5 Jul 08, 2018
2.0.4 May 20, 2018
2.0.3 Apr 29, 2018
2.0.2 Apr 16, 2018
2.0.1 Apr 01, 2018
2.0.0 Apr 01, 2018
1.2.8 Mar 30, 2018
1.2.7 Mar 24, 2018
1.2.6 Dec 26, 2017
1.2.5 Dec 10, 2017
1.2.4 Dec 08, 2017
1.2.3 Sep 01, 2017
1.2.2 May 29, 2017
1.2.1 May 18, 2017
1.2.0 May 15, 2017
1.1.12 Feb 14, 2017
1.1.11 Feb 04, 2017
1.1.10 Jan 24, 2017
1.1.9.post1 Jan 18, 2017
1.1.9 Jan 18, 2017
1.1.8 Jan 08, 2017
1.1.7 Nov 30, 2016
1.1.6 Oct 20, 2016
1.1.5 Oct 02, 2016
1.1.4 Sep 29, 2016
1.1.3 Sep 11, 2016
1.1.2 Aug 20, 2016
1.1.1 Jul 20, 2016
1.1.0 Jul 10, 2016
1.0.10 Jul 10, 2016
1.0.9 May 24, 2016
1.0.8 May 01, 2016
1.0.7 Apr 16, 2016
1.0.6 Apr 01, 2016
1.0.5 Mar 26, 2016
1.0.4 Feb 20, 2016
1.0.3 Feb 17, 2016
1.0.2 Jan 08, 2016
1.0.1.post1 Dec 14, 2015
1.0.1 Dec 10, 2015
1.0.0 Dec 06, 2015
0.0.24 Dec 05, 2015
0.0.22 Nov 05, 2015
0.0.21 Nov 01, 2015
0.0.20 Oct 31, 2015
0.0.19 Sep 23, 2015
0.0.18 Sep 13, 2015
0.0.17 Sep 12, 2015
0.0.16 Sep 07, 2015
0.0.15 Sep 01, 2015
0.0.14 Aug 30, 2015
0.0.13 Aug 30, 2015
0.0.12 Aug 30, 2015
0.0.11 Aug 30, 2015
0.0.10 Aug 30, 2015
0.0.9 Aug 30, 2015
0.0.8 Aug 30, 2015
0.0.7 Aug 30, 2015
0.0.6 Aug 30, 2015
0.0.5 Aug 30, 2015

Wheel compatibility matrix

Platform Python 3
any

Files in release

Extras: None
Dependencies:
requests (>=2.14.2)
python-dateutil (>=2.7.5)
cachetools (>=3.1.0)
pytz (>=2018.9)
six (>=1.14.0)