Skip to main content

⚓️ Ankr Python SDK

Compact Python library for interacting with Ankr's Advanced APIs.

Get started in 2 minutes

1. Install the package from PyPi

pip install ankr-sdk

2. Initialize the SDK

Note: to use Advanced API for free starting from May 29th, 2023 you have to register on the platform.

Get your individual endpoint here https://www.ankr.com/rpc/advanced-api and provide it to the AnkrWeb3 class.

from ankr import AnkrWeb3

ankr_w3 = AnkrWeb3("YOUR-TOKEN")

3. Use the sdk and call one of the supported methods

Node's API

from ankr import AnkrWeb3
ankr_w3 = AnkrWeb3("YOUR-TOKEN")

eth_block = ankr_w3.eth.get_block("latest")
bsc_block = ankr_w3.bsc.get_block("latest")
polygon_block = ankr_w3.polygon.get_block("latest")

Ankr NFT API

from ankr import AnkrWeb3
from ankr.types import Blockchain, GetNFTsByOwnerRequest

ankr_w3 = AnkrWeb3("YOUR-TOKEN")

nfts = ankr_w3.nft.get_nfts(
    request=GetNFTsByOwnerRequest(
        blockchain=Blockchain.Eth,
        walletAddress="0x0E11A192d574b342C51be9e306694C41547185DD"
    )
)

Ankr Token API

from ankr import AnkrWeb3
from ankr.types import GetAccountBalanceRequest

ankr_w3 = AnkrWeb3("YOUR-TOKEN")

assets = ankr_w3.token.get_account_balance(
    request=GetAccountBalanceRequest(
        walletAddress="0x77A859A53D4de24bBC0CC80dD93Fbe391Df45527"
    )
)

Ankr Query API

from ankr import AnkrWeb3
from ankr.types import Blockchain, GetLogsRequest

ankr_w3 = AnkrWeb3("YOUR-TOKEN")

logs = ankr_w3.query.get_logs(
    request=GetLogsRequest(
        blockchain=[Blockchain.Eth],
        fromBlock=1181739,
        toBlock=1181739,
        address=["0x3589d05a1ec4af9f65b0e5554e645707775ee43c"],
        topics=[
            [],
            ["0x000000000000000000000000feb92d30bf01ff9a1901666c5573532bfa07eeec"],
        ],
        decodeLogs=True,
    ),
    limit=10
)

Ankr Advanced APIs supported chains

ankr-sdk supports the following chains at this time:

Mainnet

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

Testnet

  • Ethereum Sepolia: "eth_sepolia"
  • Ethereum Holesky: "eth_holesky"
  • Avalanche Fuji: "avalanche_fuji"
  • Polygon Amoy: "polygon_amoy"
  • Optimism Sepolia: "optimism_sepolia"
  • Base Sepolia: "base_sepolia"
  • Neura Devnet "neura_devnet"
  • Neura Testnet "neura_testnet_v1"

Appchain Testnet

  • Incentiv Devnet "incentiv_devnet_v3"
  • Incentiv Testnet "incentiv_testnet"

When passing blockchain, you can use one available from types.py (preferred) or just a string value.

Available methods

ankr-sdk supports the following methods:

Early Access

Token API

NFT API

Query API

Note: some methods are available in *_raw format, allowing to get full reply with syncStatus and control pagination by hands.

Pagination

  • methods with *_raw ending support customized pagination, where you are controlling it, using pageSize and pageToken
  • other methods support automatic pagination, DON'T use pageSize and pageToken fields to these methods

Examples

Early Access API

get_token_price_history / get_token_price_history_raw

Get a list of history of the price for given contract to given timestamp.

from ankr import AnkrAdvancedAPI
from ankr.types import Blockchain, GetTokenPriceHistoryRequest

advancedAPI = AnkrAdvancedAPI("YOUR-TOKEN")

