DEV Community

Cover image for How to generate C# or TypeScript client code for OpenAPI (Swagger) specification
Chebotov Nickolay
Chebotov Nickolay

Posted on • Edited on

How to generate C# or TypeScript client code for OpenAPI (Swagger) specification

Alt Text

This How-To describes how to generate C# or TypeScript client code for OpenAPI (Swagger) specification with Unchase OpenAPI (Swagger) Connected Service to Visual Studio 2017/2019 projects.

The Unchase OpenAPI (Swagger) Connected Service is a Visual Studio 2017/2019 extension to generate C# (TypeScript) HttpClient (or C# Controllers) code for OpenAPI (formerly Swagger) web services with NSwag. Simply put, it is like kind old Add Service Reference for WCF or Add Web Reference for WSDL, but for JSON (YAML) API with customization of code generation like in NSwagStudio.

Follow *4 steps below to achieve this:

Alt Text

Step 1. Install the Connected Service

Launch the installed Visual Studio 2017/2019. Open the menu to manage Visual Studio extensions:

  • For Visual Studio 2017: Tools -> Extensions and Updates...

Alt Text

  • For Visual Studio 2019: Extensions -> Manage Extensions

Alt Text

Choose Online -> Visual Studio Marketplace tab. In search field type unchase, then install Unchase OpenAPI (Swagger) Connected Service (just click Download button):

Alt Text

After all, restart the Visual Studio to install the Connected Service.

Step 2. Open the Connected Service wizard for Visual Studio project

Open the Visual Studio solution with project to generate client code in. In Solution Explorer make right-click on Connected Services or Dependencies item in the project for which you need to generate client code, and click on Add Connected Service:

Alt Text

Alt Text

Then choose Unchase OpenAPI (Swagger) Connected Service to show the Connected Service wizard:

Alt Text

Alt Text

Step 3. Configure OpenAPI specification endpoint

In the first page of the wizard you need to configure some necessary options:

  • Service name - the name of the folder for generated files (by default, if field is empty - OpenAPIService);

  • Generated file name - the name of the generated files prefix (by default, if field is empty - OpenAPI);

  • OpenAPI (Swagger) specification URI (path) - the specification URI (URL or local file);

  • One or few of Generate CSharp Client, Generate TypeScript Client, Generate CSharp Controller - to generate C# (TypeScript) client code or/and C# Controllers code file(s) for OpenAPI specification. When you check a checkbox, the corresponding tab item with additional generation settings is added to the wizard:

Alt Text

After setting the necessary options, you can immediately start generating the code files by clicking the Finish button. In this case, there will choose default code generation options.


There are a few optional options you can specify before start generation process:

  • In the Runtime and variables section - the same options as in NSwagStudio:

Alt Text

  • Copy OpenAPI (Swagger) json-specification to nswag-file checkbox - the same as Create local Copy button in NSwagStudio:

Alt Text

  • Open generated files on complete in IDE checkbox - allows automatically open the generated files in IDE in generation process;

  • If you need to set network credentials for connecting to the endpoint (specification file), you should specify options in the Network Credentials section. If your network used Proxy, you should specify options in the Web Proxysection:

Alt Text

Step 4 (Optional). Configure code generation options

If you want to specify some options for generating code, just need to go to generation settings tab(s):

Alt Text

There are the same options like in NSwagStudio.


Enjoy programming with Unchase!

Top comments (0)