A week ago while I was browsing around what’s trending on Github, I stumbled upon something called Microsoft Kiota. Kiota is a command line tool for generating an API client to call any OpenAPI described API you are interested in. Getting started was quite a good experience as documentation from project Kiota is quite decent, especially for Building SDK’s in .NET.

Since Kiota is a .NET Tool and distributed on nuget.org, installation is as simple as

dotnet tool install --global --prerelease Microsoft.OpenApi.Kiota

The command line arguments for Kiota is very straight forward and looks something like this:

kiota generate -d [relative path to OpenAPI spec file] -n [default namespace] -o [relative output path]

The code generated by Microsoft Kiota depends on the following NuGet packages:

Kiota is built to target .NET 7.0 so this is required to run Kiota. The code C# generated by Kiota builds on the following .NET versions:

  • .NET 7.0 and 6.0
  • .NET Standard 2.1 and 2.0
  • .NET Framework 4.8.1, 4.8, 4.7.2, 4.6.2

All this perfectly fits the requirements I have in my Visual Studio extension, REST API Client Code Generator, so I immediately got started with integrating Kiota in my extension

After generating code, the extension will install the required NuGet packages and configure the project to use a Custom Tool on the OpenAPI Swagger file

Currently my tool just installs and runs the Kiota CLI tool, so there is a slight pause in Visual Studio while the custom tool is running, because Visual Studio needs to start an external process and wait for the results

Here’s an example of how to use the Kiota generated code from the Swagger Petstore OpenAPI specifications example.

using Microsoft.Kiota.Http.HttpClientLibrary;
using Microsoft.Kiota.Abstractions.Authentication;

var authProvider = new AnonymousAuthenticationProvider();
var requestAdapter = new HttpClientRequestAdapter(authProvider);
requestAdapter.BaseUrl = "https://petstore3.swagger.io/api/v3";

var client = new ApiClient(requestAdapter);
var pet = await client.Pet["0"].GetAsync(c => c.Headers.Add("accept", "application/json"));

Console.WriteLine($"Name: {pet.Name}");
Console.WriteLine($"Category: {pet.Category.Name}");
Console.WriteLine($"Status: {pet.Status}");

The code above outputs the following:

Name: doggie
Category: Dogs
Status: Available

It is the equivalent of calling the Swagger Petstore API using cURL

curl -X 'GET' 'https://petstore3.swagger.io/api/v3/pet/0' -H 'accept: application/json'

which responds with

{
   "id":0,
   "category":{
      "id":1,
      "name":"Dogs"
   },
   "name":"doggie",
   "photoUrls":[
      "string"
   ],
   "tags":[
      {
         "id":0,
         "name":"string"
      }
   ],
   "status":"available"
}

I think Kiota is really promising and I think that you should also give it a shot. You can use it via my Visual Studio extension REST API Client Code Generator, my CLI tool Rapicgen, or via the Kiota CLI Tool directly