asyncapi-rust
Generate AsyncAPI 3.0 specifications from Rust code using procedural macros.
Similar to how utoipa generates OpenAPI specs for REST APIs,
asyncapi-rust generates AsyncAPI specs for WebSocket and other async protocols.
Quick Start
Add to your Cargo.toml:
[]
= "0.1"
= { = "1.0", = ["derive"] }
= { = "0.8", = ["derive"] }
Define your WebSocket messages:
use ;
use ;
/// WebSocket messages for a chat application
/// Complete API specification
;
Core Concepts
Message Types with #[derive(ToAsyncApiMessage)]
Generate message metadata and JSON schemas from your Rust types:
- Uses
serdefor JSON serialization - Uses
schemarsfor JSON Schema generation - Respects
#[serde(...)]attributes (rename,tag, etc.) - Supports
#[asyncapi(...)]helper attributes for documentation
Complete Specs with #[derive(AsyncApi)]
Generate complete AsyncAPI specifications declaratively:
#[asyncapi(...)]- Basic info (title, version, description)#[asyncapi_server(...)]- Server definitions#[asyncapi_channel(...)]- Channel definitions#[asyncapi_operation(...)]- Operation definitions
Framework Integration
Works with any WebSocket framework:
- actix-web + actix-ws - See
examples/actix_websocket.rs - axum - See
examples/axum_websocket.rs - tungstenite - See
examples/framework_integration_guide.rs
The same message types are used in both runtime handlers and documentation.
Features
- Code-first: Generate specs from Rust types, not YAML
- Compile-time: Zero runtime cost, all generation at build time
- Type-safe: Compile errors if documentation drifts from code
- Framework agnostic: Works with actix-ws, axum, or any serde-compatible types
- Binary protocols: Support for mixed text/binary WebSocket messages
Examples
See the examples/ directory for complete working examples:
simple.rs- Basic message types with schema generationchat_api.rs- Complete AsyncAPI 3.0 specificationasyncapi_derive.rs- Using#[derive(AsyncApi)]generate_spec_file.rs- Generating specification filesfull_asyncapi_derive.rs- Complete spec with servers, channels, operationsactix_websocket.rs- Real-world actix-web integrationaxum_websocket.rs- Real-world axum integrationframework_integration_guide.rs- Comprehensive framework guide
Run any example:
Generating Documentation Files
Create a binary to generate AsyncAPI spec files:
// bin/generate-asyncapi.rs
use MyApi;
Then run: cargo run --bin generate-asyncapi