Creating a C# Client¶
KL.APIClient is the C# client for the KnowledgeLake API. You generate it yourself from your tenant's OpenAPI spec, with the same generator and options the platform uses. Each API action becomes a method, and each request and response becomes a typed class. Because it comes from your own tenant's spec, the client always matches your tenant's release: generate it again after an upgrade to pick up new actions.
At a glance
- Source Your tenant's spec, served at
https://{tenant}/api/OpenAPI/V2_JSON/V2_JSON?domain=https://{tenant}(no token needed) - Generator openapi-generator 7.12.0, the
csharpgenerator with therestsharplibrary - Output A
netstandard2.1project namedKL.APIClient(works in .NET 6, 8 and later) - Needs Docker, or Java 11 or later; and the .NET SDK
- Examples on this site Every action in the API Reference has a C# example written for this client
graph TD
S["Your tenant's OpenAPI spec"] --> G["openapi-generator 7.12.0"]
G --> P["KL.APIClient project"]
P --> A["Your app: Configuration, Api classes"]
A -->|"TempIdentityToken header"| K["KnowledgeLake API"]
Generate the client¶
-
Download the spec from your tenant. Replace
yourcompany.knowledgelake.iowith your tenant host: -
Run the generator. Use version 7.12.0 and these options exactly: they are the ones the platform build uses, so the class, method and property names match the examples on this site.
docker run --rm -v "$PWD:/local" openapitools/openapi-generator-cli:v7.12.0 generate -i /local/openapi.v2.json -g csharp -o /local/KLClient --global-property apiDocs=false,modelDocs=false,modelTests=false --additional-properties packageName=KL.APIClient,targetFramework=netstandard2.1,library=restsharp,zeroBasedEnums=trueDownload
openapi-generator-cli-7.12.0.jarfrom Maven Central (https://repo1.maven.org/maven2/org/openapitools/openapi-generator-cli/7.12.0/) and check it against the.sha1file next to it. Then run:The generator writes the project to
KLClient/src/KL.APIClient/KL.APIClient.csproj. -
Reference the generated project from your app:
To share the client across several solutions instead, pack it once and add the package from a local folder:
dotnet pack KLClient/src/KL.APIClient -o ./packages. -
Run
dotnet build. A warning that theSystem.Webreference cannot be resolved comes from the generated project and is harmless.
Keep the client in step with your tenant
- Generate the client again after your tenant is upgraded, so new actions appear as methods.
- Keep the generator at 7.12.0. A different generator version can rename methods and properties, and then the examples on this site no longer match.
- Commit the spec you generated from next to the client, so you can see what changed at the next upgrade.
The client in three lines¶
using KL.APIClient.Client;
using KL.APIClient.Model;
var config = new Configuration { BasePath = "https://yourcompany.knowledgelake.io/api" };
config.AddApiKey("TempIdentityToken", Environment.GetEnvironmentVariable("KnowledgeLakeApiToken"));
var workItems = new KL.APIClient.Api.V1KWorkItemAPIApi(config);
- Base path
BasePathis your tenant URL plus/api. - Token header
AddApiKey("TempIdentityToken", token)sends the token header on every call. Read the token from configuration (an environment variable, an app setting, a key vault). Never put it in code. - API classes Make one API class per group you call, all from the same
Configuration.
A first call:
var response = await workItems.V1KWorkItemAPIControllerGetWorkItemAsync(
new PaasWorkItemIdDTO { WorkItemId = Guid.Parse("3f2b8c1e-0d5a-4c7e-9a43-6b1f2e8d9c10") });
if (!response.Success)
{
throw new Exception("GetWorkItem failed: " + response.MessageKey);
}
foreach (var document in response.Data.Documents)
{
Console.WriteLine(document.Id);
}
Naming rules¶
The names come from the spec, so you can predict them.
| Spec | Client |
|---|---|
Group V1_KWorkItemAPI |
Class KL.APIClient.Api.V1KWorkItemAPIApi (underscore removed, Api added) |
Group V1_WorkflowAPI |
Class V1WorkflowAPIApi |
Group V1_RepositoryAPI |
Class V1RepositoryAPIApi |
Group V1_JobsAPI |
Class V1JobsAPIApi |
Group V1_RPAAPI |
Class V1RPAAPIApi |
Action GetWorkItem in V1_KWorkItemAPI |
Method V1KWorkItemAPIControllerGetWorkItemAsync |
| Pattern | {Group without underscore}Controller{Action}Async |
| Request and response types | The definition names in the spec, for example PaasWorkItemIdDTO, UpsertPaasDocumentMetadataParameter, MetadataValueDTO, DataResponseSavedSearchResultDTOCollection |
Each method also has a synchronous form (no Async suffix) and a WithHttpInfo form that returns the status code and headers.
Every action has a C# example
Each action in the API Reference opens on a C# (KL.APIClient) tab that shows the exact class, method and request type for that action. The examples are compiled against a client generated from the current spec with the same generator and options the platform build uses.
The parameter types you use most with V1KWorkItemAPIApi:
PaasWorkItemIdDTO { WorkItemId }WorkItemCreationRequestParameter { WorkItemName, Metadata }UpsertPaasWorkItemMetadataParameter { WorkItemId, Metadata }UpsertPaasDocumentMetadataParameter { WorkItemId, DocumentId, Metadata }GetDocumentFileParameter { WorkItemId, DocumentId, DocumentFileId }MetadataValueDTO { Bucket, Key, Value }
Errors¶
- HTTP 200 The response has
.Success,.MessageKeyand.Data. Check.Successon every call. - HTTP 400 or higher A response with 401, 412, 422 or 500 throws
KL.APIClient.Client.ApiException.ErrorCodeis the HTTP status.ErrorContentis the response body, which holds the envelope withMessages[].MessageKey.
try
{
var r = await workItems.V1KWorkItemAPIControllerGetWorkItemAsync(new PaasWorkItemIdDTO { WorkItemId = workItemId });
if (!r.Success) throw new Exception("GetWorkItem failed: " + r.MessageKey);
}
catch (KL.APIClient.Client.ApiException e)
{
// e.ErrorCode is 412 for an application error, 401 for a token or feature problem.
Console.Error.WriteLine("HTTP " + e.ErrorCode + ": " + e.ErrorContent);
throw;
}
See the Introduction for what each status means.
Other languages¶
The same spec generates clients for other languages: openapi-generator has python, typescript-fetch, java and more, and NSwag (nswag openapi2csclient) is another option for C#. The examples on this site that are not C# use plain HTTP, so they work with any client. Whatever you generate, configure the token as an API key named TempIdentityToken in the header, as the spec declares it.
Two spec quirks to handle in a generated client
- The spec lists the failure envelope under status 400, but the server sends 412. Treat any 412 as the failure envelope and read
Messages[].MessageKey. V1_ProcessingAPI/QueryForWorkItemHistoriesnames its responseDataResponseBatchMonitorWorkItemDataDTO, but the items areWorkitemHistoryQueryResult. If deserialization drops fields, read that response as raw JSON.