Searching with the Zendesk API

Have more questions? Submit a request

21 Comments

  • UserAdmin #55555
    Comment actions Permalink

    Is there a way I can query for the json result of all my custom ticket field entries for "Custom field ID 2967xx97"?

     

    0
  • Bryan - Community Manager
    Comment actions Permalink

    No, not right now at least (Aug 2018). You can do a search against custom fields using the "fieldvalue" keyword but it searches across all custom fields. This is a known limitation that our team is looking into but no ETA on when field specific searches might become available.

    0
  • Mike C
    Comment actions Permalink

    I'm getting inconsistent results from the search API.   Tickets in our system have several custom files.  One of them is a group ID.  There is only one ticket with status < closed with a given group ID.  At one point in our workflow, an agent will set the ticket to the Solved status.  I have an app that finds the ticket in the Solved status and sets the status to Open using the Core API.  If I immediately search for that ticket in the search API only on the group ID, I find it with the Open status.  If I search for that ticket with the Group ID and  with "status<pending", nothing is returned.  After some period of time, this query will return ticket.  But if I query immediately after the update, nothing is found.  Is this expected behavior?  

    0
  • Joey
    Comment actions Permalink

    Hi Mike-

    Yes this is expected. As documented under our Core API:

    Note: It can take up to a few minutes for new tickets, users, and other resources to be indexed for search. If new resources don't appear in your search results, wait a few minutes and try again.

    0
  • Francisco Oirdobro
    Comment actions Permalink

    Hi,

    is there a way to search with the API using AJAX?

    Thanks

    0
  • Bryan - Community Manager
    Comment actions Permalink

    Hi Francisco. Yes, Zendesk APIs can be called via AJAX. I'd check out the API Getting Started section of the docs and start with an overview. If you have follow-up questions, you can post to the online REST APIs community topic or reach us at support@zendesk.com.

    I just found this article, too, that gives an AJAX example: Adding KB search to your website with AJAX

    0
  • Anurag Toshniwal
    Comment actions Permalink

    Hi Zendesk Team,

    I've been trying to leverage the Search API with Python to fetch all "Incident" type tickets for a given date but I keep getting partial result - the endpoint returns all incident type tickets but does not honor the date parameter; I'm getting incident type tickets created on other dates as well. 

    Below is the endpoint that I'm using. Can someone please provide some insights? Thanks!

    <sub-domain>/api/v2/search.json?query=created:2019-09-06&ticket_type:incident
    0
  • Bryan - Community Manager
    Comment actions Permalink

    Hi Anurag.

    While not that obvious, the "query" parameter is the only HTTP request parameter being sent here. Putting an ampersand/& character essentially creates a new parameter that's not recognized (and thus ignored).

    If running from your browser, try:

    <sub-domain>/api/v2/search.json?query=created:2019-09-06 ticket_type:incident

    If running from, say, the command line using cURL, try:

    curl -v -u username@example.com:password https://your_subdomain.zendesk.com/api/v2/search -G --data-urlencode "query=created:2019-09-06 ticket_type:incident"

    Here's some references that maybe you've already checked out, but I'll list them anyway. They each add to the overall "Search" picture...

    Searching Tickets:

    https://support.zendesk.com/hc/en-us/articles/203663206-Searching-tickets

    Zendesk Support search reference:

    https://support.zendesk.com/hc/en-us/articles/203663226-Zendesk-Support-search-reference

    Zendesk Support advanced search:

    https://support.zendesk.com/hc/en-us/articles/217385687-Zendesk-Support-advanced-search

    Search API reference:

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

    In this last reference link, you can see that there are three HTTP parameters that can be passed -- "query", "sort_by", and "sort_order". Like I said, it's not that obvious. When you add possible encoding that might be needed along with a HTTP query parameter called "query", things can get a bit confusing.

    0
  • Sam
    Comment actions Permalink

    Regarding the Python example, it's not necessary to encode the passed parameters. The magical Requests library does all URL encoding for the user as long as the params are passed correctly. This should be done as follows:

    url = 'https://your_subdomain.zendesk.com/api/v2/search.json'

    params = {
        'query': 'type:ticket status:open',
        'sort_by': 'created_at',
        'sort_order': 'asc',
    }

    response = session.get(url, params=params)
    0
  • Adnan
    Comment actions Permalink

    I'm trying to use API token to do get request but seems not to be working, can someone help how to do this in python?

    credentials = 'myemail/token:6h2gouafy69ubSTklEJlHXnj1Cxxxxx'
    session = requests.Session()
    session.auth = credentials

    params = {

        'query': 'type:ticket tags:"send_whatsapp" status<closed',

    }

    url = 'https://propertypro.zendesk.com/api/v2/search.json?' + urlencode(params)

    response = session.get(url,headers={'Authorization': credentials}

    )

    if response.status_code != 200:

        print('Status:', response.status_code,

              'Problem with the request. Exiting.')

        exit()
    0
  • Sam
    Comment actions Permalink

    @adnan You're doing a few things wrong here. First, credentials are incorrectly passed. Second, parameters should be passed using requests built-in kwarg. Try this:

    import requests

    credentials = ("username@domain/token", "6h2gouafy69ubSTklEJlHXnj1Cxxxxx")
    session = requests.Session()
    session.auth = credentials

    url = 'https://propertypro.zendesk.com/api/v2/search.json'
    params = {'query': 'type:ticket tags:"send_whatsapp" status<closed'}

    try:
    response = session.get(url, params=params)
    response.raise_for_status()

    except requests.exceptions.HTTPError as e:
    code = e.response.status_code
    print(f'Status {code}: {e}. Problem with the request. Exiting.')
    exit()

    I highly suggest reading the requests docs thoroughly, and the Zendesk introductions and examples. Even better, use a Python Zendesk wrapper lib.

     

    EDIT: Forgot indents in code block

    1
  • Bryan - Community Manager
    Comment actions Permalink

    One more thing, please keep in mind the fairly recent restrictions agains the Search API documented here:
    New Search API Result Limits

    The maximum number of results returned is now 1000.

    0
  • Jonathan March
    Comment actions Permalink

    Tobi somewhat tangentially, if you plan to do much work in Python with the ZD API, you might find this high-level, well-maintained, wrapper package useful. https://github.com/facetoe/zenpy 

    0
  • Tobi
    Comment actions Permalink

    Jonathan March - Thanks for that!

     

    Silly question maybe you or @... may know.

     

    How do I get the api to return custom field values for me using the id of the field?

    data = response.json()
    for result in data['results']:
    print(result['customField:custom_field_360041463053'])

    The above throws me an error for obvious reasons :)

     

    Edit, I figured the above out by doing this:

     

    data = response.json()
    for result in data['results']:
        print(result['custom_fields'][5]['value'])

     

    Where "5" represents the index number of the custom field I want to return.

    Thanks!

    1
  • Jonathan March
    Comment actions Permalink

    Tobi - Good investigation! One suggestion -- using a constant index is fragile long-term, likely to break and cause hard-to-find errors eventually, e.g. after another custom field is added and used somewhere else. A brute-force but more robust and long-term-readable way to do the same thing would be to use something like this (not tested):

    my_special_field_id = 360041463053
    data = response.json()
    for result in data['results']:
    for field in result['custom_fields']:
    if field['id'] == my_special_field_id:
    print(field['value'])
    break
    1
  • Qumulo Inc
    Comment actions Permalink

    Is there a method of sending a query as a json payload rather than URL encoded?  sadly it seems not all encodeURI function in all languages can handle a colon properly (I wish this was a joke, see dataweave 2.x documentation on non-uri encodable chars).

    Thanks! 

    0
  • Andy Loeber
    Comment actions Permalink

    Hi there!

    I want to import tickets from another system into Zendesk. I have a very strange behavior that makes me go nuts.

    To check if a ticket is already in Zendesk I use the Search API:

    search.json?query=type:ticket%20external_id:MY_EXTERNAL_ID

    If this gives me a result, I update the ticket, if it gives me no results, I create a new ticket in Zendesk. But the above call sometimes returns an empty result although the ticket already exists in Zendesk and as a result I then have 2, 3, 4, ... of the same ticket in Zendesk.

    {
    "results": [],
    "facets": null,
    "next_page": null,
    "previous_page": null,
    "count": 0
    }

    Sometimes I also get count 1, but the results array is empty...

    {
    "results": [],
    "facets": null,
    "next_page": null,
    "previous_page": null,
    "count": 1
    }

    It's even possible that I first get a result and just one second later I get no result for the same request...

    Have you ever heard about this issue?
    What's the best way to check if a ticket is already present within Zendesk?

    Best regards,
    Andy

    0
  • Greg - Community Manager
    Comment actions Permalink

    Hi Andy! That type of behavior sounds like it may be an indexing delay for search APIs, which can take a couple of minutes to complete. Could you confirm if the search is being run shortly after ticket creation or are you still seeing these results hours/days+ later?

    0
  • Andy Loeber
    Comment actions Permalink

    Hi Greg!
    Thank you for your quick answer. I keep watching it, but it seems that this behavior only occurs when I search shortly after ticket creation or deletion, so it's an indexing delay. Interesting would be how long I have to wait to be sure that the Search API returns correct results.

    0
  • Greg - Community Manager
    Comment actions Permalink

    Due to the nature of indexing, it's never really possible to give a definitive answer, so our official stance is the very vague language in this article

    Note: It can take a few minutes for Zendesk Support to index new and modified tickets, users, and other resources. If they don't appear in your search results, wait a few minutes and try again.

    I would say that in my experience, most of the time, for db tables that aren't "massive" (more vagueness, I know) in scale, it shouldn't take more than a minute or two.

    I'm not sure that this would be worth your time, but one possibility would be to store the 201 response after ticket creation so that you could run a search on your end before hitting Zendesk. not a great solution and probably overkill, just wanted to share some possible ideas!

    1
  • Andy Loeber
    Comment actions Permalink

    Thank you Greg for sharing a possible solution. I haven't thought about this one, but probably this will best suit my business case.
    Thank you!

    1

Please sign in to leave a comment.

Powered by Zendesk