Top 10 Async API Solutions for Streamlining Your Workflow

Jennie Lee
9 min readApr 11, 2024

Looking for a Postman alternative?

Try APIDog, the Most Customizable Postman Alternative, where you can connect to thousands of APIs right now!

Introduction to Async API Documentation

Async API documentation is a crucial aspect of event-driven systems, as it helps in streamlining the workflow of applications that rely on asynchronous communication. In such systems, events play a significant role in enabling different components to communicate and respond to each other’s actions promptly. Async API documentation helps in defining, documenting, and visualizing these events, making it easier for developers to understand and work with them.

In event-driven systems, events are typically exchanged through messaging systems like Kafka or RabbitMQ. These events represent various interactions between different components of the system. The Async API specification describes the structure and behavior of these events, enabling developers to integrate their applications seamlessly.

Importance in Event-Driven Systems

Async API documentation provides a standardized way of documenting the events used in event-driven systems, making it easier for developers to understand and work with these events. It acts as a contract between different components of the system, ensuring that they communicate effectively.

By having clear documentation for the events, developers can easily understand the purpose and structure of each event, making it easier to build or maintain functionalities that rely on them. It also helps in reducing the learning curve for new developers joining the project, as they can quickly refer to the Async API documentation to grasp the event-driven architecture of the system.

Overview of Channels and Components

In the context of Async API documentation, channels represent the topics or message queues where events are published and consumed. Each channel consists of one or more messages with unique names. These messages define the structure and content of the events.

Components, on the other hand, contain the schema definitions used within the messages. They define the data transfer objects (DTOs) and their properties, which represent the structure of the event payload. Components are reusable entities that simplify the event specification by allowing developers to define common schemas once and reference them across different messages.

The Role of DTO Schemas

DTOs play a crucial role in Async API documentation as they define the structure of the event payload. They determine the properties and their types, ensuring that the published and consumed events are consistent across different components.

DTO schemas can be defined using various formats, such as JSON Schema or OpenAPI. These schemas help in validating the event payload and provide a contract for both the publisher and the consumer. By enforcing a well-defined structure, potential issues, such as data inconsistency or type mismatches, can be mitigated.

Example of an Async API Specification in JSON Format

To have a better understanding of the Async API specification, let’s take a look at an example in JSON format:

{
"asyncapi": "2.0.0",
"info": {
"title": "Order Management System",
"version": "1.0.0"
},
"channels": {
"orders": {
"publish": {
"message": {
"headers": {},
"payload": {
"type": "object",
"properties": {
"orderId": {
"type": "string",
"description": "The ID of the order"
},
"customerId": {
"type": "string",
"description": "The ID of the customer who placed the order"
},
"totalAmount": {
"type": "number",
"description": "The total amount of the order"
}
},
"required": ["orderId", "customerId", "totalAmount"]
}
}
}
}
},
"components": {}
}

In this example, the Async API specification defines an order management system. The “info” section provides the title and version of the API. The “channels” section contains a channel named “orders”, which represents the topic or queue where order events are published.

Within the “orders” channel, there is a “publish” message. The message includes the structure of the event payload, defined by the “payload” property. In this case, the payload is an object with properties like “orderId”, “customerId”, and “totalAmount”. Each property has a type and a description.

The “required” property specifies the fields that are mandatory in the event payload. In this example, “orderId”, “customerId”, and “totalAmount” are required fields. By providing this information, the Async API specification ensures that publishers and consumers adhere to this structure when working with the “orders” channel.

The “components” section is currently empty, but it can be used to define reusable schemas for common properties or objects used across different messages.

Autogenerating Async API Documentation

Writing Async API documentation manually can be tedious and error-prone. To streamline the process, we can leverage tools and libraries to generate the documentation automatically from our codebase. Let’s walk through the process step by step.

Step 1: Defining DTOs using Decorators from “@nestjs/swagger”

In this example, we will use the popular NestJS framework, which provides seamless integration with the Swagger ecosystem. Using decorators from the “@nestjs/swagger” package, we can define the DTOs and their properties directly within our codebase.