result = advancedAPI.get_token_price_history(
    request=GetTokenPriceHistoryRequest(
        blockchain=Blockchain.Eth,
        contractAddress='0x50327c6c5a14dcade707abad2e27eb517df87ab5',
        toTimestamp=1696970653,
        interval=100,
        limit=10
    )
)
print(result)

Token API

explain_token_price / explain_token_price_raw

Get a list of tokens and pool how price for calculated.

from ankr import AnkrAdvancedAPI
from ankr.types import Blockchain, ExplainTokenPriceRequest

advancedAPI = AnkrAdvancedAPI("YOUR-TOKEN")

pairs, estimates = advancedAPI.explain_token_price(
    request=ExplainTokenPriceRequest(
        blockchain=Blockchain.Eth,
        tokenAddress='0x8290333cef9e6d528dd5618fb97a76f268f3edd4',
        blockHeight=17463534,
    )
)

print(pairs)
print(estimates)

get_account_balance / get_account_balance_raw

Get the coin and token balances of a wallet.

from ankr import AnkrAdvancedAPI
from ankr.types import GetAccountBalanceRequest

advancedAPI = AnkrAdvancedAPI("YOUR-TOKEN")

result = advancedAPI.get_account_balance(
    request=GetAccountBalanceRequest(
        walletAddress="0x77A859A53D4de24bBC0CC80dD93Fbe391Df45527"
    )
)

for balance in result:
    print(balance)

get_currencies / get_currencies_raw

Get a list of supported currencies for a given blockchain.

from ankr import AnkrAdvancedAPI
from ankr.types import Blockchain, GetCurrenciesRequest

advancedAPI = AnkrAdvancedAPI("YOUR-TOKEN")

result = advancedAPI.get_currencies(
    request=GetCurrenciesRequest(
        blockchain=Blockchain.Fantom,
    )
)

for currency in result:
    print(currency)

get_token_holders / get_token_holders_raw

Get the list of token holders for a given contract address.

from ankr import AnkrAdvancedAPI
from ankr.types import Blockchain, GetTokenHoldersRequest

advancedAPI = AnkrAdvancedAPI("YOUR-TOKEN")

result = advancedAPI.get_token_holders(
    request=GetTokenHoldersRequest(
        blockchain=Blockchain.Eth,
        contractAddress='0xdac17f958d2ee523a2206206994597c13d831ec7',
    )
)

for balance in result:
    print(balance)

get_token_holders_count_history / get_token_holders_count_history_raw

Get historical data about the number of token holders for a given contract address.

from ankr import AnkrAdvancedAPI
from ankr.types import Blockchain, GetTokenHoldersCountRequest

advancedAPI = AnkrAdvancedAPI("YOUR-TOKEN")

result = advancedAPI.get_token_holders_count_history(
    request=GetTokenHoldersCountRequest(
        blockchain=Blockchain.Eth,
        contractAddress='0xdAC17F958D2ee523a2206206994597C13D831ec7',
    )
)

for balance in result:
    print(balance)

get_token_holders_count / get_token_holders_count_raw

Get current data about the number of token holders for a given contract address.

from ankr import AnkrAdvancedAPI
from ankr.types import Blockchain, GetTokenHoldersCountRequest

advancedAPI = AnkrAdvancedAPI("YOUR-TOKEN")

result = advancedAPI.get_token_holders_count_history_raw(
    request=GetTokenHoldersCountRequest(
        blockchain=Blockchain.Eth,
        contractAddress='0xdAC17F958D2ee523a2206206994597C13D831ec7',
    )
)

print(result)

get_token_price / get_token_price_raw

Get token price by contract.

from ankr import AnkrAdvancedAPI
from ankr.types import Blockchain, GetTokenPriceRequest

advancedAPI = AnkrAdvancedAPI("YOUR-TOKEN")

result = advancedAPI.get_token_price(
    request=GetTokenPriceRequest(
        blockchain=Blockchain.Eth,
        contractAddress='',
    )
)

print(result)

get_token_transfers / get_token_transfers_raw

Get token transfers of specified address.

