Search code examples
c#synology

How to use the Synology Calendar API to create events in C#?


I'm planning to provide a C# client for the Synology calendar API and I'm using the documentation provided by Synology directly. So far, I am able to do the following things in C# (And Postman for testing):

  • Login (With version 2+ and Syno-Token enabled, should work in version 1 without, but doesn't)
  • Logout
  • Get API information (No login required)
  • Get all calendars (E.g. List all calendars)
  • Get all events from a certain calendar in a time range
  • Set event data (E.g. Update an event)

What doesn't work for me, is the adding of an event. I'm trying the following request (Passwords and stuff replaced, of course):

GET {{baseUrl}}/webapi/entry.cgi?api=SYNO.Cal.Event&version=1&method=create&cal_id=test&description=Test2&dtstart=TZID%3dEurope%2fBerlin%3a20231003T210000&dtend=TZID%3dEurope%2fBerlin%3a20231003T230000&is_all_day=false&is_repeat_evt=false&evt_location=Test3&original_cal_id=test2&summary=Test4&transp=TRANSPARENT&tzid=Europe%2fBerlin

{{baseUrl}} is a Postman variable and should be replaced the Synology station base address, something like http://192.168.2.205:5000.

The only custom header I add is the value for X-SYNO-TOKEN you get from the login process.

With this request, I get a result that indicates no success and error code 9007. However, this error code is not documented in the Synology API description. I'm not really sure what I'm doing wrong, I have tested with less parameters already as well as with POST instead of GET (Which leads to an error that tells that the method is not supported). I have already checked some Synology forum entries and other pages, but didn't really find anything related by now.

Other "special" findings about the API:

  • All bool values must be lower case in the API (IS never stated in the docs)
  • All requests seem to be GET requests so far (Updating e.g. works as a GET request)
  • The documentation is quite difficult to read as the examples are either incomplete or wrong (e.g. POST instead of GET or stuff like this)

Solution

  • I'm now using plain CalDav via a custom created NuGet package from https://github.com/SeppPenner/CalDAVNet.

    Code example below:

    using Ical.Net.CalendarComponents;
    
    /// <summary>
    /// The main method.
    /// </summary>
    public static async Task Main()
    {
        // Create client.
        var calDavClient = new Client("http://192.168.2.2/caldav.php/user/someid", "user", "password");
    
        // Get all calendars for the user.
        var calendars = await calDavClient.GetAllCalendars();
    
        // Get the calendar by the uid.
        var calendarByUid = await calDavClient.GetCalendarByUid("/caldav.php/user/uniqueid/");
    
        // Get the default calendar.
        var defaultCalendar = await calDavClient.GetDefaultCalendar();
    
        // Add an event.
        var calendarEvent = new CalendarEvent();
        var added = await calDavClient.AddOrUpdateEvent(calendarEvent, new Ical.Net.Calendar());
    
        // Delete an event.
        var deleted = await calDavClient.DeleteEvent(calendarEvent);
    }