C# for Exporting Data from Rossum API

This C# code shows how to download JSON data captured in a given Rossum queue for the previous calendar day.

This C# code shows how to download JSON data captured in a given queue for the previous calendar day – as part of the regular export routine. Update the parameters in the main() function based on your scenario.

The export API is more powerful - you can filter by different criteria or download the data e.g. as a XML instead - see the full docs.

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.1</TargetFramework>
    <RootNamespace>rossum_example</RootNamespace>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Newtonsoft.Json" Version="9.0.1" />
  </ItemGroup>

</Project>
using System;
using System.Collections.Specialized;
using System.Net;
using System.Web;
using Newtonsoft.Json;

namespace rossum_example
{
    class Credentials {
        public string username { get; set; }
        public string password { get; set; }
    }
    class LoginResponse {
        public string key { get; set; }
    }

    class RossumClient
    {
        static string ApiEndpoint = "https://example.rossum.app/api/v1/";
        static void Main(string[] args)
        {
            var rossumClient = new RossumClient();
            
            string authToken = rossumClient.authorize(new Credentials() {
                username="[email protected]",
                password="secret_password"
            });

            var queueId = "12345";
            var exported = rossumClient.exportToday(queueId, authToken);
            Console.WriteLine(exported);
        }

        public string authorize(Credentials credentials) {
            
            using(WebClient client = new WebClient()) {  
                client.BaseAddress = ApiEndpoint;
                client.Headers[HttpRequestHeader.ContentType] = "application/json"; 
                var response = client.UploadString("auth/login", JsonConvert.SerializeObject(credentials));
                return JsonConvert.DeserializeObject<LoginResponse>(response).key;
            }
        }

        public string export(string queueId, string dateStart, string dateEnd, string authToken) {
            using(WebClient client = new WebClient()) {  
                client.BaseAddress = ApiEndpoint;

                client.Headers["Authorization"] =  $"token {authToken}";
                client.Headers[HttpRequestHeader.ContentType] = "application/json";

                NameValueCollection query = new NameValueCollection();
                query.Add("format", "json");
                query.Add("exported_at_after", dateStart);
                query.Add("exported_at_before", dateEnd);
                client.QueryString = query;

                return client.DownloadString($"queues/{queueId}/export");
            }
        }

        public string exportToday(string queueId, string authToken) {
            DateTime today = DateTime.Today;
            var dateEnd = today.ToString("yyyy-MM-dd");
            DateTime yesterday = today.AddDays(-1);
            var dateStart = yesterday.ToString("yyyy-MM-dd");
            return export(queueId, dateStart, dateEnd, authToken);
        }
    }
}

This example will export at most 100 documents. To export more documents, you need to modify the code download multiple pages one by one, until it reaches the page count (or it can simply stop when an empty set of results is finally downloaded).