from ankr import AnkrAdvancedAPI
from ankr.types import Blockchain, GetTransfersRequest

advancedAPI = AnkrAdvancedAPI("YOUR-TOKEN")

result = advancedAPI.get_token_transfers(
    request=GetTransfersRequest(
        blockchain=Blockchain.Eth,
        address=['0xf16e9b0d03470827a95cdfd0cb8a8a3b46969b91'],
        fromTimestamp=1674441035,
        toTimestamp=1674441035,
        descOrder=True,
    )
)

for transfer in result:
    print(transfer)

NFT API

get_nfts / get_nfts_raw

Get data about all the NFTs (collectibles) owned by a wallet.

from ankr import AnkrAdvancedAPI
from ankr.types import Blockchain, GetNFTsByOwnerRequest

advancedAPI = AnkrAdvancedAPI("YOUR-TOKEN")

result = advancedAPI.get_nfts(
    request=GetNFTsByOwnerRequest(
        blockchain=Blockchain.Eth,
        walletAddress='0x0E11A192d574b342C51be9e306694C41547185DD',
    )
)

for nft in result:
    print(nft)

get_nft_metadata / get_nft_metadata_raw

Get NFT's contract metadata.

from ankr import AnkrAdvancedAPI
from ankr.types import Blockchain, GetNFTMetadataRequest

advancedAPI = AnkrAdvancedAPI("YOUR-TOKEN")

reply = advancedAPI.get_nft_metadata(
    request=GetNFTMetadataRequest(
        blockchain=Blockchain.Eth,
        contractAddress='0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d',
        tokenId='1500',
        forceFetch=False,
    )
)

print(reply.metadata)
print(reply.attributes)

get_nft_holders / get_nft_holders_raw

Get NFT's holders.

from ankr import AnkrAdvancedAPI
from ankr.types import Blockchain, GetNFTHoldersRequest

advancedAPI = AnkrAdvancedAPI("YOUR-TOKEN")

result = advancedAPI.get_nft_holders(
    request=GetNFTHoldersRequest(
        blockchain=Blockchain.Arbitrum,
        contractAddress='0xc36442b4a4522e871399cd717abdd847ab11fe88',
    ),
    limit=1000
)

for holder in result:
    print(holder)

get_nft_transfers / get_nft_transfers_raw

Get NFT Transfers of specified address.

from ankr import AnkrAdvancedAPI
from ankr.types import Blockchain, GetTransfersRequest

advancedAPI = AnkrAdvancedAPI("YOUR-TOKEN")

result = advancedAPI.get_nft_transfers(
    request=GetTransfersRequest(
        blockchain=[Blockchain.Eth, Blockchain.Bsc],
        address=['0xd8da6bf26964af9d7eed9e03e53415d37aa96045'],
        fromTimestamp=1672553107,
        toTimestamp=1672683207,
    )
)

for transfer in result:
    print(transfer)

Query API

get_logs / get_logs_raw

Get logs matching the filter.

from ankr import AnkrAdvancedAPI
from ankr.types import Blockchain, GetLogsRequest

advancedAPI = AnkrAdvancedAPI("YOUR-TOKEN")

result = advancedAPI.get_logs(
    request=GetLogsRequest(
        blockchain=[Blockchain.Eth],
        fromBlock=1181739,
        toBlock=1181739,
        address=["0x3589d05a1ec4af9f65b0e5554e645707775ee43c"],
        topics=[
            [],
            ["0x000000000000000000000000feb92d30bf01ff9a1901666c5573532bfa07eeec"],
        ],
        decodeLogs=True,
    ),
    limit=10
)

for log in result:
    print(log)

get_blocks / get_blocks_raw

Query data about blocks within a specified range.

from ankr import AnkrAdvancedAPI
from ankr.types import Blockchain, GetBlocksRequest

advancedAPI = AnkrAdvancedAPI("YOUR-TOKEN")

