Making requests to the Zendesk API

Have more questions? Submit a request

30 Comments

  • Jeremiah Currier
    Comment actions Permalink

    Excellent!

    0
  • DeVaris Brown
    Comment actions Permalink

    We also have a blog post, http://developer.zendesk.com/blog/2013/01/03/accessing-the-api-with-python/, detailing how to make calls to the Zendesk API using Python. And if you anyone wants to use a Python client library check it out on Github, https://github.com/eventbrite/zendesk. Good work Charles!

    0
  • Gopi Natha
    Comment actions Permalink

    Its simply awesome.thanks a ton.........:)

    0
  • Kelsey Hales
    Comment actions Permalink

    Hey guys,

    So I've been playing around with this with the end goal of figuring out how to pull large amounts of data from Zendesk so we can line it up with out in-house data for analysis. I'm a bit of a newbie, and can't figure out what's wrong. I've run the `get_groups.py` script and feel like I've got a handle on what it's doing, but when I try to modify it to see if I can make it work on something similar without the training wheels, I'm getting this error.
    ```C:\Python34>zendemo1
    Traceback (most recent call last):
    File "C:\Python34\zendemo1.py", line 25, in
    form_list = data['ticket_forms']
    KeyError: 'ticket_forms'
    ```

    Here's my script, without all the url, user, pwd variable setup at the beginning.
    ```
    form_list = data['ticket_forms']
    for name in form_list:
    print(ticket_forms['name'])
    ```

    So it sounds like it's not reading the `ticket_forms` json correctly, and not sure what I'm doing wrong there. I've reviewed [Ticket forms doc](https://developer.zendesk.com/rest_api/docs/core/ticket_forms) and the key is ticket_forms right?

    0
  • Angelica Coleman
    Comment actions Permalink

    Hey Kelsey,

    Make sure you change your URL to:
    url = 'https://your_subdomain.zendesk.com/api/v2/ticket_forms.json'

    Angie

    0
  • Chris Berns
    Comment actions Permalink

    I am writing a script that will go through users and use the name field to populate two user-custom fields, first and last name. I can get the user info, I run it through my python script but when I try to do a series of PUTs to update I always get a 401 error. I make a session after I do the initial GET of all of the user data. I am assuming if I have permission to get all users it isnt an Auth issue. I am incredibly new to REST and have a hard time debuggin

    0
  • Charles Nadeau
    Comment actions Permalink

    @Chris,

    Are you authenticating as an agent? Agents can update the user records of end-users, but not of agents or administrators. Only administrators can update the records of all users. (The same rule applies in the Zendesk admin interface.)

    0
  • Eco Willson
    Comment actions Permalink

    Can you share a way to use a token for authentication instead of password?

    0
  • Charles Nadeau
    Comment actions Permalink

    Good idea, Eco. I added the following below the get_groups script.

    > You can use an API token instead of a password to make API requests. Grab an API token from the Zendesk Admin interface at Admin > Channels > API. To use the token in the script, append the string '/token' (not the actual token value) to your email address in the user variable and assign the token value to the pwd variable, as follows:

    >```
    >user = 'your_email_address' + '/token'
    >pwd = 'your_api_token'
    >```

    0
  • Eco Willson
    Comment actions Permalink

    Thanks Charles works perfectly.

    0
  • Deron Fuller
    Comment actions Permalink

    I thought I would add a comment for anyone doing this from an Ubuntu (or other linux distro) where you more than likely have 2 versions of python. To get the 'requests' library for python3 this is what I did:


    sudo apt-get install python3-pip
    pip3 install requests

    The instructions here indicate to just use pip, but if you're system does have 2 versions, you'll need to use pip3 to work with python3.


     

    0
  • Luckyddurand
    Comment actions Permalink

    Charles, thanks for the article. I began to study Python. Very useful to me such knowledge and experience as you have.

    0
  • Nathaniel Erlandson
    Comment actions Permalink

    Thanks a lot for this tutorial @Charles, helped a ton in getting me started

    0
  • Kevin Lamenzo
    Comment actions Permalink

    Could use some help translating a Help Center create attachment request from CURL into Python. Speficially, the -F tags in the CURL statement are throwing me off. I also tried GETing and attachment, then recreating the JSON object returned as my payload for the post, but I'm getting back a 500 error.


    I took my subdomain, usr/pass out of the snippets below.


    CURL:


    curl -u <usrname:pass> https://<subdomain>..zendesk.com/api/v2/help_center/articles/115005591108/attachments.json -F "inline=true" -F "file=@635564" -v -X POST

    Python (I copied the example above to create a ticket and tweaked it for posting an article attachment in HC):


    import requests

    # Package the data in a dictionary matching the expected JSON
    data = {"article_id": 115005591108, "display_file_name": "635564", "file_name": "635564", "inline": 'true'}
    data2 = {"article_id": 115005591108}

    # Encode the data to create a JSON payload
    payload = json.dumps(data)

    # Set the request parameters
    url = 'https://<subdomain>.zendesk.com/api/v2/help_center/articles/attachments'
    user = <email>
    pwd = <apikey>
    headers = {'content-type': 'application/json'}

    # Do the HTTP post request
    response = requests.post(url, data=payload, auth=(user, pwd), headers=headers)

    # Check for HTTP codes other than 201 (Created)
    if response.status_code != 201:
        print('Status:', response.status_code, 'Problem with the request. Exiting.')
        exit()

    # Report success
    print('Successfully created the ticket.')

    0
  • Daniel Pawluk
    Comment actions Permalink

    @Kevin,


    Here is a gist I created that should help you with the python. Let me know how it works for you.

    1
  • Jaimin Patel
    Comment actions Permalink

    Is there any way to query article (Help Center) docs using python? Sample/example documentation would be helpful. 

    0
  • Charles Nadeau
    Comment actions Permalink

    Hi Jaimin,


    You can combine the info in the article about searching tickets with Python with the API doc on searching the Help Center. The Python is the same in both cases -- just the query parameters and response attributes are different.


    How to search with Python (tickets example) -


    https://help.zendesk.com/hc/en-us/articles/229136927-Searching-with-the-Zendesk-API#python


    HC search API


    https://developer.zendesk.com/rest_api/docs/help_center/search


    Charles


     


     


     


     

    1
  • Linde Skinner
    Comment actions Permalink

    Hello Zendesk - as we prepare for GDPR, can you provide the specific API calls that will allow us to close accounts and scrub user information from tickets? We don't store values for any users who may submit a ticket therefore we want to query Zendesk based on a metric like email addresses.


    Thank you, Linde

    1
  • Abdelhakim Salama
    Comment actions Permalink

    how can i Make a requests to the Zendesk API with javascript !!! , can you shar with me an exemple !!

    0
  • Joseph May
    Comment actions Permalink

    Hi Abdelhakim-

    I would suggest starting here in this article, which will introduce you to making these calls in the browser console.

    0
  • Abdelhakim Salama
    Comment actions Permalink

    thank you

    0
  • tomasz.czarny
    Comment actions Permalink

    Hello 👋 Zendesk Community! I am new to the Zendesk API and could use your help. For whatever reason I continue to receive a 401 error when I try to authenticate to our sandbox account. I have tried authenticating both using an API token and the email and password method. Both are enabled in Zendesk. 

    In my instance I am trying to pull down the user data via request.get. Can anyone spot what's going on? I've even tried to copy paste the python example in this article and modify where necessary... 

    Thanks in advance! 

    0
  • Bryan Flynn
    Comment actions Permalink

    Hi Tomasz -- since this is a sandbox instance, users (and related info such as passwords, etc) would need to be recreated in the sandbox. Sandboxes are completely separate instances than the "production" instance. Perhaps this particular user and password are different in the sandbox?? Just guessing here.

    If the user and password are accurate for the sandbox instance, please submit a ticket to support@zendesk.com. It may require more details and access to your account that just can't be done over a community conversation. Thanks!

    0
  • Derrick Robinson
    Comment actions Permalink

    I need help, trying to use Python to make the following request for tickets updated within the last 24 hours but cant seem to figure this out based on the example provided.It gives me a valid response in Postman so the GET request works, I just can't figure out how to code this.

    https://clubready.zendesk.com/api/v2/search.json?sort_by=updated_at&sort_order=desc&query=updated>24hours type:ticket

    0
  • Joseph May
    Comment actions Permalink

    Hi there Derrick-
    Are you just using the requests library or are you using one of the Zendesk Python API clients?

    0
  • Joseph May
    Comment actions Permalink

    Hi there Derrick-
    A quick search led me to the following:

    You can convert JSON to CSV using the built-in JSON and CSV libraries in Python. Import them into your code by adding lines saying "import json" and "import csv" near the top of your code. Then, use the JSON library's "load" method to import the data from a JSON file. It will be loaded as a Python dictionary.

    Once in CSV it should easier to work with in Excel. Searching the internet for "python JSON to CSV" presents no shortage of how-to's.

    0
  • Muthana
    Comment actions Permalink

    Hey Guys, 

     

    When i create the ticket using the request lib, can i set the sender or recipient email? 

    0
  • Bryan Flynn
    Comment actions Permalink

    Hi Muthana. The example shown above is calling the product's REST API endpoint. Whatever it can do, you can do through the request/lib call.

    For tickets, checkout all the attributes on the documentation reference site at:

    https://developer.zendesk.com/rest_api/docs/support/tickets#json-format

    There's actually a section that discusses setting the "requester" (i.e. sender) property here:

    https://developer.zendesk.com/rest_api/docs/support/tickets#creating-a-ticket-with-a-new-requester

    The recipient is also an attribute of a ticket.

    0
  • Muthana
    Comment actions Permalink

    Hello Again Guys, 

     

    Can you share an example of creating multiple tickets the same way.

    0
  • Bryan Flynn
    Comment actions Permalink

    Hi Muthana. If you have questions related to particular Zendesk Support API calls, the Community forum at https://develop.zendesk.com/hc/en-us/community/topics/360000019807-Zendesk-REST-APIs would be a better place to post questions.

    As mentioned at the top of this article, it's really for "instructional purposes only. Zendesk does not support or guarantee the code. Zendesk also can't provide support for third-party technologies such as Python." That doesn't prevent anyone from posting solution samples, of course, but it's not expected.

    0

Please sign in to leave a comment.

Powered by Zendesk