Skip to main content

Provide access to indexed TON blockchain.

Project description

PyTONAPI

PyPI Python Versions License

Python wrapper for tonapi.io v2
You need an api key to use it, get it here tonconsole.com

Dependencies

Installation

pip install pytonapi

Examples

Get account info:

# Importing required package
from pytonapi import Tonapi


def main():
    # Creating new Tonapi object
    tonapi = Tonapi(api_key="Your api key")

    account_id = "EQC-3ilVr-W0Uc3pLrGJElwSaFxvhXXfkiQA3EwdVBHNNess"  # noqa
    account = tonapi.accounts.get_info(account_id=account_id)

    # print account address (raw)
    print(account.address.to_raw())
    # output: 0:bede2955afe5b451cde92eb189125c12685c6f8575df922400dc4c1d5411cd35

    # print account address (userfriendly)
    print(account.address.to_userfriendly())
    # output: EQC-3ilVr-W0Uc3pLrGJElwSaFxvhXXfkiQA3EwdVBHNNess

    # print account balance (nanoton)
    print(account.balance.to_nano())
    # output: 1500000000

    # print account balance (amount)
    print(account.balance.to_amount())
    # output: 1.5


if __name__ == '__main__':
    main()

Asynchronous example:

# Importing required package
import asyncio

from pytonapi import AsyncTonapi


# Declaring asynchronous function for using await
async def main():
    # Creating new Tonapi object
    tonapi = AsyncTonapi(api_key="Your api key")

    account_id = "EQC-3ilVr-W0Uc3pLrGJElwSaFxvhXXfkiQA3EwdVBHNNess"  # noqa
    account = await tonapi.accounts.get_info(account_id=account_id)

    # print account address (raw)
    print(account.address.to_raw())
    # output: 0:bede2955afe5b451cde92eb189125c12685c6f8575df922400dc4c1d5411cd35

    # print account address (userfriendly)
    print(account.address.to_userfriendly())
    # output: EQC-3ilVr-W0Uc3pLrGJElwSaFxvhXXfkiQA3EwdVBHNNess

    # print account balance (nanoton)
    print(account.balance.to_nano())
    # output: 1500000000

    # print account balance (amount)
    print(account.balance.to_amount())
    # output: 1.5


if __name__ == '__main__':
    # Running asynchronous function
    asyncio.run(main())


Get account transactions:

# Importing required package
from pytonapi import Tonapi
from pytonapi.utils import nano_to_amount


def main():
    # Creating new Tonapi object
    tonapi = Tonapi(api_key="Your api key")

    account_id = "EQC-3ilVr-W0Uc3pLrGJElwSaFxvhXXfkiQA3EwdVBHNNess"  # noqa
    result = tonapi.blockchain.get_account_transactions(
        account_id=account_id, limit=100,
    )
    for transaction in result.transactions:

        # print transaction value (nanoton)
        print(transaction.in_msg.value)
        # output: 1000000000

        # print transaction value (amount)
        print(nano_to_amount(transaction.in_msg.value))
        # output: 1.0

        # print transaction text comment
        if transaction.in_msg.decoded_op_name == "text_comment":
            print(transaction.in_msg.decoded_body["Text"])
            # output: Hello, World!


if __name__ == '__main__':
    main()


Get NFT items from collection by collection address:

# Importing required package
from pytonapi import Tonapi


def main():
    # Creating new Tonapi object
    tonapi = Tonapi(api_key="Your api key")

    account_id = "EQAUxYSo-UwoqAGixaD3d7CNLp9PthgmEZfnr6BvsijzJHdA"  # noqa
    result = tonapi.nft.get_items_by_collection_address(
        account_id=account_id, limit=100, offset=0
    )
    for nft in result.nft_items:
        # print nft address (raw)
        print(nft.address.to_raw())
        # output: 0:74c15fd0695c4706213ded4ac45eb7a2f87ff4c8f5d6bc904fe7dd1b735936e4

        # print nft address (userfriendly)
        print(nft.address.to_userfriendly())
        # output: EQB0wV_QaVxHBiE97UrEXrei-H_0yPXWvJBP590bc1k25KtT

        # print nft owner address
        print(nft.owner.address.to_userfriendly())
        # output: EQCOF6xKqliQjY1yrwwn7qm8OjyEJBpH_sqVCe-XgMak5MDN

        # print nft previews url
        print(nft.previews[0].url)
        # output: https://cache.tonapi.io/imgproxy/c0wiqWVwr79OCcmR7qD-4WIKWOnk8t4vX_cqmohhE8s/rs:fill:100:100:1/g:no/aHR0cHM6Ly9uZnQudG9ubWVuZG9uLmNvbS9jb2xsZWN0aW9ucy9jYXJkcy8zMi5wbmc.webp


if __name__ == '__main__':
    main()


Get NFT items by owner address:

# Importing required package
from pytonapi import Tonapi


def main():
    # Creating new Tonapi object
    tonapi = Tonapi(api_key="Your api key")

    account_id = "EQC-3ilVr-W0Uc3pLrGJElwSaFxvhXXfkiQA3EwdVBHNNess"  # noqa
    result = tonapi.accounts.get_nfts(
        account_id=account_id, limit=10, offset=0
    )
    for nft in result.nft_items:
        # print nft address (raw)
        print(nft.address.to_raw())
        # output: 0:74c15fd0695c4706213ded4ac45eb7a2f87ff4c8f5d6bc904fe7dd1b735936e4

        # print nft address (userfriendly)
        print(nft.address.to_userfriendly())
        # output: EQB0wV_QaVxHBiE97UrEXrei-H_0yPXWvJBP590bc1k25KtT

        # print nft collection address
        print(nft.collection.address.to_userfriendly())
        # output: EQCA14o1-VWhS2efqoh_9M1b_A9DtKTuoqfmkn83AbJzwnPi

        # print nft previews url
        print(nft.previews[0].url)
        # output: https://cache.tonapi.io/imgproxy/c0wiqWVwr79OCcmR7qD-4WIKWOnk8t4vX_cqmohhE8s/rs:fill:100:100:1/g:no/aHR0cHM6Ly9uZnQudG9ubWVuZG9uLmNvbS9jb2xsZWN0aW9ucy9jYXJkcy8zMi5wbmc.webp


if __name__ == '__main__':
    main()

And more . . .


Donations:

EQC-3ilVr-W0Uc3pLrGJElwSaFxvhXXfkiQA3EwdVBHNNess

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

pytonapi-0.0.4.tar.gz (16.9 kB view details)

Uploaded Source

File details

Details for the file pytonapi-0.0.4.tar.gz.

File metadata

  • Download URL: pytonapi-0.0.4.tar.gz
  • Upload date:
  • Size: 16.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 colorama/0.4.4 importlib-metadata/4.6.4 keyring/23.5.0 pkginfo/1.8.2 readme-renderer/34.0 requests-toolbelt/0.9.1 requests/2.25.1 rfc3986/1.5.0 tqdm/4.57.0 urllib3/1.26.5 CPython/3.10.6

File hashes

Hashes for pytonapi-0.0.4.tar.gz
Algorithm Hash digest
SHA256 51f35404b881a4be3047ad465ea893b4368d56a296f8642872541c52dbd76f78
MD5 4ec2c9320a1ce4a4d2426b3111ab83c7
BLAKE2b-256 27d05013da3e0d37854a56ef6c531ccdd5b845dd6fea0637989680788ce44e94

See more details on using hashes here.

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