# Api class

## Why do you need an Api class?

If you want to send a message to another user, call **api method** for a **different bot**, or get data from the [api reference](https://core.telegram.org/bots/api), you can use **Api class methods**

## Get Api class in controller

To get **Api class** in controller, use `@GetApi` property decorator, and **Api class** as type

{% code title="app.controller.ts" %}

```typescript
import { OnCommand, Controller, GetApi, Api } from 'nestgram';
import { AppService } from './app.service';

@Controller()
export class AppController {
  @GetApi() api: Api;

  constructor(private readonly appService?: AppService) {}

  @OnCommand('start')
  async start(): Promise<string> {
    await this.api.send('userId', 'Hello!'); // send message
    return 'Hello, world!';
  }
}
```

{% endcode %}

#### .send Api method take arguments:

<table><thead><tr><th data-type="number">Argument</th><th>Description</th><th>Required</th></tr></thead><tbody><tr><td>1</td><td>User ID or channel ID (<em>string or number</em>)</td><td><strong>Required</strong></td></tr><tr><td>2</td><td>Message you want to send (you can use <strong>MessageSend class</strong> too)</td><td><strong>Required</strong></td></tr><tr><td>3</td><td>Keyboard</td><td>Optional</td></tr><tr><td>4</td><td><a href="https://core.telegram.org/bots/api#sendmessage">Options</a></td><td>Optional</td></tr></tbody></table>

{% hint style="info" %}
If you want to get a bot token, you can get it via `.token` **api property**
{% endhint %}

## Api methods

You can use any method from the **Answer class** in the **Api class**

{% hint style="info" %}
If Api method takes chat id or message id, you need to pass it as 1st and 2nd arguments
{% endhint %}

## Get Api for different token

If you want to call some **api method** for **different bot**, use `bot.to` method

{% hint style="info" %}
bot.to method takes argument: token you want to get api
{% endhint %}

{% code title="main.ts" %}

```typescript
import { ChatAction } from 'nestgram';
import { bot } from './bot';

bot.to('token').send('chatId', new ChatAction('typing'));
```

{% endcode %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://degreetpro.gitbook.io/nestgram/config/api-class.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
