Let's delve deeper into these components:
info: info section provides metadata about the API, such as its title and versionchannels: channels section defines the communication channels used by the API. Each channel corresponds to a specific topic or queue in the message broker. The channels section includes the type of messages sent on each topic/queuecomponents: components section defines reusable components that can be referenced elsewhere in the API definition. This can include message definitions, schemas, parameters, and more. By defining these components in one place, you can maintain consistency across your APItags: tags section allows you to categorize operations in your API. It can be useful for grouping related operations together, making the API easier to navigate and understand.Operations: operations are defined within channels. For example, asendoperation might be defined for a channel to specify that messages are sent by that channel.Messages: Messages are defined within operations and specify the content of the messages that are sent or received on a channel. Each message can have aname,title,summary,contentType, andpayload. Thepayloaddescribes the structure of the message content, and it can reference a schema defined in thecomponentssectionSchemas: Schemas are defined in thecomponentssection and describe the structure of message content. They can be referenced by messages to define their structure. Schemas can be simple types likestringorinteger, or they can be complex types likeobjectorarray
A: Writing an AsyncAPI document from scratch involves several steps. This process can be broken down into the following parts:
- Defining the AsyncAPI version and general information The first section of the AsyncAPI document specifies the AsyncAPI version and general information about the API. This includes the title, version, and description of the API. For example:
asyncapi: 3.0.0
info:
title: Sending Signal to Eve
version: 0.1.0In this section, asyncapi: 3.0.0 specifies that the document is written following version 3.0.0 of the AsyncAPI specification. The info: section provides more detail about the API itself.
- Defining the channels and messages The next part involves defining the channels and messages that the API uses. Channels represent the routes where messages are sent or received, while messages represent the data being transferred. For example:
channels:
userSignedUp:
address: Earth/letter
messages:
lettertoEarth:
description: Communicating with Eve.
payload:
type: object
additionalProperties: false
properties:
fullName:
type: string
email:
type: string
format: email
age:
type: integer
minimum: 18In this case, the document specifies a userSignedUp channel with an address of Earth/letter. The lettertoEarth message is sent on this channel and includes the user's full name, email, and age in the payload.
- Defining the operations
Lastly, operations represent the actions that can be performed on channels. They specify whether the API sends (
publish) or receives (subscribe) messages on a particular channel. For example:
operations:
userSignedUp:
action: send
channel:
$ref: '#/channels/lettertoEarth'Here, the API is specified to send (action: send) messages on the userSignedUp channel.
Q: Using AsyncAPI tools for validating the document?
A: AsyncAPI studio.
Q: Hands-on exercises to create and validate AsyncAPI documents?
A: Here's an exercise you can do to practice creating and validating AsyncAPI documents:
Using the example AsyncAPI document provided, modify the payload section to add a new property for username of type string.
Add a new channel named userLoggedIn with an address of Mars/letter and a message named lettertoMars.
Validate your updated AsyncAPI document using the AsyncAPI studio.
Remember to carefully follow the AsyncAPI specification when adding new components to your document.