C# for Uploading Documents in Rossum API

This C# code shows how to upload a document automatically using Rossum API to a particular Rossum queue.

This C# code demonstrates how to upload a document using Rossum API to a particular Rossum queue. Set the options at the beginning of the script – they store your credentials, and also the identifier of the queue to upload in. You can find out the queue id by looking at your main screen URL, it is the number 12345 in https://example.rossum.app/annotations/12345.

See the reference API documentation for more details about how to use the upload API.

<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.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Web;
using Newtonsoft.Json;

namespace rossum_api_examples_c_sharp
{
    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"
            });
            Console.WriteLine("auth key: " + authToken);

            var queue_id = "12345";
            
            rossumClient.upload(queue_id, "document.pdf", authToken);
        }

        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 void upload(string queueId, string filePath, string authToken) {
            FileStream fs = File.OpenRead(filePath);
            Console.WriteLine("Submitting invoice: " + filePath);
            var ext = Path.GetExtension(filePath).ToLower();
            var mediaType = (ext == ".png") ? "image/png" : "application/pdf";
            var fileContent = new StreamContent(fs);
            Console.WriteLine("Media type: " + mediaType);
            fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse(mediaType);
            var multiPartContent = new MultipartFormDataContent {
                {fileContent, "content", Path.GetFileName(filePath)}
            };
            
            HttpClient client = new HttpClient();
            client.DefaultRequestHeaders.Add("Authorization", $"token {authToken}");
            client.DefaultRequestHeaders.Add("ContentType", "application/json");
            
            var response = client.PostAsync($"{ApiEndpoint}queues/{queueId}/upload", multiPartContent).Result;
            
            int statusCode = (int)response.StatusCode;
            Console.WriteLine($"HTTP status code: {statusCode} ({response.StatusCode})");
            var resultJson = response.Content.ReadAsStringAsync().Result;

            Console.WriteLine(statusCode < 400 ? "Document submitted." : "Error!");
            Console.WriteLine("Response: " + resultJson);
        }
    }
}