Skip to content

Latest commit

 

History

History
142 lines (90 loc) · 12.2 KB

asyncapi.md

File metadata and controls

142 lines (90 loc) · 12.2 KB

AsyncAPI

AsyncAPI is a specification for defining and documenting asynchronous APIs. It provides a common, human- and machine-readable format for describing the channels, messages, and bindings of an API that uses a publish/subscribe or event-driven architecture.

AsyncAPI is designed to be easy to read and understand, both for humans and for tools and systems that need to consume the specification. It uses a familiar and intuitive YAML-based syntax, and is based on proven concepts and standards from the world of messaging and event-driven systems.

AsyncAPI can be used to define the channels, messages, and bindings of an API in a way that is independent of the specific technology or platform used to implement the API. This makes it possible to use the AsyncAPI document as a blueprint or specification for generating code, documentation, and other artifacts that can be used to implement the API in a consistent and interoperable way.

Specification

The specification is the document or set of documents that define the structure and content of an AsyncAPI document. It describes the elements, attributes, and other details that can be used to define the channels, messages, and bindings of an asynchronous API.

The AsyncAPI specification is written in a human-readable format, using YAML as the underlying syntax. It is organized into several sections, each of which describes a different aspect of an AsyncAPI document, such as the metadata, channels, messages, bindings, and other elements.

The specification is designed to be comprehensive and self-contained, providing all the necessary information and examples to help users understand and implement AsyncAPI. It is regularly updated and maintained by the AsyncAPI community, to ensure that it remains relevant and useful for a wide range of API projects.

Channels

In the context of AsyncAPI, a channel refers to a logical communication path for publishing and subscribing to messages. A channel can represent a message queue, a pub/sub topic, or any other mechanism for sending and receiving messages asynchronously.

Channels are defined in an AsyncAPI document using the channels element. Each channel has a unique name and is associated with a message payload schema, which defines the structure and format of the messages that can be published or subscribed to on that channel.

The messages that are published on a channel are typically triggered by some event or action, such as the creation of a new resource or the completion of a background job. The messages can then be consumed by any number of subscribers, who can use the information in the message to perform some action or update their own state.

Overall, channels provide a flexible and scalable way to exchange information between different parts of an application or system asynchronously.

Publish vs Subscribe

In the context of messaging and event-driven architectures, the terms "publish" and "subscribe" refer to two different ways of exchanging information between different parts of an application or system.

Publishing refers to the act of sending out a message on a specific channel or topic. This is typically done by a publisher, which is a piece of code or application that is responsible for generating and transmitting the message.

Subscribing, on the other hand, refers to the act of receiving and processing a message that has been published on a channel or topic. This is typically done by a subscriber, which is a piece of code or application that is interested in receiving and reacting to the messages published on a particular channel.

In a publish/subscribe system, publishers and subscribers do not directly communicate with each other. Instead, they interact with a messaging system or broker, which is responsible for routing messages from publishers to the appropriate subscribers. This allows for a loosely coupled and scalable architecture, where publishers and subscribers can be added or removed without affecting the overall system.

Naming Standard

There is no universal naming standard for AsyncAPI documents or the elements within them. However, it is generally recommended to use descriptive and meaningful names that are clear and easy to understand.

For example, when defining channels, you can use names that reflect the purpose or functionality of the channel, such as user-created or order-processed. When defining message payloads, you can use names that reflect the content or structure of the payload, such as user or order.

It is also a good idea to follow the naming conventions of the programming language or platform you are using. For example, if you are working with JavaScript, you can use camelCase for naming elements, while if you are working with Python, you can use snake_case. This will help ensure consistency and avoid naming conflicts.

Overall, the important thing is to choose names that are clear, concise, and relevant to the element being defined. This will make your AsyncAPI document easier to read and understand for yourself and others who may use it.

Bindings

Bindings refer to the specific protocols and technologies used to implement an API and exchange messages between different parts of an application or system.

Bindings are defined in an AsyncAPI document using the bindings element. Each binding is associated with a particular channel and defines the specific details of how messages will be published and subscribed to on that channel. This can include information such as the transport protocol, the message format, and any additional properties or settings needed to support the binding.

For example, you might define a binding for a channel that uses the AMQP protocol and JSON message format. This would specify the details of how messages will be sent and received using AMQP, and how the message payloads will be structured and encoded in JSON.

Overall, bindings provide a way to connect an AsyncAPI document to the underlying technology and infrastructure used to implement an API. This allows the document to be used as a blueprint or specification for generating code and other artifacts that can be used to implement the API in a consistent and interoperable way.

