Skip to content

Latest commit

 

History

History
101 lines (67 loc) · 4.47 KB

dotnet.md

File metadata and controls

101 lines (67 loc) · 4.47 KB
title description author ms.author ms.topic date
Build API clients for .NET
Learn how use Kiota to build API clients in .NET.
jasonjoh
jasonjoh
quickstart
03/20/2023

Build API clients for .NET

In this tutorial, you build a sample app in .NET that calls a REST API that doesn't require authentication.

Required tools

Create a project

Run the following command in the directory you want to create a new project.

dotnet new console -o KiotaPosts
dotnet new gitignore

Project configuration

In case you're adding a Kiota client to an existing project, the following configuration is required:

  • *.csproj > TargetFramework set to "netstandard2.0", "netstandard2.1", "net462", or "net6" or later. More information
  • *.csproj > LangVersion set to "preview", "latest", or "7.3" or later. More information

The following configuration is recommended:

  • *.csproj > Nullable set to "enable".

Add dependencies

Before you can compile and run the generated API client, you need to make sure the generated source files are part of a project with the required dependencies. Your project must have a reference to the abstraction package. Additionally, you must either use the Kiota default implementations or provide your own custom implementations of the following packages.

For this tutorial, use the default implementations.

Run the following commands to get the required dependencies.

dotnet add package Microsoft.Kiota.Abstractions
dotnet add package Microsoft.Kiota.Http.HttpClientLibrary
dotnet add package Microsoft.Kiota.Serialization.Form
dotnet add package Microsoft.Kiota.Serialization.Json
dotnet add package Microsoft.Kiota.Serialization.Text
dotnet add package Microsoft.Kiota.Serialization.Multipart

Generate the API client

Kiota generates API clients from OpenAPI documents. Create a file named posts-api.yml and add the following.

:::code language="yaml" source="~/code-snippets/get-started/quickstart/posts-api.yml":::

This file is a minimal OpenAPI description that describes how to call the /posts endpoint in the JSONPlaceholder REST API.

You can then use the Kiota command line tool to generate the API client classes.

kiota generate -l CSharp -c PostsClient -n KiotaPosts.Client -d ./posts-api.yml -o ./Client

Tip

Add --exclude-backward-compatible if you want to reduce the size of the generated client and are not concerned about potentially source breaking changes with future versions of Kiota when updating the client.

Create the client application

The final step is to update the Program.cs file that was generated as part of the console application to include the following code.

:::code language="csharp" source="~/code-snippets/get-started/quickstart/dotnet/src/Program.cs" id="ProgramSnippet":::

Note

The JSONPlaceholder REST API doesn't require any authentication, so this sample uses the AnonymousAuthenticationProvider. For APIs that require authentication, use an applicable authentication provider.

Run the application

To start the application, run the following command in your project directory.

dotnet run

See also