Skip to content
Alex Wichmann edited this page May 25, 2025 · 3 revisions

The AsyncAPI.NET library is a document object model (DOM) for an AsyncAPI description document. It provides a set of classes for constructing semantically valid AsyncAPI specifications. The library also includes a writer for serializing the DOM into JSON or YAML AsyncAPI specifications

Getting started

var myFirstAsyncApi = new AsyncApiDocument
{
    Info = new AsyncApiInfo
    {
        Title = "my first asyncapi",
        Version = "1.0.0",
    },
    Channels = new Dictionary<string, AsyncApiChannel>
    {
        {
            "UserSignup", new AsyncApiChannel
            {
                Address = "user/signedUp",
                Messages = new Dictionary<string, AsyncApiMessage>()
                {
                    {
                        "UserMessage", new AsyncApiMessage
                        {
                            Payload = new AsyncApiJsonSchema()
                            {
                                Type = SchemaType.Object,
                                Properties = new Dictionary<string, AsyncApiJsonSchema>()
                                {
                                    {
                                        "displayName", new AsyncApiJsonSchema()
                                        {
                                            Type = SchemaType.String,
                                            Description = "Name of the user",
                                        }
                                    },
                                },
                            },
                        }
                    },
                },
            }
        },
    },
    Operations = new Dictionary<string, AsyncApiOperation>()
    {
        {
            "ConsumerUserSignups", new AsyncApiOperation
            {
                Action = AsyncApiAction.Receive,
                Channel = new AsyncApiChannelReference("#/channels/UserSignup"),
            }
        },
    },
};
var yamlV2 = myFirstAsyncApi.SerializeAsYaml(AsyncApiVersion.AsyncApi2_0);
// asyncapi: 2.6.0
// info:
//   title: my first asyncapi
//   version: 1.0.0
// channels:
//   user/signedUp:
//     publish:
//       message:
//         payload:
//           type: object
//           properties:
//             displayName:
//               type: string
//               description: Name of the user

var yamlV3 = myFirstAsyncApi.SerializeAsYaml(AsyncApiVersion.AsyncApi3_0);
// asyncapi: 3.0.0
// info:
//   title: my first asyncapi
//   version: 1.0.0
// channels:
//   UserSignup:
//     address: user/signedUp
//     messages:
//       UserMessage:
//         payload:
//           type: object
//           properties:
//             displayName:
//               type: string
//               description: Name of the user
// operations:
//   ConsumerUserSignups:
//     action: receive
//     channel:
//       $ref: '#/channels/UserSignup'
// components: { }

Reading

var httpClient = new HttpClient
{
  BaseAddress = new Uri("https://raw.githubusercontent.com/asyncapi/spec/"),
};

var stream = await httpClient.GetStreamAsync("master/examples/streetlights-kafka.yml");
var asyncApiDocument = new AsyncApiStreamReader().Read(stream, out var diagnostic);

You can read more about more advanced ways to of reading or writing in the Wiki

Clone this wiki locally