Skip to main content

A Python library that provides tools for using and validating Telegram web app init data.

Project description

init-data-py

Package version License

A Python library that provides tools for using and validating Telegram web app init data.

Installation

You can install the library using pip:

pip install init-data-py

Usage

Parsing

To parse the window.Telegram.WebApp.initData query string into an InitData object for easier access to attributes and validation, you can use the InitData.parse method:

from init_data_py import InitData

init_data = InitData.parse(query_string)

Validation

To validate the init data, you can use the InitData.validate method:

is_valid = init_data.validate(bot_token)

Signing

If you need to create and sign your own init data, you can create an InitData object and sign it:

from init_data_py import InitData
from init_data_py.types import User

user = User(id=5167898484, first_name="xin")
init_data = InitData(user=user).sign(bot_token)

Converting InitData to Query String

After creating and signing init data, you can convert it to a query sting using the InitData.to_query_string method:

query_string = init_date.to_query_string()

Real-World Validation Example

This example demonstrates how to validate a Telegram Mini App init data. It parses the init data query string into an InitData object and validate it using the provided bot token, checking that the data is valid and has not expired:

from init_data_py import InitData

bot_token = "7244657541:AAEgqk0HDC3WD5cdbnGMdd6L0TJ74FDp97Y"
query_string = "query_id=AAF03wc0AgAAAHTfBzROOCVW&user=%7B%22id%22%3A5167898484%2C%22first_name%22%3A%22xin%22%2C%22last_name%22%3A%22%22%2C%22username%22%3A%22pvnimaxin%22%2C%22language_code%22%3A%22en%22%2C%22allows_write_to_pm%22%3Atrue%7D&auth_date=1722938610&hash=8654c8c617c143abf656f4f159be2539880a56f58c2d9be622f90c0346aa162b"

init_data = InitData.parse(query_string)

is_valid = init_data.validate(
    bot_token=bot_token,
    lifetime=3600,
)

License

This library is licensed under the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

init_data_py-0.2.7.tar.gz (8.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

init_data_py-0.2.7-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

Details for the file init_data_py-0.2.7.tar.gz.

File metadata

  • Download URL: init_data_py-0.2.7.tar.gz
  • Upload date:
  • Size: 8.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for init_data_py-0.2.7.tar.gz
Algorithm Hash digest
SHA256 b83f51e4f5fa75e9cb5da718155b18330cb2a2b77ec9676d401185cb536e8bb7
MD5 fdc6075bff89aecd78b4d170e7ca34aa
BLAKE2b-256 999f9b7a40f0a53a7784ed4a6d794c1f096acefa35a1270d0a9529e443b3858b

See more details on using hashes here.

Provenance

The following attestation bundles were made for init_data_py-0.2.7.tar.gz:

Publisher: publish.yml on nimaxin/init-data-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file init_data_py-0.2.7-py3-none-any.whl.

File metadata

  • Download URL: init_data_py-0.2.7-py3-none-any.whl
  • Upload date:
  • Size: 9.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for init_data_py-0.2.7-py3-none-any.whl
Algorithm Hash digest
SHA256 48c652a929912645842db044eecfb81b049927f9ca202a7fbc3215edcf69956d
MD5 48c5c1aa64fa6914c22a9e4fe8300c7f
BLAKE2b-256 afa84c2b055bf8870bc45b72c41d7bb7583a9e99a2df8d867162db9f405625cb

See more details on using hashes here.

Provenance

The following attestation bundles were made for init_data_py-0.2.7-py3-none-any.whl:

Publisher: publish.yml on nimaxin/init-data-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page