Skip to content

AsyncAPI Complete Guide: Event-Driven API Specification

In this tutorial, you'll learn about AsyncAPI: an open-source specification for describing event-driven APIs that brings the same documentation and code generation benefits to message-based systems that OpenAPI brings to REST.

AsyncAPI is an open specification for documenting event-driven APIs using a JSON/YAML format parallel to OpenAPI, enabling automated documentation and code generation for Kafka, MQTT, and WebSocket systems.

What You'll Learn

  • AsyncAPI 2.x and 3.x specification structure
  • Channels, messages, payloads, and server definitions
  • Bindings for Kafka, MQTT, and AMQP
  • Code generation with AsyncAPI Generator
  • Designing event-driven APIs with confidence

Why AsyncAPI Matters

REST APIs have OpenAPI — a standard format for documentation, code generation, and validation. Event-driven APIs had no equivalent, so teams relied on wiki pages and tribal knowledge. DodaTech's Durga Antivirus Pro uses AsyncAPI to document its threat event stream, enabling SIEM partners to integrate without reverse-engineering binary message formats.

flowchart TB
    subgraph "AsyncAPI Document"
        S["Spec Version\n(asyncapi: 2.6.0)"] --> I["Info: title, version"]
        S --> SV["Servers: URL, protocol"]
        S --> C["Channels: topics/queues"]
        C --> OP["Operations:\npublish / subscribe"]
        OP --> M["Messages:\npayload schema"]
        M --> REF["$ref components\n(reusable schemas)"]
        SV --> SEC["Security Schemes"]
    end
    style S fill:#dbeafe,stroke:#2563eb
    style C fill:#fef3c7,stroke:#d97706
    style M fill:#dcfce7,stroke:#16a34a
â„šī¸ Info

Prerequisites: Familiarity with REST APIs and OpenAPI. Understanding of message brokers (Kafka, RabbitMQ) is helpful but not required.

AsyncAPI vs OpenAPI

Aspect OpenAPI AsyncAPI
API type REST (request-response) Event-driven (pub/sub)
Transport HTTP/HTTPS Kafka, MQTT, WebSocket, AMQP
Key element Paths + operations Channels + messages
Direction Client sends, server responds Server publishes/subscribes
Code gen OpenAPI Generator AsyncAPI Generator
Version 3.x 2.x, 3.x

Common Mistakes

1. Confusing Publish and Subscribe Directions

In AsyncAPI, publish means the server sends messages (clients receive). subscribe means the server receives messages (clients send). Always think from the server's perspective.

2. Not Defining Reusable Components

Writing inline schemas for every message leads to duplication. Use $ref to reference reusable components.messages and components.schemas.

3. Ignoring Server Definitions

Without server definitions, your spec is documentation-only. Add protocol, URL, and security to enable tooling to generate working clients.

4. Not Versioning Event Schemas

Message schemas evolve. Use semantic versioning for your AsyncAPI spec and maintain backward-compatible changes.

5. Overlooking Message Headers

Correlation IDs, timestamps, and message types belong in headers, not payloads. Define headers separately in the message object.

Practice Questions

  1. What is the difference between publish and subscribe in AsyncAPI?
  2. How does AsyncAPI compare to OpenAPI in terms of purpose?
  3. What are bindings and why are they important?
  4. How do you reference reusable schemas in AsyncAPI?
  5. What new features does AsyncAPI 3.0 introduce?

Answers:

  1. From the server's perspective: publish means the server publishes messages for clients to receive. subscribe means the server subscribes to messages from clients.
  2. OpenAPI describes REST APIs (request-response). AsyncAPI describes event-driven APIs (publish-subscribe). Both use similar structures (YAML/JSON, components, $ref).
  3. Bindings provide protocol-specific configuration (Kafka topic settings, MQTT QoS levels, AMQP queue attributes) that generic message schemas cannot Express.
  4. Use $ref: '#/components/messages/MessageName' to reference reusable message schemas. Components can include messages, schemas, security schemes, and parameters.
  5. AsyncAPI 3.0 introduces operations as first-class citizens, improves reusability with application-level components, and adds better multi-protocol support in a single spec.

Challenge: Write an AsyncAPI spec for DodaTech's threat detection system. Include channels for threat detection events, scan completion events, and system health status. Use a Kafka server with SASL authentication. Define reusable message schemas for threat payloads and scan results.

FAQ

Is AsyncAPI compatible with OpenAPI?

Yes — both use JSON Schema for message/request bodies. AsyncAPI 2.x reuses OpenAPI 3.x concepts like components, security schemes, and server variables. APIs can have both OpenAPI and AsyncAPI specs.

What message brokers does AsyncAPI support?

