Forums/Community/Support tips & notes

Using Python with the Zendesk API

Adam
posted this on May 16, 2012 19:05

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 http://pycurl.sourceforge.net/. After you've downloaded the archive file, extract the files from it.

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

sudo python setup.py 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
 
cred="apanzer+spoke@zendesk.com/token:[token]"
 
def pyCurlWrap(pUrl, meth, data):
    buf = cStringIO.StringIO()
    c=pycurl.Curl()
    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'])
    c.perform()
    result = buf.getvalue()
    buf.close()
    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("https://subdomain.zendesk.com/api/v2/ticket_fields.json", "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(“https://subdomain.zendesk.com/api/v2/tickets/123.json”, “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("https://subdomain.zendesk.com/api/v2/ticket_fields.json", "GET", "null"))

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