Making requests to the Zendesk API Follow

Comments

15 comments

  • Avatar
    Jeremiah Currier

    Excellent!

  • Avatar
    DeVaris Brown

    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!

  • Avatar
    Gopi Natha

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

  • Avatar
    Kelsey Hales

    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?

  • Avatar
    Angelica Coleman

    Hey Kelsey,

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

    Angie

  • Avatar
    Chris Berns

    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

  • Avatar
    Charles Nadeau

    @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.)

  • Avatar
    Eco Willson

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

  • Avatar
    Charles Nadeau

    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'
    >```

  • Avatar
    Eco Willson

    Thanks Charles works perfectly.

  • Avatar
    Deron Fuller

    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.


     

  • Avatar
    Luckyddurand

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

  • Avatar
    Nathaniel Erlandson

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

  • Avatar
    Kevin Lamenzo

    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.')
  • Avatar
    Daniel Pawluk

    @Kevin,

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

Please sign in to leave a comment.

Powered by Zendesk