AsyncAPI is protocol-agnostic. It supports Kafka, RabbitMQ, MQTT, WebSocket, AMQP, HTTP Webhooks, NATS, and custom protocols via the protocol field in server definitions.

Can I validate messages against AsyncAPI schemas?

Yes — use the AsyncAPI Schema Validator or tools like Spectral with AsyncAPI rulesets to validate messages at runtime against your specification.

What is the difference between AsyncAPI 2.x and 3.x?

AsyncAPI 3.0 introduces operations as first-class citizens, improves reusability with application-level components, and adds better support for multiple protocols in a single spec.

Does AsyncAPI support code generation?

Yes — the AsyncAPI Generator CLI can produce Node.js, Python, Java, Go, and Rust clients and servers from your spec, along with HTML documentation and React components.

Try It Yourself

# Install AsyncAPI Generator
npm install -g @asyncapi/generator

# Generate HTML docs from a spec
asyncapi generate fromTemplate https://raw.githubusercontent.com/asyncapi/spec/master/examples/simple.yml @asyncapi/html-template -o docs/

# Open docs/index.html in your browser

What's Next

Topic Description
Introduction to AsyncAPI First steps with the spec
Webhooks Guide Compare Webhooks with AsyncAPI
OpenAPI Specification REST API specification standard
Apache Kafka Guide Event streaming platform
➡ AsyncAPI Introduction
âŦ… Webhooks Guide

Published Topics

Asyncapi Mqtt

✓ Live

Asyncapi Project

✓ Live

Asyncapi Websocket

✓ Live

Introduction to AsyncAPI

Learn what AsyncAPI is, why it matters for event-driven APIs, and how it helps document, design, and build asynchronous systems with a standard specification.

✓ Live

AsyncAPI vs OpenAPI — Complete Guide

Compare AsyncAPI and OpenAPI specifications to understand their differences, use cases, and how they complement each other in API design.

✓ Live

AsyncAPI Specification Structure — Complete Guide

Explore the structure of an AsyncAPI document, including its top-level fields, metadata, servers, channels, and message definitions.

✓ Live

AsyncAPI Channels — Complete Guide

Master AsyncAPI channel definitions including channel parameters, publish/subscribe operations, and binding protocol-specific configuration.

✓ Live

AsyncAPI Messages — Complete Guide

Learn how to define messages in AsyncAPI including headers, payloads, correlation IDs, traits, and protocol-specific message bindings.

✓ Live

AsyncAPI Message Payloads — Complete Guide

Design message payloads in AsyncAPI using JSON Schema, Avro, and other schema formats for structured event data.

✓ Live

AsyncAPI Servers — Complete Guide

Learn how to define servers in AsyncAPI including URLs, protocols, security schemes, and environment-specific configurations.

✓ Live

AsyncAPI Operations — Complete Guide

Learn how to define operations in AsyncAPI including operation IDs, message routing, bindings, and traits for publish and subscribe patterns.

✓ Live

AsyncAPI Protocol Bindings — Complete Guide

Explore protocol-specific bindings in AsyncAPI for Kafka, MQTT, AMQP, WebSocket, and HTTP to configure message broker integration.

✓ Live

AsyncAPI Security Schemes — Complete Guide

Learn how to define security schemes in AsyncAPI including API keys, OAuth2, OpenID Connect, SASL, and custom authentication methods.

✓ Live

AsyncAPI Tags and External Documentation

Learn how to use tags and external documentation in AsyncAPI to organize, categorize, and enhance your event-driven API documentation.

✓ Live

AsyncAPI Code Generation — Complete Guide

Learn how to generate code from AsyncAPI specifications including client libraries, server stubs, documentation, and validation code.

✓ Live

AsyncAPI Studio — Complete Guide

Learn how to use AsyncAPI Studio for visual editing, validation, and preview of event-driven API specifications.

✓ Live

AsyncAPI Tools and Ecosystem — Complete Guide

Explore the AsyncAPI tooling ecosystem including CLI, generators, validators, parsers, and integrations for event-driven API development.

✓ Live

Event-Driven APIs with AsyncAPI — Complete Guide

Learn how to design and document event-driven API patterns using AsyncAPI, including event sourcing, CQRS, saga patterns, and pub-sub.

✓ Live

AsyncAPI with Kafka — Complete Guide

Learn how to document Apache Kafka topics, consumer groups, and schema registry integration using AsyncAPI specifications and bindings.

✓ Live

AsyncAPI Schemas — Complete Guide

Learn how to design reusable schemas in AsyncAPI using JSON Schema, Avro, and Protobuf for message payload validation and code generation.

✓ Live

All 20 topics in AsyncAPI Complete Guide: Event-Driven API Specification are published.