Schema

There are several benefits to using a schema in an AsyncAPI document:

  • A schema provides a common and agreed-upon format for defining the structure and content of message payloads. This ensures that publishers and subscribers can communicate effectively and understand the messages that are being exchanged.
  • A schema can be used to validate the message payloads to ensure that they conform to the expected format and structure. This helps to prevent errors and ensure that the messages are correctly processed by the subscribers.
  • A schema can be used to generate code or other artifacts, such as documentation, from an AsyncAPI document. This can help to speed up the development process and ensure that the API is implemented consistently across different platforms and languages.
  • A schema can be used to evolve and extend the message payloads over time, without breaking compatibility with existing publishers and subscribers. This allows the API to evolve and adapt to changing requirements and business needs.

Overall, using a schema in an AsyncAPI document can help to improve the reliability, interoperability, and maintainability of an API and the systems it connects.

Schema format

The schema used in an AsyncAPI document can be defined in a variety of formats, including JSON Schema and XML Schema. The most commonly used format is JSON Schema, as it is well-suited to defining the structure and content of JSON-formatted message payloads.

JSON Schema is a popular and widely-used format for defining the structure and content of JSON objects. It is well-suited to defining the message payloads used in an AsyncAPI document, as many APIs use JSON as the format for their message payloads.

Using JSON Schema in an AsyncAPI document has several benefits, including:

It allows you to define the structure and content of your message payloads in a clear and standardized way. This ensures that publishers and subscribers can communicate effectively and understand the messages that are being exchanged.

It provides a way to validate the message payloads to ensure that they conform to the expected format and structure. This helps to prevent errors and ensure that the messages are correctly processed by the subscribers.

It can be used to generate code or other artifacts from an AsyncAPI document. This can help to speed up the development process and ensure that the API is implemented consistently across different platforms and languages.

It can be used to evolve and extend the message payloads over time, without breaking compatibility with existing publishers and subscribers. This allows the API to adapt and respond to changing requirements and business needs.

Example

asyncapi: 2.0.0
info:
  title: My API
  version: 1.0.0
channels:
  user-created:
    publish:
      message:
        payload:
          type: object
          properties:
            user:
              type: object
              properties:
                id:
                  type: string
                  format: uuid
                name:
                  type: string
    subscribe:
      message:
        payload:
          type: object
          properties:
            user:
              type: object
              properties:
                id:
                  type: string
                  format: uuid

This AsyncAPI document defines a single channel called user-created, which is used to publish and subscribe to messages when a new user is created. The channel has a single publish operation and a single subscribe operation, both of which use the same message payload schema. The schema defines the structure and format of the message payload, which includes an id field of type string and a name field of type string.

Overall, this is a simple and straightforward AsyncAPI document that illustrates the basic elements and structure of an AsyncAPI specification. Of course, in a real-world API, the specification would likely be more complex and include additional channels, messages, bindings, and other details.

Getting Started

  • First, familiarize yourself with the AsyncAPI specification. This will help you understand the basic structure and elements of an AsyncAPI document.
  • Next, identify the channels and messages you want to define in your AsyncAPI document. For each channel, determine the message payloads that will be published and the message payloads that will be subscribed to.
  • Define the channels and messages in your AsyncAPI document using the appropriate elements and syntax. Be sure to include any necessary metadata, such as the message schema and payload format.
  • Use the AsyncAPI tools, such as the AsyncAPI generator, to validate and verify your document. This will ensure that your document is well-formed and conforms to the specification.
  • Publish your AsyncAPI document so that others can use it to integrate with your API. This can be done by hosting the document on a website or sharing it through a collaboration tool.
  • Use the AsyncAPI document to generate client and server code, as needed. This can help speed up the development process and ensure that your API is implemented consistently across different platforms and languages.
  • Monitor and maintain your AsyncAPI document over time to ensure that it stays up-to-date and accurate. This may involve making updates as your API evolves, or responding to feedback from users.

Open Source & Community

AsyncAPI is an open-source specification. It is available under the Apache License 2.0, which allows for use, modification, and distribution of the specification without any restrictions.

The source code for AsyncAPI and the related tools and libraries are hosted on GitHub, where anyone can contribute to the development and improvement of the specification. This open and collaborative approach has helped to make AsyncAPI a widely-used and widely-supported specification for defining and documenting asynchronous APIs.

Additionally, there is an active community of users and contributors who are involved in the development and evolution of AsyncAPI. This community provides support and resources for using and implementing AsyncAPI, and helps to ensure that the specification remains relevant and useful for a wide range of API projects.