import { ApiProperty } from '@nestjs/swagger';

export class OrderDto {
@ApiProperty()
orderId: string;

@ApiProperty()
customerId: string;

@ApiProperty()
totalAmount: number;
}

In this code snippet, we define an OrderDto class, representing the structure of the order event payload. Each property is annotated with the @ApiProperty() decorator, which helps in generating the corresponding Async API documentation.

Step 2: Generating OpenAPI Documentation from DTOs

Once we have defined our DTOs using the Swagger decorators, we can generate OpenAPI documentation from them. The OpenAPI specification is a widely adopted standard for documenting RESTful APIs.

To generate the OpenAPI documentation, we can use tools like the “@nestjs/swagger” package, which automatically generates the documentation based on our annotated DTOs.

import { NestFactory } from '@nestjs/core';
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';

async function bootstrap() {
const app = await NestFactory.create(AppModule);

const options = new DocumentBuilder()
.setTitle('Order Management System')
.setDescription('API documentation for the order management system')
.setVersion('1.0')
.build();

const document = SwaggerModule.createDocument(app, options);
SwaggerModule.setup('api', app, document);

await app.listen(3000);
}
bootstrap();

In this example, we create a Swagger document builder, providing the necessary information like the title, description, and version of our API. We then use SwaggerModule.createDocument() to generate the OpenAPI documentation from our annotated DTOs.

Step 3: Parsing and Reusing Component Schemas

With the OpenAPI documentation generated, we can now parse it to extract the component schemas and reuse them in our Async API documentation. This helps in keeping the documentation consistent and eliminates the need to define the same schemas in multiple places.

There are libraries available, such as “asyncapi-parser” or “asyncapi-react”, that allow us to parse the OpenAPI documentation and extract components like DTO schemas.

import { AsyncAPIDocument, default as parseAsyncAPI } from 'asyncapi-parser';

async function generateAsyncAPIDocument() {
const openAPIDocument = await parseAsyncAPI('path/to/openapi.json');

// Extract component schemas from the OpenAPI document
const components = openAPIDocument.components;

// Generate Async API documentation using the extracted components
const asyncAPIDocument: AsyncAPIDocument = {
asyncapi: '2.0.0',
info: {
title: 'Order Management System',
version: '1.0.0',
},
channels: {
orders: {
publish: {
message: {
headers: {},
payload: {
// Reuse the OrderDto schema
$ref: '#/components/schemas/OrderDto',
},
},
},
},
},
components,
};

return asyncAPIDocument;
}

In this code snippet, we use the “asyncapi-parser” library to parse the OpenAPI documentation from a JSON file. We then extract the “components” section from the OpenAPI document, which contains the schema definitions.

Finally, we generate the Async API documentation by reusing the extracted component schemas. In this example, we reference the “OrderDto” schema using the $ref keyword. This ensures that the Async API specification uses the schema defined in the OpenAPI document.

Validating and Previewing Async API Documentation

To ensure the accuracy and validity of our Async API documentation, it is essential to validate and preview the specification before generating the final documentation. This helps in identifying potential issues or missing information in the specification.

Introduction to AsyncAPI Studio

AsyncAPI Studio is a powerful online editor and previewer for Async API documentation. It provides a user-friendly interface to create, edit, and validate Async API specifications. With AsyncAPI Studio, you can visualize your event-driven architecture using channels and messages, and it provides instant feedback on the validity of your specification.

Options for Previewing Generated Documentation

In addition to AsyncAPI Studio, there are other options available for previewing the generated documentation. One such option is the “asyncapi-preview” extension for Visual Studio Code. It allows you to preview the Async API specification directly within your code editor, making it convenient for developers to check the documentation while working on their codebase.

Using AsyncAPI Studio or “asyncapi-preview” VSCode Extension

To use AsyncAPI Studio, you can simply copy and paste your Async API specification into the editor and see the visualization of your event-driven architecture in real-time. It provides an interactive preview, allowing you to navigate through channels, messages, and component schemas.

