Skip to content

Examples

Every directory under examples/ is a project that compiles as it stands. It holds the TypeSpec source, the tspconfig.yaml it was compiled with, and the output. The output is committed, so you can read an input and its output side by side without running anything.

The examples

ExampleWhat it shows
Hello worldThe smallest document the emitter can produce. @service and @info, and nothing else.
Payload schemasThe shapes of the schema layer: models, scalars, enums, arrays, records, and the constraints on them.
Schema compositionFour ways to build one schema out of others, plus the @jsonSchemaExtension escape hatch.
Message metadataEverything that sits around a payload: headers, correlation ids, examples, tags and links.
Channels and parametersChannel ids, templated addresses, and the parameters an address declares.
Servers and securityServers, server variables, and the security schemes a server offers.
Request and replyThree operations, and the two shapes an AsyncAPI reply can take.
Kafka user signupOne realistic Kafka contract, with all four Kafka bindings on one document. It also carries an Avro payload and a Protobuf payload.
MQTT bindingsThe three MQTT decorators, and the one kind of name the generic @binding still carries.
StreetlightsThe canonical AsyncAPI example, written in TypeSpec.
Multiple protocolsOne application over Kafka, WebSocket and SQS, from one payload model.
HTTP callbacksThe HTTP bindings on a webhook, including the statusCode of a reply.
Enterprise brokersAMQP, JMS, IBM MQ and Anypoint MQ describing one destination four ways.
Streaming platformsNATS, Pulsar, Google Cloud Pub/Sub and Solace, and the fields each one requires.
Specification extensionsThe x- fields the specification leaves to the author, on all four objects that take one.
Protobuf payloadsTwo Protobuf packages over RabbitMQ, with the .proto files emitted next to the document.
Avro schemasOne order domain written as Avro schema files by tsp-avro. No AsyncAPI document.
Avro payloadsTwo Avro records over Kafka, with the .avsc files emitted next to the document.

Running one

Clone the repository, then compile inside the directory you want:

bash
git clone https://github.com/marvin-hsu/tsp-asyncapi.git
cd tsp-asyncapi
pnpm install && pnpm build
cd examples/01-hello-world
pnpm exec tsp compile .

The emitter writes asyncapi.yaml next to main.tsp, overwriting the copy in the repository. git diff then shows whether your build produces what was committed.