Advanced API
Ankr.py SDK

Ankr.py SDK

Ankr.py SDK is a compact Python library that enables interaction with Advanced API.

Get started

  1. Install the latest package stored on PyPi (opens in a new tab).
pip install ankr-sdk
  1. Initialize the SDK.

Sign in to the RPC Service (opens in a new tab) platform, get your individual endpoint from the Advanced API (opens in a new tab) pane, and provide it to the AnkrWeb3 class.

from ankr import AnkrWeb3
 
ankr_w3 = AnkrWeb3("YOUR_ENDPOINT")
  1. Use the SDK to call the methods supported:
  • Node API
eth_block = ankr_w3.eth.get_block("latest")
bsc_block = ankr_w3.bsc.get_block("latest")
polygon_block = ankr_w3.polygon.get_block("latest")
  • NFT API
from ankr.types import Blockchain
 
nfts = ankr_w3.nft.get_nfts(
    blockchain=[Blockchain.ETH, Blockchain.BSC],
    wallet_address="0x0E11A192d574b342C51be9e306694C41547185DD",
    filter=[
        {"0x700b4b9f39bb1faf5d0d16a20488f2733550bff4": []},
        {"0xd8682bfa6918b0174f287b888e765b9a1b4dc9c3": ["8937"]},
    ],
)
  • Token API
assets = ankr_w3.token.get_account_balance(
    wallet_address="0x77A859A53D4de24bBC0CC80dD93Fbe391Df45527"
)
  • Query API
logs = ankr_w3.query.get_logs(
    blockchain="eth",
    from_block="0xdaf6b1",
    to_block=14350010,
    address=["0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"],
    topics=[
        [],
        ["0x000000000000000000000000def1c0ded9bec7f1a1670819833240f027b25eff"],
    ],
    decode_logs=True,
)

Chains supported

Currently, ankr.py supports interaction with the following chains using their aliases:

Mainnet:

  • Arbitrum: arbitrum.
  • Avalanche: avalanche.
  • Base: base.
  • BNB Smart Chain: bsc.
  • Ethereum: eth.
  • Fantom: fantom.
  • Flare: flare.
  • Gnosis: gnosis
  • Linea: linea.
  • Optimism: optimism.
  • Polygon: polygon.
  • Polygon zkEVM: polygon_zkevm.
  • Rollux: rollux.
  • Scroll: scroll.
  • Stellar: stellar.
  • Syscoin: syscoin.
  • Telos: telos
  • Xai: xai.
  • X Layer: xlayer

Testnet:

  • Avalanche Fuji: avalanche_fuji.
  • Base Sepolia: base_sepolia
  • Ethereum Holesky: eth_holesky.
  • Ethereum Sepolia: eth_sepolia.
  • Optimism Testnet: optimism_testnet.
  • Polygon Amoy: polygon_amoy.

Methods supported

get_nfts

Retrieves data on all the NFTs (collectibles) owned by a wallet.

nfts = ankr_w3.nft.get_nfts(
    blockchain="eth",
    wallet_address="0x0E11A192d574b342C51be9e306694C41547185DD",
    filter=[
        {"0x700b4b9f39bb1faf5d0d16a20488f2733550bff4": []},
        {"0xd8682bfa6918b0174f287b888e765b9a1b4dc9c3": ["8937"]},
    ],
)

get_nft_metadata

Retrieves metadata of a particular NFT.

nfts = ankr_w3.nft.get_nft_metadata(
    blockchain="eth",
    contract_address="0x4100670ee2f8aef6c47a4ed13c7f246e621228ec",
    token_id="4",
)

get_token_holders

Retrieves holders of a particular NFT.

holders = ankr_w3.token.get_token_holders(
    blockchain="bsc",
    contract_address="0xf307910A4c7bbc79691fD374889b36d8531B08e3",
    limit=10,
)

get_token_holders_count_history

Retrieves the number of token holders for the particular period of time.

daily_holders_history = ankr_w3.token.get_token_holders_count_history(
    blockchain="bsc",
    contract_address="0xf307910A4c7bbc79691fD374889b36d8531B08e3",
    limit=10,  # last 10 days history
)

get_token_holders_count

Retrieves the number of token holders on the latest block.

holders_count = ankr_w3.token.get_token_holders_count(
    blockchain="bsc",
    contract_address="0xf307910A4c7bbc79691fD374889b36d8531B08e3",
)

get_account_balance

Retrieves the balance of a particular account.

assets = ankr_w3.token.get_account_balance(
    wallet_address="0x77A859A53D4de24bBC0CC80dD93Fbe391Df45527",
    blockchain=["eth", "bsc"],
)

get_logs

Retrieves history data of a particular block range.

logs = ankr_w3.query.get_logs(
    blockchain="eth",
    from_block="0xdaf6b1",
    to_block=14350010,
    address=["0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"],
    topics=[
        [],
        ["0x000000000000000000000000def1c0ded9bec7f1a1670819833240f027b25eff"],
    ],
    decode_logs=True,
)

get_blocks

Retrieves full info of a particular block.

blocks = ankr_w3.query.get_blocks(
    blockchain="eth",
    from_block=14500001,
    to_block=14500001,
    desc_order=True,
    include_logs=True,
    include_txs=True,
    decode_logs=True,
)

get_transaction

Retrieves the details of a transaction specified by hash.

tx = ankr_w3.query.get_transaction(
    transaction_hash="0x82c13aaac6f0b6471afb94a3a64ae89d45baa3608ad397621dbb0d847f51196f",
    include_logs=True,
    decode_logs=True,
    decode_tx_data=True,
)