Raw Methods (Generated)
Powered By GitBook
Quick Start
this should set you up in no time!

Installation

Installing Gramjs is a straight forward process:
1
$ npm i telegram
Copied!
If you want to use gramjs in a browser please check the advanced installation page.
Once you've installed gramjs you'll need an API ID and an API hash (read more in Authorization). Get them from https://my.telegram.org/apps: Afterward, you can use the following code to send a message to yourself.
JavaScript
TypeScript
hello.js
1
const { TelegramClient } = require('telegram')
2
const { StringSession } = require('telegram/sessions')
3
const input = require('input') // npm i input
4
​
5
const apiId = 123456
6
const apiHash = '123456abcdfg'
7
const stringSession = new StringSession(''); // fill this later with the value from session.save()
8
(async () => {
9
console.log('Loading interactive example...')
10
const client = new TelegramClient(stringSession, apiId, apiHash, { connectionRetries: 5 })
11
await client.start({
12
phoneNumber: async () => await input.text('number ?'),
13
password: async () => await input.text('password?'),
14
phoneCode: async () => await input.text('Code ?'),
15
onError: (err) => console.log(err),
16
});
17
console.log('You should now be connected.')
18
console.log(client.session.save()) // Save this string to avoid logging in again
19
await client.sendMessage('me', { message: 'Hello!' });
20
})()
21
​
22
​
Copied!
hello.ts
1
import { TelegramClient } from 'telegram'
2
import { StringSession } from 'telegram/sessions'
3
import input from 'input'; // npm i input
4
​
5
const apiId = 123456
6
const apiHash = '123456abcdfg'
7
const stringSession = new StringSession(''); // fill this later with the value from session.save()
8
(async () => {
9
console.log('Loading interactive example...');
10
const client = new TelegramClient(stringSession, apiId, apiHash, { connectionRetries: 5 });
11
await client.start({
12
phoneNumber: async () => await input.text('number ?'),
13
password: async () => await input.text('password?'),
14
phoneCode: async () => await input.text('Code ?'),
15
onError: (err) => console.log(err),
16
});
17
console.log('You should now be connected.');
18
console.log(client.session.save()) // Save this string to avoid logging in again
19
await client.sendMessage('me', { message: 'Hello!' });
20
})()
21
​
Copied!
Using session strings is the most reliable way to save your session for now.

Full API

gramjs is still in its early stages but it can access all API methods from telegram using the following
JavaScript
TypeScript
fullapi.js
1
const {Api} = require("telegram/tl");
2
​
3
const result = await client.invoke(new Api.channels.CheckUsername({
4
username: "testing"
5
}));
6
console.log("Result is ",result);
7
​
Copied!
fullapi.ts
1
import {Api} from "telegram/tl";
2
​
3
const result = await client.invoke(new Api.channels.CheckUsername({
4
username: "testing"
5
}));
6
console.log("Result is ",result);
7
​
Copied!
All methods and classes are accessible under Api. for a full list of them check out https://gram.js.org/​
Last modified 6mo ago
Copy link