C# wrapper for the Zendesk REST API

Here's a full C# wrapper for the Zendesk REST API: https://github.com/eneifert/ZendeskApi_v2. The readme contains a list of all the public methods, but hopefully it will be simple to use. The code is of course open source, so feel free to add or make whatever changes you like.

If you aren't familiar with GitHub, visit their help page.

Enjoy and feel free to ask questions.

Have more questions? Submit a request

Comments

  • Avatar
    Jake Holman

    Nice work, Eric!

  • Avatar
    David Finch

    Hey Eric,

     

    I was just in the process of writing one of these when I came to this forum to ask a question.  Seems however that there is already something available for what I am trying to create (your API wrapper).  However, my question still remains;

     

    I notice in your readme.txt where you detail all your public methods there exists two methods as follows;

    bool UpdateTicket(int ticketId, string description);

    bool UpdateTicket(int ticketId, Comment comment);

     

    Am I correct in thinking then that you cannot update a ticket with (for example) changed values of custom fields, altered subject title ect ?

  • Avatar
    Eric Neifert

    Hi David,

    Thanks for helping to make this api better. I have just added a method to do what you are asking about. bool UpdateTicket(Ticket ticket)

    Here is how you update custom fields:

    var myTicketFields = api.GetTicketFields();

    var myTicket = api.GetTicketById(yourTicketId);

    myTicket.TicketFieldEntries.Add(new TicketFieldEntry

                                                    {

                                                        TicketFieldId = myTicketFields.Find(x => x.Title == "some title").Id,

                                                        Value = "my new value"

                                                    });

    api.UpdateTicket(myTicket);

    Hope this helps,

    Eric

     

  • Avatar
    David Finch

    Wow Eric, speedy update, thank you !

    I have not had chance to try it out yet but will do over the weekend and get back to you.  We have a drop down field which our external program controls the selected item.  We have the field-id for it but could not see a way with your previous documentation how this could be maintained.

     

    Now all I must do is figure out this Git Hub thing and I'm away.

     

    Regards,

    David

  • Avatar
    David Finch

    Eric,

    Im having issues getting your tests to pass.  Specifically the "CanGetATicket".  I have filled out my specific details in ZenDeskSettings and have created a test ticket which most certainly does have comments attached to it, both public and private made by both agents and requester.  I have then placed the nice-id of this ticket into the '_api.GetTicketById()' method.  However the test is failing on the Assertion that the comments are greater than 0 because the Comments object itself is null.

     

    Any ideas why this may be ?

     

    Regards,

    David

  • Avatar
    Eric Neifert

    Hmm, I checked them all again today and the tests should all pass if you have the settings correct. Can you tell me if anyone them pass?

  • Avatar
    David Finch

    It is strange.  Sometimes the 'CanGetForums' will pass but not anymore. More often than not it will ask me to find the source of View.cs.  Now however, it seems to be consistently failing all tests.

     

    Please find attached the xml output from running NUnit.

  • Avatar
    David Finch

    Hi Eric,

    From playing around with it further, there is something in your code which believes the location of your model classes are still on your machine.  I was debugging your tests and tried to step-into the api.GetTicketById() method.  It then said that it could not find 'Ticket.cs' in the location 'C:\Users<your username>\Documents\Visual Studio 2010\Projects\'. I pointed the debugger at the correct location for Ticket.cs relative to my machine but still, after each build, it keeps trying to look for that location on your machine.

    Any ideas what's going on?

  • Avatar
    David Finch

    I have now re-downloaded your solution.  Cleaned it, compiled it and re-tested. It can now find the location of Ticket.cs but I get the same error as previously mentioned but searching for 'RestClient.Sync.cs', again, it is looking for it in a location on your machine.

  • Avatar
    David Finch

    Ok, sorry for the hassle but I really want to get to the bottom of this.  I have managed to get all the code talking to each-other correctly, i.e not looking for locations of source on your machine.

    The code is failing to return a Ticket object with any values.  It fails on ZendeskApi.Execute<T>(ZenRestRequest request).  If I use the debugger to inspect the response variable produced by _client.Execute<T>(request); I see that it has an errorExeption as follows;

     

     

    System.ArgumentException: Can not convert Integer to String.

       at Newtonsoft.Json.Linq.JToken.op_Explicit(JToken value)

       at RestSharp.Extensions.MiscExtensions.AsString(JToken token)

       at RestSharp.Deserializers.JsonDeserializer.Map(Object x, JToken json)

       at RestSharp.Deserializers.JsonDeserializer.CreateAndMap(Type type, JToken element)

       at RestSharp.Deserializers.JsonDeserializer.BuildList(Type type, JEnumerable`1 elements)

       at RestSharp.Deserializers.JsonDeserializer.Deserialize[T](RestResponse response)

       at RestSharp.RestClient.Deserialize[T](RestRequest request, RestResponse raw)

    System.ArgumentException: Can not convert Integer to String.   at Newtonsoft.Json.Linq.JToken.op_Explicit(JToken value)   at RestSharp.Extensions.MiscExtensions.AsString(JToken token)   at RestSharp.Deserializers.JsonDeserializer.Map(Object x, JToken json)   at RestSharp.Deserializers.JsonDeserializer.CreateAndMap(Type type, JToken element)   at RestSharp.Deserializers.JsonDeserializer.BuildList(Type type, JEnumerable`1 elements)   at RestSharp.Deserializers.JsonDeserializer.Deserialize[T](RestResponse response)   at RestSharp.RestClient.Deserialize[T](RestRequest request, RestResponse raw)

     

    Does this seem strange to you ?  Your code appears to be set up to request and recieve xml, however the request and response objects are using json to transfer data.  Could this be the reason for null references and such exceptions as detailed above?  An XMLSerializer attempting to parse json ?

     

  • Avatar
    Eric Neifert

    Hi David,

    It is no hassle and I thank you for looking into this. One thing I noticed is that my tests pass when I have the settings pointed to the trial version account where as some fail when I have them pointed to our new production account. The overall problem lies in the Deserialization of the xml (going from the xml to the objects). I have posted an issue on the RestSharp google group and got a reply back yesterday. I am going to look over his response more tomorrow morning. I will let you know what I find.

    And as per your last comment, I knew before that their was some funkyness in the xml parsing before but tried to get around it by using json where possible. I see now that was a mistake and will go back and change that to always use xml. Also if the content-type is json then the application would not try to serialize it using json, it would use the RestSharp JsonSerializers.

    Take care,

    Eric

  • Avatar
    Eric Neifert

    Hi David,

    I re-wrote how all the xml deserialization happens. If you get the latest code now, I think all of your tests should pass. Let me know if any do not.

    Have a good day,

    Eric

  • Avatar
    David Finch

    Hi Eric,

    Yes ! Thank you this works perfectly now.  Thought I spotted an issue with .GetTicketsInView as none of the ticket object had .TicketFieldEntries values but then I realise the xml returned for this query doesnt include field info anyway so there is no way you could populate these properties. 

    Thank you very much for your help with this code, you have saved me masses of development time.

     

    Regards,

    David.

  • Avatar
    David Finch

    Eric,

    I could not see a test for adding different types of comments to an existing ticket.  Can you confirm that your API is able to add both public and private comments to a ticket ?  Its just all comments submitted, regardless of the Comment objects "isPublic" property value seem to come out as private.

  • Avatar
    Eric Neifert

    David,

    Thanks again for taking the time to post your questions. Looks like ZenDesk does not accept the value "True" for bools, they only accept lower case "true". I will change the serializer it to use lower case for booleans.

    Eric

  • Avatar
    David Finch

    Eric,

    No need to thank, your time and effort in building this API in the first place has saved me huge amounts.  To siply use it and report back to you is the least I can do.

    When you update your API with the aforementioned change, would it be possible to download only the object you modified.  Its just, I have made a minor change in your ticket object so that I do not get null reference exceptions and also so new comments are automatically added to the ticket upon submission.  Obviously if I update your entire API, these changes will be reverted and ill have to put it back in every time I update.

    Regards,

    David

  • Avatar
    Eric Neifert

    David,

    I made the changes and verified that public comments now work. Also please put any future questions here: https://github.com/eneifert/ZendDeskApi/issues

    Have a good day,

  • Avatar
    Eric Neifert

    Also the only file that really has changed here is the ZenDeskXmlSerializer, so you can just get that.

  • Avatar
    Michael Gold

    Hi, I am creating a ticket using the CreateTicketAsEndUser. I am setting

     

    t.TicketTypeId = (int)TicketType.Problem;

                t.PriorityId = (int) TicketPriorities.Urgent;

    t.TicketTypeId = (int)TicketType.Problem;            

    t.PriorityId = (int) TicketPriorities.Urgent;

     

    In Zend it is not showing the ticket created with these settings. 

    Thanks

  • Avatar
    Jeff Smith

    I am totally lost with this API wrapper. How are you folks "running the tests"? When I try to run, there is no output class specified. How do I make this stuff run (out-the-box)? What can I do to step through this stuff and see it actually work with my settings?

    This looks really cool and useful. I am completely unsure how to begin making it work....any ideas how to run the test project?

  • Avatar
    David Finch

    Hi Jeff,

    The test  project is a class library so you wont be able to run it 'out of the box'.  If my memory serves me correctly, the tests are built using NUnit.  If so, you will need to download the NUnit runner *rummage for link*  http://www.nunit.org/?p=download Once you have this, open it, select file > open project and select the .Tests .dll.  I cant remember the exact name of the test project but ill assume if your asking about using this your clued up on how to find the output .dll of a project.  Once you have loaded this correctly you will see a list of tests available to you and you can run them from the GUI.

     

    If you would like to step over the code yourself and inspect object properties, you will need to attach it to the NUnit process. To do this, from inside visual studios, i THINK its in the debug menu; Debug > Attach to process (might be Ctrl + P for a shortcut) and select the Nunit process from the menu (Not the Nunit agent)  now when you move back to your NUnit runner, and run a test, it will stop on any breakpoints you have set.

     

    Hope this helped.

  • Avatar
    Mikhail Zakharov

    Hi, Eric.

    I'm trying to use the ZenDeskApi Wrapper. My test application using ZenDeskApi.GetOrganizations method to get all organizations. But it's return nothing. I used Fiddler to debug. And there is correct responce with organizations data In a trace log.

    I found some strange things in method (Core.cs):

            public T Execute<T>(ZenRestRequest request) where T : new()

            {

                var response = _client.Execute<T>(request);           

                return response.Data;

            }

    In quick watch window (see attach) "Data" property is null. But "Content" property contains a value. And there is error message in "Error exception". Would you explain this and help me?

  • Avatar
    Eric Neifert

    Hi Mikhail,

    Seems like the project is missing a reference to Newtonsoft.Json.Net35. This dll was removed completely in Feb so I would recommend just getting the latest version. However, if you are all ready in a production environment and don't want to update then you could download the dll from here: http://json.codeplex.com/releases/view/50552 and add it to your project.

     

    (Also for future reference please submit any new issues/questions to here:  https://github.com/eneifert/ZendDeskApi/issues)

    Hope this helps,

    Eric

  • Avatar
    Mikhail Zakharov

    Eric,

    Thanks for assist. I'll try to work with references to Newtonsoft.Json.Net35. If I find a bug I'll post it to GitHub

  • Avatar
    Harpreet Kaur

    please send me completeasp.net application integrated with zendesk,

     

    or kindly send me links from where i maka asp.net application using zendesk.

    because i am very new in this line.

     

    i am serching zendesk application in asp.net but cant find that how to make it step wise. kindly help me, its urgent

     

  • Avatar
    Mikhail Zakharov

    Hi Eric,

    Thanks fo advice. Problem is in wrong reference.

  • Avatar
    Eric Neifert

    Hello Harpreet,

    Your question goes a little beyond the scope of this forum but I will try to give you some direction.  

    If you are new to asp.net mvc you best place to go is  http://tekpub.com/channels/microsoft Watch his series on mastering asp.net mvc and also try out his examples and you will be an asp.net guru in no time.

    If you are working with asp.net web forms, I would strongly recommend switching to mvc if possible (in my humble opinion: this is where the asp.net industry is going and will make you a more valuable developer), but if you are set on web forms I guess you should start out by finding some good tutorials on the web. Here is one I found which might help you:  http://webproject.scottgu.com/CSharp/HelloWorld/Helloworld.aspx 

    Now on to using the zendesk api. Download all the dlls from here: https://github.com/eneifert/ZendDeskApi/tree/master/Output. Then add them as references in your project.

    Now in your code where you want to use the api just instantiate it like this: ZenDeskApi.ZenDeskApi _api = new ZenDeskApi.ZenDeskApi("http://yousite.zendesk.com", "your_admin_email_here",

                                                                       "password here");

    As for what you can do from there I would say to check out the readme file at:  https://github.com/eneifert/ZendDeskApi or look through the test class at:  https://github.com/eneifert/ZendDeskApi/blob/master/ZenDeskTests/ZenDeskTests.cs for more examples of what the api can do.

    Best of luck,

    Eric 

  • Avatar
    Eric Neifert

    This project is now on Nuget and should make things easier to install in the future. 

    To use in Visual Studio just right click on "References" and select "Add Library Package Reference". Then search for Zendesk in online section, click install and you are good to go!

    3 cheers for Nuget!

  • Avatar
    Dmitry Kirillov

    Hello Eric. Great job.

    I tried to use your api in the following way:

    User user = api.GetUserById(66705667);

    Organization org = api.GetOgranizationById(20131763);

    org.Users = new List<User> {user};

    api.CreateOrUpdateOrganization(org);

    So i need to add an user to an organization. But it does not work. Could you please advice whether it's possible and if it is what I do wrong?

    Dima.

  • Avatar
    Dmitry Kirillov

    I found an issue. Some properties in User class were not serialized because of Skip = true 

Please sign in to leave a comment.