> Documentation index: https://docs.zernio.com/llms.txt. Every page is also Markdown: add .md to its URL.

# Connect

Connect a Telegram account with the Zernio API, every step with code.

Call `GET /v1/connect/telegram` with `profileId` to [generate an access code](/connect/get-telegram-connect-status), valid for 15 minutes.

The user adds the bot to the channel or group and sends it the code. You poll until the connection completes.

## Access code flow

The [connecting accounts guide](/guides/connecting-accounts#telegram) covers where this fits next to the OAuth platforms.

<Tabs items={['Node.js', 'Python', 'curl']}>
<Tab value="Node.js">
```typescript
import Zernio from '@zernio/node';

const zernio = new Zernio();

const { data: access } = await zernio.connect.telegram.getTelegramConnectStatus({
  query: { profileId: '66a1f0c2a4b9d3e8f1a2b3c4' }
});

console.log(access.code, access.botUsername);
```
</Tab>
<Tab value="Python">
```python
from zernio import Zernio

client = Zernio()

access = client.connect.get_telegram_connect_status(
    profile_id="66a1f0c2a4b9d3e8f1a2b3c4"
)

print(access["code"], access["botUsername"])
```
</Tab>
<Tab value="curl">
```bash
curl "https://zernio.com/api/v1/connect/telegram?profileId=66a1f0c2a4b9d3e8f1a2b3c4" \
  -H "Authorization: Bearer $ZERNIO_API_KEY"
```
</Tab>
</Tabs>

Response (`200`):

```json
{
  "code": "ZRN-ABC123",
  "expiresAt": "2027-01-01T12:15:00.000Z",
  "expiresIn": 900,
  "botUsername": "LateScheduleBot",
  "instructions": [
    "1. Add @LateScheduleBot as an administrator in your channel/group (check the exact username: lookalike bots copy its name and picture)",
    "2. Open a private chat with @LateScheduleBot",
    "3. If your channel has a public username, send: ZRN-ABC123 @yourchannel",
    "4. Private channel (no public username)? Forward any message from it to the bot along with the code: ZRN-ABC123",
    "5. Wait for confirmation - the connection will appear in your dashboard"
  ]
}
```

The user then does 3 things in Telegram:

1. Adds `@LateScheduleBot` to the channel as an administrator with the Post Messages permission, or to the group as an administrator. Lookalike bots copy its display name and picture, so the username must be exactly `@LateScheduleBot`.
2. Opens a private chat with [@LateScheduleBot](https://t.me/LateScheduleBot).
3. Sends the code with the channel username: `ZRN-ABC123 @yourchannel`. For a private channel without a username, they forward any message from the channel to the bot together with the code.

Poll `PATCH /v1/connect/telegram` with the code until `status` is `connected`:

<Tabs items={['Node.js', 'Python', 'curl']}>
<Tab value="Node.js">
```typescript
const { data: status } = await zernio.connect.telegram.completeTelegramConnect({
  query: { code: access.code }
});

console.log(status.status);
```
</Tab>
<Tab value="Python">
```python
status = client.connect.complete_telegram_connect(code=access["code"])

print(status["status"])
```
</Tab>
<Tab value="curl">
```bash
curl -X PATCH "https://zernio.com/api/v1/connect/telegram?code=ZRN-ABC123" \
  -H "Authorization: Bearer $ZERNIO_API_KEY"
```
</Tab>
</Tabs>

Response (`200`) while the user has not sent the code yet:

```json
{
  "status": "pending",
  "expiresAt": "2027-01-01T12:15:00.000Z",
  "expiresIn": 542
}
```

Response (`200`) once the bot has received it:

```json
{
  "status": "connected",
  "chatId": "-1001234567890",
  "chatTitle": "My Channel",
  "chatType": "channel",
  "account": {
    "_id": "66b2e19d8c3f5a7e9d0b1c2d",
    "platform": "telegram",
    "username": "mychannel",
    "displayName": "My Channel"
  }
}
```

`account._id` is the `accountId` for every call below.

A code that is not used within 15 minutes returns `status: "expired"`. Generate a new one.

## Direct connection

If the bot is already an administrator and you know the chat id, skip the code and [connect directly](/connect/initiate-telegram-connect) with `POST /v1/connect/telegram`:

<Tabs items={['Node.js', 'Python', 'curl']}>
<Tab value="Node.js">
```typescript
const { data: connected } = await zernio.connect.telegram.initiateTelegramConnect({
  body: {
    profileId: '66a1f0c2a4b9d3e8f1a2b3c4',
    chatId: '-1001234567890'
  }
});

console.log(connected.account._id);
```
</Tab>
<Tab value="Python">
```python
connected = client.connect.initiate_telegram_connect(
    profile_id="66a1f0c2a4b9d3e8f1a2b3c4",
    chat_id="-1001234567890"
)

print(connected["account"]["_id"])
```
</Tab>
<Tab value="curl">
```bash
curl -X POST https://zernio.com/api/v1/connect/telegram \
  -H "Authorization: Bearer $ZERNIO_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "profileId": "66a1f0c2a4b9d3e8f1a2b3c4",
    "chatId": "-1001234567890"
  }'
```
</Tab>
</Tabs>

Response (`200`):

```json
{
  "message": "Telegram channel connected successfully",
  "account": {
    "_id": "66b2e19d8c3f5a7e9d0b1c2d",
    "platform": "telegram",
    "username": "mychannel",
    "displayName": "My Channel",
    "isActive": true,
    "chatType": "channel"
  }
}
```

`chatId` is the username with an `@` prefix for a public channel (`@mychannel`). For a private channel or a group, it is the numeric id.

To find a numeric id:

- **Channel**: forward a message from the channel to [@userinfobot](https://t.me/userinfobot), which replies with the id. Private channel ids start with `-100`.
- **Group**: add that bot to the group for a moment, and remove it once it has shown the group's negative id.