result = advancedAPI.get_blocks(
    request=GetBlocksRequest(
        blockchain=Blockchain.Eth,
        fromBlock=14500001,
        toBlock=14500004,
        descOrder=True,
        includeLogs=True,
        includeTxs=True,
        decodeLogs=True,
    )
)

for block in result:
    print(block)

get_transaction / get_transaction_raw

Query data about transaction by the transaction hash.

from ankr import AnkrAdvancedAPI
from ankr.types import GetTransactionsByHashRequest

advancedAPI = AnkrAdvancedAPI("YOUR-TOKEN")

result = advancedAPI.get_transaction(
    request=GetTransactionsByHashRequest(
        transactionHash='0x82c13aaac6f0b6471afb94a3a64ae89d45baa3608ad397621dbb0d847f51196f',
        decodeTxData=True
    )
)

print(result)

get_transactions_by_address / get_transactions_by_address_raw

Query data about transactions of specified address.

from ankr import AnkrAdvancedAPI
from ankr.types import Blockchain, GetTransactionsByAddressRequest

advancedAPI = AnkrAdvancedAPI("YOUR-TOKEN")

result = advancedAPI.get_transactions_by_address(
    request=GetTransactionsByAddressRequest(
        blockchain=Blockchain.Bsc,
        fromBlock=23593283,
        toBlock=23593283,
        address=[
            "0x97242e3315c7ece760dc7f83a7dd8af6659f8c4c"
        ],
        descOrder=True,
    )
)

for transaction in result:
    print(transaction)

get_blockchain_stats / get_blockchain_stats_raw

Returns blockchain stats (num of txs, etc.).

from ankr import AnkrAdvancedAPI
from ankr.types import Blockchain, GetBlockchainStatsRequest

advancedAPI = AnkrAdvancedAPI("YOUR-TOKEN")

result = advancedAPI.get_blockchain_stats(
    request=GetBlockchainStatsRequest(
        blockchain=Blockchain.Bsc,
    )
)

for stat in result:
    print(stat)

get_interactions / get_interactions_raw

Returns on which chain address was interacting.

from ankr import AnkrAdvancedAPI
from ankr.types import GetInteractionsRequest

advancedAPI = AnkrAdvancedAPI("YOUR-TOKEN")

result = advancedAPI.get_interactions(
    request=GetInteractionsRequest(
        address='0xF977814e90dA44bFA03b6295A0616a897441aceC',
    )
)

for blockchain in result:
    print(blockchain)

About API keys

Ankr is offering free access to Advanced API, however you have to register on Ankr platform to access it.

Get your individual API Key here https://www.ankr.com/rpc/advanced-api.

Release files for ankr-sdk 1.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ankr-sdk 1.0.3
File Size Uploaded
ankr_sdk-1.0.3.tar.gz 18.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ankr-sdk 1.0.3
File Interpreter ABI Platform
ankr_sdk-1.0.3-py3-none-any.whl Python 3 none any Details

Total release size: 35.1 kB

Release files / ankr_sdk-1.0.3.tar.gz

Download URL ankr_sdk-1.0.3.tar.gz
Size 18.3 kB
Tags Source
SHA-256 checksum
How to use checksums
401663711f1f27ac33af0ba5c4252657a0646678cc3ef7d367a6b072d178a267
BLAKE2b-256 checksum
How to use checksums
a2dea7c2756f8f3e07f3b794b493acc30889a16390bdefbfd0e6205d5afee876
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.2.1 CPython/3.11.13 Linux/6.11.0-1018-azure

Release files / ankr_sdk-1.0.3-py3-none-any.whl

Download URL ankr_sdk-1.0.3-py3-none-any.whl
Size 16.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
52ec6b9c02de7550ba9eb108cea2bc7138ef18e77274cae2d125ee68719c5be1
BLAKE2b-256 checksum
How to use checksums
460a5d964fe5741329b10873cfb03a48330328fea080ea7e180915ffe4dee61e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.2.1 CPython/3.11.13 Linux/6.11.0-1018-azure

Release history Release notifications | RSS feed

This release

1.0.3 This release

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page