TG Stock

What is a Telegram StringSession and .session file (Telethon)

What a StringSession, a .session file and a .json file are for a Telegram account, how Telethon bots and automations use them and how to store them safely.

By TG Stock team · Updated · Ler em português

A StringSession is a piece of text that stores a Telegram account's session: the server (DC) the account lives on and the authorization key that proves the session has already logged in. A .session file stores the same thing, but as an SQLite file in Telethon's format. With either one, a Python script opens the account directly, without asking for a phone number or a code.

A .json file, on the other hand, does not open the account: it is a record of the account's details (phone, DC and 2FA status), handy for organizing many accounts in a spreadsheet or script. At TG Stock, every account comes with the StringSession and the authorization key on the order page, plus the .session and .json files to download. This guide explains what each one is, when to use it and how to store it without taking risks.

What a session is on Telegram

When you log in to Telegram, the app and the server agree on an authorization key. From then on, the app uses that key to prove who it is, without asking for a code again. Each login creates a different key, and each one shows up as a line in Settings > Devices. That is what we call a session.

A session has two essential parts:

  • The DC (data center): the Telegram server where the account lives, numbered 1 to 5.
  • The authorization key: a 256-byte block that becomes a 512-character string in hexadecimal.

Whoever has both has an open session on the account. That is why this data deserves the same care as a password.

StringSession

A StringSession is how Telethon (a Python library for the Telegram API) stores an entire session in a single string. It combines the DC, the server address, the port and the authorization key, and encodes everything into one line.

Advantages:

  • It fits in an environment variable, a database field or a secrets manager.
  • It does not depend on a file on disk, which makes life easier on servers and containers.
  • You just paste it into your code: TelegramClient(StringSession(text), api_id, api_hash).

The api_id and api_hash are your own Telegram application credentials, which you create at my.telegram.org with your own account. They identify the program, not the account being opened.

The .session file

A .session file is an SQLite file in Telethon's format. When you create a client with a name, for example TelegramClient("15551234567", api_id, api_hash), Telethon looks for 15551234567.session in the folder and uses the session stored in it.

Besides the DC and the key, the file works as a cache for Telethon: it keeps data about contacts and chats it comes across, which saves API calls over time. That is why many automation tools ask specifically for the .session file.

At TG Stock, the file is named after the account's phone number, so it is easy to tell accounts apart. It is generated ready to go, in the format Telethon opens.

The .json file

The .json file is a plain-text record of the account. It includes the phone number, the account ID, the DC and the two-step verification status. It does not contain the authorization key, so it cannot open the account on its own. It is useful to:

  • import account details into a spreadsheet or database;
  • check in a script which .session file belongs to which number;
  • keep an inventory when you work with many accounts.

Authorization key with DC

The order page also shows the authorization key in hexadecimal, with the DC in a separate field. This is the rawest form of the session. It is meant for people using another library or tool that builds the session from the key and the DC. If you use Telethon, the StringSession or the .session file already do the job and are more convenient.

All formats are summarized on the delivery formats page (the store pages are in Portuguese).

When to use each format

  • Bot or script on a server: StringSession, kept in an environment variable.
  • A tool that asks for a file: the .session file, placed in the folder the tool points to.
  • Spreadsheet or inventory: the .json file.
  • Another library: the hexadecimal authorization key plus the DC.
  • Using the account in the app, like a regular user: none of these. Sign in with the number and code, as explained in the guide on how to log in to a purchased Telegram account. For a computer, there is the Telegram Desktop folder (see how to open the account in Telegram Desktop).

The golden rule: one session, one place

The StringSession, the .session file, the hexadecimal key and the Telegram Desktop folder from your order are the same session in different formats. If you open that session in two places at once, for example on a server and on your own computer, with different IP addresses, Telegram may decide the key was copied and terminate the session. Then both places go down.

How to avoid it:

  • Run each session in a single place.
  • If you need the account in two places, create a second session: sign in with the number and code on the other device. The code arrives in the official "Telegram" chat (777000), and the login code button on the order page shows it to you.
  • Do not paste the same StringSession into several servers "as a backup". Keep the backup stored and unused.

How to store sessions safely

Whoever has the session has the account, as long as that session is active. Treat this data like a password:

  • Never commit it to Git. Add *.session to your .gitignore and keep the StringSession out of your code, in an environment variable or an unversioned .env file.
  • Do not send it in chats or paste it into public text-sharing sites.
  • Keep an encrypted backup (a password manager, a password-protected drive). If the server breaks, you do not lose access.
  • Restrict permissions on the server folder where the files live.
  • Turn on 2FA on the account. The session still opens the account, but any new login will require your password. See how to secure the account.

Also keep in mind that if you terminate the session in Settings > Devices, every format of it stops working at once. The key is no longer valid on Telegram's servers, and there is no way to "reactivate" the file.

Many accounts: bulk orders

If you buy in bulk, the accounts are delivered one by one in the background and you download a single .zip with the .session and .json files of every delivered account. Each pair is named after the phone number, ready to import into a tool. Bulk orders are paid with store balance only (the store is in Portuguese and balance is topped up by Pix in Brazilian reais). When bulk buying makes sense is covered in the guide on buying Telegram accounts in bulk.

Common questions

Can I convert a StringSession into a .session file and back?

Both store the same essential data, and Telethon can work with either. But there is no need to convert anything: the store already delivers both formats ready to use.

Does it work with libraries other than Telethon?

The StringSession and the .session file are in Telethon's format. Other libraries have their own formats; for them, the hexadecimal authorization key plus the DC is the starting point.

Do I need the login code to use the session?

No. The session has already logged in. The code is only needed when you want to open a new session, for example in the phone app.

Does the session expire?

It stays valid until it is terminated, whether by you, by another session on the account or by Telegram itself. That is why the one-place rule makes such a difference.

Read next