Using Python with the Zendesk API Follow

This support tip describes how to set up the tools you need to access the Zendesk API using Python.

Tools you need

  1. Xcode
    You can get this from the Mac App store free. Once you download it, you’ll need to open it and go to Xcode > Preferences > Downloads . Where it says “Command Line Tools”, click Install .
  2. pyCurl (installation instructions below)

Setting up pyCurl

To access the Zendesk API from Python, you will need to download the pyCurl library. You can get it from . After you've downloaded the archive file, extract the files from it.

Open the Terminal Application (/Applications/Utilities/ and navigate to the directory where you extracted pyCurl. Enter the following command in the terminal:

sudo python install

You’ll be prompted to enter a password which. Don’t worry if you don’t see any astericks (*) or letters as you type. Just enter your password and then click the Return key. This is the same password you type in when you login.

If all goes well, you should be able to type "python", hit Return and then type “import pycurl” and hit Return again. If you get an error message, then pyCurl is not installed correctly. If you don’t see an error message, press Control + D to exit.

Once pyCurl is installed, you're ready to start writing your first script.

That’s a Wrapper

A wrapper is something that makes it easier to use a library. In this case, we have pre-made one for pyCurl that works with the Zendesk API.

Important Note : Python is very strict about spacing and tabbing. Do not copy this script from here but download the attached file.

import pycurl
import cStringIO
import json
def pyCurlWrap(pUrl, meth, data):
    buf = cStringIO.StringIO()
    c.setopt(c.URL, pUrl);
    c.setopt(c.WRITEFUNCTION, buf.write)
    c.setopt(c.CONNECTTIMEOUT, 5)
    c.setopt(c.TIMEOUT, 8)
    if not data == "null":
        c.setopt(c.POSTFIELDS, data)
    c.setopt(c.FOLLOWLOCATION, True)
    c.setopt(c.MAXREDIRS, 10)
    c.setopt(c.USERPWD, cred)
    c.setopt(c.CUSTOMREQUEST, meth)
    c.setopt(c.HTTPHEADER, ['Content-type: application/json'])
    result = buf.getvalue()
    return result;

After you've added this to a new script, you can call the wrapper using a command like this (replace 'subdomain' with the name of your Zendesk account):

pyCurlWrap("", "GET", "null")

The "null" flag is important. If you aren’t doing a POST or PUT to the API, make sure to add "null" so that curl doesn’t include the postfields option. If you're going to PUT or POST to the API, include your POST data as a string. Here's an example:

data = “{\"ticket\":{\"comment\":{\"value\":\"my comment\", \"is_public\": false}}}"

You then pass that to the wrapper as shown here:

pyCurlWrap(“”, “POST”, data)

The ideal way to parse the returned data is to pass it directly into json.load so that it can be broken out into lists.

json_obj = json.loads(pyCurlWrap("", "GET", "null"))

Hopefully this should give you a good start into working with Python and the Zendesk API.

Have more questions? Submit a request

Please sign in to leave a comment.

Powered by Zendesk