# Build an extension

Extension let you extend Azot with your own features to create a custom downloading experience.

In this tutorial, you'll compile an extension example from source code and load it into Azot.

## What you'll learn

After you've completed this tutorial, you'll be able to:

- Configure an environment for developing Azot extensions.
- Compile an extension from source code.
- Reload an extension after making changes to it.

## Prerequisites

To complete this tutorial, you'll need:

- [Git](https://git-scm.com/) installed on your local machine.
- A local development environment for [Node.js](https://nodejs.org/en/download).
- A code editor, such as [Visual Studio Code](https://code.visualstudio.com/).

> **Info**
>
> It is assumed that you are already familiar with [JavaScript](https://developer.mozilla.org/en-US/docs/Web/JavaScript) and [TypeScript](https://www.typescriptlang.org). Don't worry, you don't need to be an expert. If you need some help with the basics, check out TypeScript's [Handbook](https://www.typescriptlang.org/docs/handbook/intro.html) and Mozilla's [JavaScript Guide](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide).

## Step 1: Create a new extension project

Run the interactive scaffolder to create a starter project with TypeScript, `azot/vitest`, `AGENTS.md`, and a release workflow. Pass `--match` for Bitmovin so the test URL in Step 3 matches right away:

```sh
npx azot@beta init hello-world --match "https://*.bitmovin.com/*"
```

For scripts or coding agents, pass flags and `--yes` to skip prompts:

```sh
npx azot@beta init hello-world --kind provider --match "https://*.bitmovin.com/*" --yes --json
```

> **Info: Prefer a template repository?**
>
> You can still clone or fork [`azot-labs/azot-extension-example`](https://github.com/azot-labs/azot-extension-example) if you prefer starting from a GitHub template repository.

> **Info: Docs for coding agents**
>
> The `azot` npm package bundles the extension docs inside `node_modules/azot/docs/` and `node_modules/azot/SKILL.md`. Online references are also available at [`/docs/llms.txt`](https://azot.mov/docs/llms.txt) and [`/docs/llms-full.txt`](https://azot.mov/docs/llms-full.txt).

## Step 2: Build the extension

In this step, you'll compile the extension so that Azot can load it.

1. Navigate to the extension directory.

```sh
cd hello-world
```

2. Install dependencies.

```sh
npm install
```

> **Warning: Install `azot@beta`**
>
> The current Azot v5 is published under the `beta` tag. Plain `npm i azot` installs an outdated release from the `latest` tag. When adding Azot to your own project, install the beta:
>
> ```sh
> npm i -D azot@beta
> ```

3. Compile the source code. The following command keeps running in the terminal and rebuilds the extension when you modify the source code.

```sh
npm run dev
```

Notice that the extension directory now has a `dist/main.cjs` file that contains a compiled version of the extension. Azot runs extensions as CommonJS, so `main` in `package.json` must point to a `.cjs` or `.js` file.

## Step 3: Test and load the extension

While developing, you can inspect URLs without downloading media and load the folder in developer mode.

- **Fast local dry-run (no download):** test your provider's `list()` and `resolve()` directly from the extension directory:

  ```sh
  npx azot try https://bitmovin.com/demos/stream-test --json
  ```

- **Validate metadata, bundle, permissions, and `manifest.json`:**

  ```sh
  npx azot check
  ```

- **CLI:** pass `--extension` to a download, or add `--dry-run --json` to run with your Azot session and cookies without downloading bytes:

  ```sh
  azot https://bitmovin.com/demos/stream-test --extension . --dry-run --json
  azot https://bitmovin.com/demos/stream-test --extension .
  ```

- **Desktop:** turn on **Developer Mode** in settings, then choose **Load from folder** in the extensions page. Azot remembers the folder while Developer Mode is on and reloads it automatically whenever `npm run dev` or `npm run build` updates `dist/` or `manifest.json`. While loaded, it replaces an installed extension with the same name.

You're now ready to use the extension in Azot. Next, we'll make some changes to the extension.

## Step 4: Update the extension info

In this step, you'll rename the extension and describe which URLs it supports.

1. Open `package.json` in your code editor.
2. Change `name` to a unique identifier, such as "hello-world". The package name is the extension's identity.
3. Also fill in `description`, `author` and `funding` fields with your information.
4. Rename the extension folder to match the extension's name.
5. Open `main.ts` and set `title`, `icon` and the provider's `matches` in code. To show the title and description in the user's language, see [Localization](https://azot.mov/docs/extensions/api.md#localization):

```ts
import { defineExtension, defineProvider } from "azot";

export default defineExtension({
  title: "Hello world",
  icon: "https://example.com/icon.png",
  bitmovin: defineProvider({
    matches: ["https://*.bitmovin.com/*"],
    async list({ url }) {
      return [];
    },
  }),
});
```

6. Run `npx azot manifest`. It regenerates `manifest.json` from the code without a release build. `azot build` and `azot dev` regenerate it too, `azot dev` after every rebuild. Never edit `manifest.json` by hand.
7. Load the folder again to pick up the changes: run the next download with `--extension /path/to/hello-world`, or choose **Load from folder** again in the desktop app.

## Step 5: Update the source code

To let the user interact with your extension, add a prompt for desired stream format when user trying to download video.

1. Open `main.ts` in your code editor.

2. Import helpers from the `azot` package and update `list()`.

```ts
import { defineExtension, defineProvider, prompt } from "azot";

export default defineExtension({
  title: "Hello world",
  bitmovin: defineProvider({
    matches: ["https://*.bitmovin.com/*"],
    async list({ url }) {
      const { format } = await prompt("Choose stream format", {
        title: "Stream format",
        form: {
          format: {
            type: "text",
            title: "Format",
            defaultValue: "dash",
          },
        },
      });

      // Injected fetch is cancelled with the operation, no signal needed.
      const response = await fetch(`${url}?format=${format}`);
      const body = await response.text();
      const mpd = body.split("'").find((line) => line.endsWith(".mpd"));

      if (!mpd) return [];

      return [
        {
          kind: "video",
          title: "Bitmovin Stream Test",
          source: { url: mpd },
        },
      ];
    },
  }),
});
```

In the Azot CLI, run the command with URL that extension can handle.

```sh
azot https://bitmovin.com/demos/stream-test --extension /path/to/hello-world
```

You can now see download progress after metadata fetching.

For providers, `resolve()`, sign-in, settings and the other module kinds, see [Extension API](https://azot.mov/docs/extensions/api.md).

## Conclusion

In this tutorial, you've built your first Azot extension using the TypeScript API. You've modified the extension and reloaded it to reflect the changes inside Azot.
