Generate code

Found an error? Have a suggestion?Edit this page on GitHub

Introduction

In this tutorial, you'll learn how to generate code from your AsyncAPI document using the AsyncAPI generator tool.

Background context

The AsyncAPI Generator is a tool that you can use to generate whatever you want based on the AsyncAPI document. You can generate docs and code. It can be used as a library in a Node.js application or through the AsyncAPI CLI.

The generator tool supports a number of templates to generate code for a variety of different languages and protocols as the output. These templates help to specify what exactly must be generated, and in this tutorial, you'll use a Node.js template.

Installation guide

Remember

It is important to have Node.js installed which will enable the installation of the required dependencies using npm.

CLI Installation

Follow the AsyncAPI CLI installation instructions below, based on your computer’s operating system.

MacOS

brew


You can install in MacOS via brew: `brew install asyncapi`.

pkg


Each release of CLI produces a MacOS dedicated pkg file that enables you to install this CLI as MacOS application.
1
2
3
4
# Download latest release. To download specific release, your link should look similar to https://github.com/asyncapi/cli/releases/download/v0.13.0/asyncapi.pkg. All releases are listed in https://github.com/asyncapi/cli/releases
curl -OL https://github.com/asyncapi/cli/releases/latest/download/asyncapi.pkg
# Install AsyncAPI CLI
sudo installer -pkg asyncapi.pkg -target /
Linux

You can install in Linux via dpkg, a debian package manager:

  1. curl -OL https://github.com/asyncapi/cli/releases/latest/download/asyncapi.deb
  2. sudo dpkg -i asyncapi.deb
Other OS

Read further AsyncAPI CLI installation instructions for different operating systems.

Using NPM and Node.js

Alternitavely, you can install the AsyncAPI CLI with Node.js >=v10 and NPM.

Install CLI globally

Install AsyncAPI CLI globally with the following command:

npm install -g @asyncapi/cli
Install specific CLI version

To install a specific version of the AsyncAPI CLI, pass the verion during installation:

npm install -g @asyncapi/cli@{version}

Generate code

To generate code from the AsyncAPI document created in a previous tutorial, follow the steps listed below:

Remember

If you did not follow the previous tutorial and do not have an asyncapi.yaml file ready, generate one running asyncapi new --example=tutorial.yml --no-tty.

  1. Trigger generation of the Node.js code:

    1
    asyncapi generate fromTemplate asyncapi.yaml @asyncapi/nodejs-template -o output -p server=mosquitto

    Let's break down the previous command:

    • asyncapi generate fromTemplate is how you use AsyncAPI Generator via the AsyncAPI CLI.
    • asyncapi.yaml is how you point to your AsyncAPI document and can be a URL.
    • @asyncapi/nodejs-template is how you specify the Node.js template.
    • -o determines where to output the result.
    • -p defines additional parameters you want to pass to the template. Here, the server parameter specifies the server's name as it is defined in AsyncAPI document.
  2. List all files in directory and check that the Node.js application is generated:

    1
    cd output && ls

    Upon execution of the command above, the following is an example of the expected result:

    1
    2
    3
    4
    5
    6
    7
    8
    $ ls
    Dockerfile
    asyncapi.yaml
    docs
    src
    README.md
    config
    package.json
    

Start generated application

  1. Install dependencies of the newly generated application:

    1
    npm install
  2. Start the application:

    1
    npm start

Send message to broker

  1. In another terminal install the MQTT.js library:

    1
    npm install mqtt -g
  2. Send a message to the MQTT broker that's connected to your generated application. Run the following MQTT client command:

    1
    mqtt pub -t 'light/measured' -h 'test.mosquitto.org' -m '{"id": 1, "lumens": 3, "sentAt": "2017-06-07T12:34:32.000Z"}'
  3. Go back to the previous terminal to check if your application logged the streetlight condition you just sent. You should see something like this displayed in the terminal:

    1
    2
    light/measured was received:
    { id: 1, lumens: 3, sentAt: '2017-06-07T12:34:32.000Z' }

Summary

In this tutorial, you learned how to generate your code from the Streetlights API specification document created in a previous tutorial using the AsyncAPI generator tool.

Additionally, you've learned how to run your code by installing the generated code's dependencies and sending several test messages to the Streelights application using the MQTT client.

Next steps

Now that you've completed this tutorial, go ahead and learn how to validate your AsyncAPI messages (events) through the message validation techniques supported by AsyncAPI.

Was this helpful?
Help us improve the docs by adding your contribution.
OR
Create Issue on GitHub