Skip to main content

C# Sample — Context API

Full workflow: authenticate → get presigned URL → upload file → submit enrichment → poll results.

Download the C# Notebook (.ipynb)

Setup

using System;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;

const string ContextApiUrl = "https://knowledge-enrichment.ai.app.hyland.com/latest/api/context-enrichment"; // replace with your environment URL
const string OAuthUrl = "https://auth.app.hyland.com/idp";
const string ClientId = "YOUR_CLIENT_ID";
const string ClientSecret = "YOUR_CLIENT_SECRET";

HttpClient httpClient = new();

Workflow

1. Get access token

async Task<string> GetAccessToken()
{
var body = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("grant_type", "client_credentials"),
new KeyValuePair<string, string>("scope", "environment_authorization")
});
string credentials = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{ClientId}:{ClientSecret}"));
httpClient.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Basic", credentials);

var response = await httpClient.PostAsync($"{OAuthUrl}/connect/token", body);
response.EnsureSuccessStatusCode();
var json = JsonDocument.Parse(await response.Content.ReadAsStringAsync());
return json.RootElement.GetProperty("access_token").GetString()!;
}

string token = await GetAccessToken();

2. Get presigned upload URL

async Task<(string PresignedUrl, string ObjectKey)> GetPresignedUrl(string token, string contentType)
{
httpClient.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Bearer", token);

var encoded = Uri.EscapeDataString(contentType);
var response = await httpClient.GetAsync($"{ContextApiUrl}/files/upload/presigned-url?contentType={encoded}");
response.EnsureSuccessStatusCode();
var json = JsonDocument.Parse(await response.Content.ReadAsStringAsync());
return (
json.RootElement.GetProperty("presignedUrl").GetString()!,
json.RootElement.GetProperty("objectKey").GetString()!
);
}

var (presignedUrl, objectKey) = await GetPresignedUrl(token, "application/pdf");
Console.WriteLine($"objectKey: {objectKey}");

3. Upload file

async Task UploadFile(string presignedUrl, string filePath, string contentType)
{
byte[] fileBytes = await File.ReadAllBytesAsync(filePath);
var content = new ByteArrayContent(fileBytes);
content.Headers.ContentType = new MediaTypeHeaderValue(contentType);

using var s3Client = new HttpClient();
var response = await s3Client.PutAsync(presignedUrl, content);
response.EnsureSuccessStatusCode();
Console.WriteLine("Upload complete");
}

await UploadFile(presignedUrl, "input/document.pdf", "application/pdf");

4. Submit enrichment job

async Task<string> Process(string token, string objectKey)
{
var body = JsonSerializer.Serialize(new
{
version = "context.api/v2",
objectKeys = new[] { new { path = objectKey } },
actions = new
{
textSummarization = new
{
maxWordCount = 150
},
textClassification = new
{
classes = new[] { "Report", "Contract", "Invoice", "Other" }
}
}
});
var content = new StringContent(body, Encoding.UTF8, "application/json");

httpClient.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Bearer", token);

var response = await httpClient.PostAsync($"{ContextApiUrl}/content/process", content);
response.EnsureSuccessStatusCode();
var json = JsonDocument.Parse(await response.Content.ReadAsStringAsync());
return json.RootElement.GetProperty("processingId").GetString()!;
}

string processingId = await Process(token, objectKey);
Console.WriteLine($"Processing ID: {processingId}");

5. Poll results

async Task<JsonDocument> PollResults(string token, string processingId, int intervalMs = 3000, int timeoutMs = 300000)
{
int elapsed = 0;
httpClient.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Bearer", token);

while (elapsed < timeoutMs)
{
var response = await httpClient.GetAsync($"{ContextApiUrl}/content/process/{processingId}/results");

if (response.StatusCode == System.Net.HttpStatusCode.Accepted)
{
Console.WriteLine("Processing in progress...");
await Task.Delay(intervalMs);
elapsed += intervalMs;
continue;
}

response.EnsureSuccessStatusCode();
var json = JsonDocument.Parse(await response.Content.ReadAsStringAsync());
return json;
}
throw new TimeoutException($"Job {processingId} did not complete within timeout");
}

var results = await PollResults(token, processingId);
Console.WriteLine(results.RootElement.GetRawText());