> Markdown version of https://www.asyncapi.com/docs/tutorials/generate-code
> Index: https://www.asyncapi.com/llms.txt

# Generate code

> In this tutorial, you'll learn how to generate code from your AsyncAPI document.

## Introduction

In this tutorial, you'll learn how to generate an application that uses the [Glee](https://github.com/asyncapi/glee) framework. You'll do it with an AsyncAPI document and the [AsyncAPI CLI](/tools/cli).

## Background context
[Glee](https://github.com/asyncapi/glee) is a TypeScript/JavaScript framework that enables you to create APIs and messaging clients based on your AsyncAPI document. Instead of generating code, this framework tightly integrates with your AsyncAPI document and binds functions to specific AsyncAPI operations. You only have to provide the code for these functions and Glee handles the rest.

Glee is often used with the [AsyncAPI CLI](/tools/cli) for a better development experience.

In the previous tutorial, you created an AsyncAPI document that is used in this tutorial.

> **Remember**
>
> If you did not follow the previous tutorial and do not have an `asyncapi.yaml` file for overview, then generate one by running the following command using the AsyncAPI CLI: 
> `asyncapi new --example=tutorial.yml --no-tty`.

## Installation guide

### Prerequisites

- [Node.js](https://nodejs.org/en/download/) <b>16 or higher</b> installed, which enables installation of the required dependencies using <b>npm</b>.

### CLI Installation

> **Remember**
>
> Follow the [AsyncAPI CLI installation](https://github.com/asyncapi/cli#installation) instructions below, based on your computer’s operating system.
> 
> <details>
> <summary>macOS</summary>
> 
> - **Using `brew`**: You can install in macOS via brew: `brew install asyncapi`.
> - **Using `pkg`**: Each release of CLI produces a macOS dedicated pkg file that enables you to
>   install this CLI as macOS application.
> 
>   ```sh
>   # Download the latest release
>   curl -OL https://github.com/asyncapi/cli/releases/latest/download/asyncapi.x64.pkg
>   # Download a specific release
>   # curl -OL https://github.com/asyncapi/cli/releases/download/<version>/asyncapi.x64.pkg
>   # All releases are listed in https://github.com/asyncapi/cli/releases
>   #
>   # Install AsyncAPI CLI
>   sudo installer -pkg asyncapi.x64.pkg -target /
>   ```
> 
> </details>
> 
> <details>
> <summary>Linux</summary>
> 
> You can install in Linux via `dpkg`, a debian package manager:
> 
> ```sh
> curl -OL https://github.com/asyncapi/cli/releases/latest/download/asyncapi.deb
> sudo dpkg -i asyncapi.deb
> ```
> 
> </details>
> 
> <details>
> <summary>Windows</summary>
> 
> For Windows, install the appropriate installer and follow the default installation steps to complete the installation process.
> 
> - For 64-bit, download the [asyncapi.x64.exe](https://github.com/asyncapi/cli/releases/latest/download/asyncapi.x64.exe)
> - For 32-bit, download the [asyncapi.x86.exe](https://github.com/asyncapi/cli/releases/latest/download/asyncapi.x86.exe)
> 
> </details>
> 
> <details>
> <summary>Other OS</summary>
> 
> [Read further AsyncAPI CLI installation instructions for different operating systems](https://github.com/asyncapi/cli#installation).
> 
> </details>

#### Using npm and Node.js

You can install the [AsyncAPI CLI](https://github.com/asyncapi/cli#installation) with Node.js 16 or higher and [npm](https://nodejs.org/en/download/package-manager/).

<details>
<summary>Install CLI globally</summary>

Install AsyncAPI CLI _globally_ with the following command:

```sh
npm install -g @asyncapi/cli
```

</details>

<details>
<summary>Install specific CLI version</summary>

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

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

</details>

## Create a Glee project

1. Trigger the creation of the Glee project:
    
```bash
asyncapi new glee --name=tutorial --template=tutorial
```

    Let's break down the previous command:
    - `asyncapi new glee` is how you use Glee via the AsyncAPI CLI. 
    - `--name=tutorial` is how you tell the AsyncAPI CLI to name your new Glee project. 
    - `--template=tutorial` is how you tell the AsyncAPI CLI to use the template of a Glee project that was created specifically for this tutorial. 

2. List all files in the directory and confirm your Glee project creation:
    
```bash
cd tutorial && ls
```

    Upon execution of the command above, the following is an example of the expected result:
    
```bash
$ ls
    LICENSE
    README.md
    asyncapi.yaml
    functions
    package.json
```

## Start generated application
1. Install dependencies of the newly generated application:
    
```bash
npm install
```

2. Start the application:
    
```bash
npm run dev
```

## Send message to broker
1. In another terminal install the MQTT.js library:
    
```bash
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:
    
```bash
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:
    
```bash
lightMeasured was received from mosquitto:
    { id: 1, lumens: 3, sentAt: '2017-06-07T12:34:32.000Z' }
    Streetlight with id "1" updated its lighting information to 3 lumens at 2017-06-07T12:34:32.000Z.
```

## Summary
In this tutorial, you learned how to create a Glee project from the [Streetlights API specification document created in a previous tutorial](https://asyncapi.com/docs/tutorials/create-asyncapi-document). 

Additionally, you've learned how to run your code by installing the project'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)](https://asyncapi.com/docs/tutorials/message-validation) through the message validation techniques supported by AsyncAPI.
