Getting large data sets with the Zendesk API and Python

Have more questions? Submit a request

21 Comments

  • Viola Rudenko
    Comment actions Permalink

    Hi Charles,


    Thank you for the great API tutorials! It's super helpful.


    Is there a way to use token for authentication in this setup? I tried to follow the token guidelines here , but it gives me 404 error:


    credentials = 'my_email' + '/token', 'my_api_token'
    session = requests.Session()
    session.auth = credentials


    OR


    user = 'my_email +'/token'
    pwd = 'my_api_key'
    session = requests.Session()
    session.auth = (user, pwd)


    I also tried to set the header like here, but still no luck:


    session = requests.Session()
    head = {'Authorization': 'access_token my_api_token'}
    response = session.get(url, headers=head)


    P.S. I was able to get access to Zendesk data with my email address and token before.

    0
  • Charles Nadeau
    Comment actions Permalink

    Hi Viola,


    Hmm, I tested the script with a token and it works for me. Example:


    credentials = 'example@outlook.com' + '/token', 'qPbD6eTsXNC4aBIDs4UkHYD3fss6fdfalnZDjmSN'

    Did you check if token access is enabled in your Zendesk? In the agent interface, click Admin (gear icon) in the lower-left, then API in the Channels category. On the Channels/API page, verify that Token Access is enabled (the blue checkbox). If not, make sure to click the Save button after selecting the checkbox.

    0
  • Viola Rudenko
    Comment actions Permalink

    Hi Charles,


    Thank you for looking into that! The token access was enabled, but looks like I picked incorrect topic_id. As soon as I switched to ticket_metrics, the script executed.

    0
  • Don Wood
    Comment actions Permalink

    Thanks! this looks very useful. I'm not very technical so excuse my question. Can this work to get all the data on tickets out to a spreadsheet? The reporting in Zendesk is limited and I want to be able to get ticket data our of the system and into a format to build reports. A simple this I would like is to report the ticket and the associated JIRA ticket number. We are using the integration with JIRA but I don't see a way to get this information.

    1
  • Emilio Méndez
    Comment actions Permalink

    Great topic, not only in content but in form as well; exactly what I was looking for !!  Thank you Charles & Zendesk Team !!

    0
  • Emilio Méndez
    Comment actions Permalink

    Hi Don:


    Basic reporting might be limited, but you can try "Insights", it's definitely a whole different thing; the only thing is that it depends on the plan you have.


    Hope that works for you !!


    Regards,

    0
  • Charles Nadeau
    Comment actions Permalink

    Hi Don,


    To learn how to use the API to export lots of data to Excel, see the companion tutorial:


    0
  • Alex Taitague
    Comment actions Permalink

    Thanks. I had trouble at the pickle part...I run my script with the API call and serializing the results, but when I go to run the script again (API commented out) I get the message that the pickle file cannot be found. I can't find any file created after the script run. (within any script run, I can write and read back the serialized data -- it just doesn't persist outside of the script run).


    Any ideas?


     


     

    0
  • Charles Nadeau
    Comment actions Permalink

    Hi Alex,


    I ran this script again to test if any Python libraries had been updated since I wrote it. Running Python 3.6, I had no issues serializing the data into a separate file named 'my_serialized_data_file'. The file appears in the same folder as my Python script.


    Maybe you're in Windows and the OS is confused by the lack of a file extension? Try adding ".p" to the Pickle file, as follows:


    topic_data = {'posts': topic_posts, 'users': user_list}
    with open('my_serialized_data_file.p', mode='wb') as f:
    pickle.dump(topic_data, f)

     

    0
  • Ann Jensen
    Comment actions Permalink

    Hi,

    Just wondering if there is a similar article that describes how to bulk update Zendesk (Help Center) e.g. by bulk uploading articles from one script?

    Thanks,

    Ann

    0
  • Charles Nadeau
    Comment actions Permalink

    Hi Ann,

    The batch-publishing tool the Zendesk Docs team uses is open source on Github for anybody to use. See https://github.com/chucknado/zpu. For instructions on how to use it, see the readme at https://github.com/chucknado/zpu/blob/master/README.md.

    Feel free to download and modify it for your use case.

    Thanks.

    0
  • Jose Juan Muñoz JImenez
    Comment actions Permalink

    Could you explain what this is about? "The Zendesk support URL should look like 'https://obscura.zendesk.com'". I do not understand how the URL should look

    0
  • Charles Nadeau
    Comment actions Permalink

    Hi Jose,

    Sorry if it's not clear. Your Zendesk URL has two parts: a subdomain name you chose when you set up your account, followed by zendesk.com (for example: mycompany.zendesk.com). 

    The URLs for 3 different accounts might look something like this:

    - https://red.zendesk.com

    - https://blue.zendesk.com

    - https://green.zendesk.com

    Hope that helps.

     

    0
  • Panaguit Chrysler
    Comment actions Permalink

    Hello my code says keyerror 'posts'

    0
  • Panaguit Chrysler
    Comment actions Permalink

    import pickle
    import time
    import requests

    ##credentials = 'your_zendesk_email', 'your_zendesk_password'
    credentials = 'user@something.com' + '/token', 'thisisatokencode'
    session = requests.Session()
    session.auth = credentials
    zendesk = 'zendeskurl'

    topic_id = 360002871987
    topic_posts = []
    user_list = []
    url = zendesk + '/api/v2/community/posts/' + str(topic_id) + '/posts.json?include=users'
    while url:
    response = session.get(url)
    if response.status_code == 429:
    print('Rate limited! Please wait.')
    time.sleep(int(response.headers['retry-after']))
    continue
    if response.status_code != 200:
    print('Error with status code {}'.format(response.status_code))
    exit()
    data = response.json()
    topic_posts.extend(data['posts'])
    user_list.extend(data['users'])
    url = data['next_page']

    topic_data = {'posts': topic_posts, 'users': user_list}
    with open('my_serialized_data_file.p', mode='wb') as f:
    pickle.dump(topic_data, f)

    with open('my_serialized_data_file.p', mode='rb') as f:
    topic = pickle.load(f)

    for post in topic['posts']:
    author = 'anonymous'
    for user in topic['users']:
    if user['id'] == post['author_id']:
    author = user['name']
    break
    print('"{}" by {}'.format(post['title'], author))

    0
  • Gary Kester
    Comment actions Permalink

    Hi Charles,

    I want to be able to use the API to download only call attachments from our Zendesk instance. There is an app that downloads attachments per ticket, but I want to be able to download all call attachments incrementally as part of our backup strategy.

    Can you help wiht this?

    Regards,

    Gary

    0
  • Greg - Community Manager
    Comment actions Permalink

    Hi Gary...we do not have any APIs that would specifically target call attachments. It's possible that this could be accomplished via an app, so could check our partner directory to find a team that might be willing to develop this for you.

    0
  • Justin Laing
    Comment actions Permalink

    I'm looking at exporting tickets.  How would I manipulate the code to do such?

    0
  • Bryan - Community Manager
    Comment actions Permalink

    Hi Justin Laing,

    There are APIs and other product functionality to export information. There are also third-party apps in the Zendesk Marketplace that can help.

    If setting up ongoing exports, see the incremental export APIs:

    What is the recommended method to regularly export Zendesk Support data?

    If wanting to get specific information, see one of the many REST APIs:

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

    Other product export option:

    Exporting data to a JSON, CSV, or XML file (Professional and Enterprise)

    Zendesk Marketplace options:

    https://www.zendesk.com/apps/directory/?q=export

    Hope the above gives some ideas and next steps on export options!

    0
  • Ahmed Aliv Jola
    Comment actions Permalink

    Hey ! 

    I'm having trouble adding the auto pagination to url  also I want to make sure there is no "429" error as well 

    can you please help me to add the pagination to my code snippet ? 

     

    <code>

    import requests , json ,csv , time


     



    def ExportAPIDataToJsonFile():

        bearer_token = 'Bearer ' + 'longtoken'
        header = {'Authorization': bearer_token}
        q = {'start_time':1592896963000000}
        
        
        response = requests.get(url, params=q ,headers=header)
      

        
        response_text = response.text
        data = json.loads(response_text)
        data_ser = json.dump(data , open('agent_timeline.json' , "w") ,indent= 4)
        </code>
     
    0
  • Bryan - Community Manager
    Comment actions Permalink

    Hi Ahmed Aliv Jola -- below is an article that gives an example in Python on how to support paging. Since paging is very similar across most of the product's APIs that support paging, this example can be applied to other APIs, too:

    Paginating through lists

    0

Please sign in to leave a comment.

Powered by Zendesk