If you prefer using the “asyncapi-preview” VSCode extension, you can install it from the Visual Studio Code Marketplace. Once installed, you can open your Async API specification file in Visual Studio Code and preview it by opening the command palette and searching for “AsyncAPI: Preview Current Document”. This will open a browser window with the rendered Async API documentation.

Both options provide a convenient way to preview and validate your Async API specification, ensuring that it accurately represents your event-driven system.

Importance of Validation and Ensuring Accuracy of the Specification

Validation and accuracy are crucial when working with Async API specifications. By validating the specification, we can ensure that it adheres to the Async API specification format and all the required fields are present. This reduces the chances of encountering issues when using the specification to implement event-driven communication.

Additionally, validating the specification helps in identifying any missing or conflicting information, preventing potential communication errors between the components of the system.

Previewing the specification is equally important as it provides a visual representation of the event-driven system and helps in verifying that the specification accurately represents the desired architecture. It allows developers to spot any inconsistencies or gaps in the documentation, enabling them to make necessary adjustments before generating the final documentation.

Generating the Actual Async API Documentation

After ensuring the accuracy and validity of the Async API specification, we can proceed with generating the actual documentation. To accomplish this, we will utilize the “@asyncapi/cli” package and choose a template package that suits our documentation needs.

Installing the “@asyncapi/cli” Package

To generate Async API documentation, we need to install the “@asyncapi/cli” package globally on our machine. This package provides a command-line interface (CLI) for working with Async API specifications.

npm install -g @asyncapi/cli

Choosing a Template Package

Once the “@asyncapi/cli” package is installed, we need to choose a template package to generate the actual documentation. The “@asyncapi/html-template” package generates HTML documentation, while the “@asyncapi/markdown-template” package generates Markdown documentation.

If you prefer HTML documentation, you can install the package using the following command:

npm install -g @asyncapi/html-template

If you prefer Markdown documentation, you can install the package using the following command:

npm install -g @asyncapi/markdown-template

Updating the package.json File with Necessary Scripts

To simplify the generation process, we can add some scripts to the package.json file of our project. These scripts will allow us to generate the Async API documentation using a simple command.

"scripts": {
"docs:html": "asyncapi generate path/to/asyncapi.yaml @asyncapi/html-template > documentation.html",
"docs:markdown": "asyncapi generate path/to/asyncapi.yaml @asyncapi/markdown-template > documentation.md"
}

In this example, we define two scripts. The “docs:html” script generates HTML documentation from the Async API specification located at “path/to/asyncapi.yaml”. The resulting documentation is saved to a file named “documentation.html”.

Similarly, the “docs:markdown” script generates Markdown documentation and saves it to a file named “documentation.md”.

To generate the documentation, we can use the following commands:

npm run docs:html

or

npm run docs:markdown

The generated documentation can then be shared with the team or published to a documentation platform for easy access.

Conclusion

Async API documentation plays a vital role in event-driven systems, enabling seamless communication between components. In this article, we explored the basics of Async API documentation, including its importance in event-driven systems and its structure.

We also discussed how to autogenerate Async API documentation by leveraging tools like the “@nestjs/swagger” package and parsing the OpenAPI documentation. Additionally, we explored the importance of validating and previewing the specification using tools like AsyncAPI Studio or the “asyncapi-preview” VSCode extension.

To generate the actual Async API documentation, we installed the “@asyncapi/cli” package and chose a template package, either “@asyncapi/html-template” for HTML documentation or “@asyncapi/markdown-template” for Markdown documentation. By adding the necessary scripts to the package.json file, we simplified the generation process.

In conclusion, Async API documentation is an essential aspect of event-driven systems, and by leveraging automated tools and processes, we can streamline the workflow and ensure accurate and effective communication between components.

Looking for a Postman alternative?

Try APIDog, the Most Customizable Postman Alternative, where you can connect to thousands of APIs right now!

--

--