Generate .NET Client from Swagger

You may want to try OpenAPI Generator (https://github.com/OpenAPITools/openapi-generator), which supports both OpenAPI spec v2 and v3. (OpenAPI spec v2 was formerly known as Swagger spec v2)

There are 3 different C# client generators:

  • csharp
  • csharp-netcore
  • csharp-dotnet2

The project also includes 2 C# server generators: csharp-nancyfx, aspnetcore

If you need help, please open an issue in the Github repo.

Disclosure: I'm the top contributor to both OpenAPI Generator and Swagger Codegen.


You can use the swagger-codegen tool from the swagger project. It produces C# files that use

  • RestClient for the HTTP calls
  • Newtonsoft.Json for json marshalling
  • .NET DataContract for the models.

You can either download the cli app or use the online editor. The petstore example models look like this:

using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization;
using Newtonsoft.Json;

namespace IO.Swagger.Model {

    /// <summary>
    /// 
    /// </summary>
    [DataContract]
    public class Order {
      /// <summary>
      /// Gets or Sets Id
      /// </summary>
      [DataMember(Name="id", EmitDefaultValue=false)]
      [JsonProperty(PropertyName = "id")]
      public long? Id { get; set; }

      /// <summary>
      /// Gets or Sets PetId
      /// </summary>
      [DataMember(Name="petId", EmitDefaultValue=false)]
      [JsonProperty(PropertyName = "petId")]
      public long? PetId { get; set; }
 .... snip ....
}