# Chat invite links

## Export chat invite link

To export chat invite link, use `.exportInviteLink` **answer/api method**

{% hint style="info" %}
If you need, you can pass chat id you want to export chat invite link as argument *(required in Api method)*

If you want to know what arguments an API method takes, see the IDE hint
{% endhint %}

Use this method to generate a new primary invite link for a chat; any previously generated primary link is revoked. The bot must be an administrator in the chat for this to work and must have the appropriate administrator rights

{% hint style="success" %}
Returns the new invite link as *string* on success
{% endhint %}

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

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

@Controller()
export class AppController {
  constructor(private readonly appService: AppService) {}

  @OnCommand('admin_export_invite_link')
  async exportInviteLink(@GetAnswer() answer: Answer): Promise<string> {
    return await answer.exportInviteLink();
  }
}
```

{% endcode %}

## Create invite link

You can create chat invite link using `.createInviteLink` **answer/api method**

#### .createInviteLink answer 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><a href="https://core.telegram.org/bots/api#createchatinvitelink">Options</a></td><td>Optional</td></tr><tr><td>2</td><td>Chat id you want to export invite link to</td><td>Optional. Current chat id by default</td></tr></tbody></table>

{% hint style="info" %}
If you want to know what arguments an API method takes, see the IDE hint
{% endhint %}

{% hint style="success" %}
Returns [chat invite link](https://core.telegram.org/bots/api#chatinvitelink) on success
{% endhint %}

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

```typescript
import { Controller, OnCommand, IChatInviteLink, GetAnswer, Answer } from 'nestgram';
import { AppService } from './app.service';

@Controller()
export class AppController {
  constructor(private readonly appService: AppService) {}

  @OnCommand('admin_create_invite_link')
  async createInviteLink(@GetAnswer() answer: Answer) {
    const inviteLinkInfo: IChatInviteLink = await answer.createInviteLink({ member_limit: 10 });
    return inviteLinkInfo.invite_link;
  }
}
```

{% endcode %}

## Edit invite link

To edit invite link, use **Edit class-method** and **InviteLink class-mark**. Or `.editInviteLink` **api method**

#### Edit class-method take arguments:

<table><thead><tr><th width="210.55476344662387" data-type="number">Argument</th><th width="322.76845025779505">Description</th><th>Required</th></tr></thead><tbody><tr><td>1</td><td>Invite link you want to edit</td><td><strong>Required</strong></td></tr><tr><td>2</td><td>Options how you want to edit link</td><td><strong>Required</strong></td></tr></tbody></table>

{% hint style="info" %}
If you want to know what arguments an API method takes, see the IDE hint
{% endhint %}

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

```typescript
import { Answer, Controller, Edit, GetAnswer, IChatInviteLink, InviteLink, OnCommand } from 'nestgram';
import { AppService } from './app.service';

@Controller()
export class AppController {
  inviteLink: string
  constructor(private readonly appService: AppService) {}

  @OnCommand('start')
  async createInviteLink(@GetAnswer() answer: Answer) {
    const linkInfo: IChatInviteLink = await answer.createInviteLink();
    return this.inviteLink = linkInfo.invite_link;
  }

  @OnCommand('edit')
  editInviteLink() {
    return new Edit(new InviteLink(this.inviteLink, { name: 'New name (edited)' })).next('Edited');
  }
}
```

{% endcode %}

{% hint style="warning" %}
We recommend using [services](https://degreetpro.gitbook.io/nestgram/nestgram-features/services) to save data
{% endhint %}

## Revoke invite link

You can revoke invite link using `.revokeInviteLink` **answer/api method**

#### .revokeInviteLink answer method take arguments:

<table><thead><tr><th width="150" data-type="number">Argument</th><th width="387.31109107303877">Description</th><th>Required</th></tr></thead><tbody><tr><td>1</td><td>Invite link you want to revoke</td><td><strong>Required</strong></td></tr><tr><td>2</td><td>Chat id in which you want to revoke link</td><td>Optional. Current chat id by default</td></tr></tbody></table>

{% hint style="info" %}
If you want to know what arguments an API method takes, see the IDE hint
{% endhint %}

{% hint style="success" %}
Returns [chat invite link](https://core.telegram.org/bots/api#chatinvitelink) on success
{% endhint %}

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

```typescript
import { Controller, OnCommand, IChatInviteLink, GetAnswer, Answer } from 'nestgram';
import { AppService } from './app.service';

@Controller()
export class AppController {
  inviteLink: string = '';
  constructor(private readonly appService: AppService) {}

  @OnCommand('admin_create_invite_link')
  async createInviteLink(@GetAnswer() answer: Answer) {
    const inviteLinkInfo: IChatInviteLink = await answer.createInviteLink({ member_limit: 10 });
    this.inviteLink = inviteLinkInfo.invite_link;
    return inviteLinkInfo.invite_link;
  }

  @OnCommand('admin_revoke_invite_link')
  async revokeInviteLink(@GetAnswer() answer: Answer) {
    await answer.revokeInviteLink(this.inviteLink);
    return `${this.inviteLink} link revoked`;
  }
}
```

{% endcode %}

{% hint style="warning" %}
We recommend using [services](https://degreetpro.gitbook.io/nestgram/nestgram-features/services) to save data
{% endhint %}
