This section provides all the information you need to start using Pyrogram. There are a few steps you have to follow before you can use the library to make API calls.
API Keys
The very first step requires you to obtain a valid Telegram API key. If you already have one you can skip this, otherwise:
- Visit https://my.telegram.org/apps and log in with your Telegram Account.
- Fill out the form to register a new Telegram application.
- Done. The Telegram API key consists of two parts: the App api_id and the App api_hash.
Important: This key should be kept secret.
Setup
Create a new config.ini
file at the root of your working directory, copy-paste the following and replace the api_id and api_hash values with your own:
[pyrogram]
api_id = 12345
api_hash = 0123456789abcdef0123456789abcdef
Authorization
Telegram requires that users be authorized in order to use the API. Pyrogram automatically manages this access, all you need to do is create an instance of the pyrogram.Client
class by passing to it a session_name
of your choice and call the start
method:
from pyrogram import Client
client = Client(session_name="example")
client.start()
This starts an interactive shell asking you to input your phone number (including your Country Code) and the phone code you will receive:
Enter phone number: +39**********
Is "+39**********" correct? (y/n): y
Enter phone code: 32768
After successfully authorizing yourself, a new file called example.session
will be created allowing Pyrogram executing API calls with your identity.
Important: The *.session
file must be kept secret.
Note: The authorization process is executed only once. However, the code above is always required; as soon as a valid session file exists, Pyrogram will use that and won't ask you to enter your phone number again when you restart your script.