Get Crypto Knowledge Utilizing a Python API for Cryptocurrency


This tutorial will educate you to make use of the Python-compatible Web3 Knowledge API from Moralis to drag cryptocurrency costs, transactions, balances, and extra! If this sounds attention-grabbing and you might be desirous to get crypto information utilizing Python, you could find three core endpoints examples beneath: 

get_token_price()

from moralis import evm_api

api_key = "YOUR_API_KEY"
params = {
    "handle": "0x2260fac5e5542a773aa44fbcfedf7c193bc2c599",
    "chain": "eth"
}

consequence = evm_api.token.get_token_price(
    api_key=api_key,
    params=params,
)

print(consequence)

get_wallet_token_balances()

from moralis import evm_api

api_key = "YOUR_API_KEY"
params = {
    "handle": "0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d",
    "chain": "eth",
}

consequence = evm_api.token.get_wallet_token_balances(
    api_key=api_key,
    params=params,
)

print(consequence)

get_wallet_token_transfers()

from moralis import evm_api

api_key = "YOUR_API_KEY"
params = {
    "handle": "0x2260fac5e5542a773aa44fbcfedf7c193bc2c599",
    "chain": "eth"
}

consequence = evm_api.token.get_wallet_token_transfers(
    api_key=api_key,
    params=params,
)

print(consequence)

Getting crypto information with the Moralis Web3 Knowledge API and Python has by no means been simpler. So, bear in mind to enroll with Moralis and begin leveraging the ability of the Web3 {industry} in the present day! By familiarizing your self with this programming interface, you possibly can construct Web3-based Python initiatives in a heartbeat.  

Get Crypto Data Using a Python API for Cryptocurrency - Sign Up with Moralis

Overview 

At present’s article will discover the Python-compatible Web3 Knowledge API from Moralis and use this interface for cryptocurrency improvement. We’ll exhibit the accessibility of the Web3 Knowledge API by displaying you use this software to question blockchain information with Python. Extra particularly, we are going to cowl the next 5 examples: 

  1. Learn how to pull cryptocurrency costs
  2. Learn how to get historic ERC-20 token costs
  3. Learn how to question pockets token balances
  4. Learn how to get pockets token transfers
  5. Learn how to question ERC-20 metadata by contract

If this sounds thrilling and also you wish to soar straight into the coding half, click on right here to instantly navigate to the ”Python API for Cryptocurrency – Get Crypto Knowledge” part! 

Along with the principle tutorial, we are going to provoke the article by answering the query, ”is there a Python API to drag cryptocurrency costs, balances, and different blockchain information?”. In doing so, we are going to briefly discover the intricacies of the Python-compatible Web3 Knowledge API and what it entails. Together with the Web3 Knowledge API, Moralis additionally options one other Python-compatible API: the Web3 Streams API. With this interface, you possibly can effortlessly arrange your individual stream to watch any blockchain occasion. If you wish to learn to use this API with Python to watch cryptocurrency occasions, you might be in luck, as we are going to cowl simply that within the ”Utilizing the Moralis Streams API with Python” part! 

Lastly, within the article’s remaining part, we are going to discover Python in a Web3 context and the way Moralis can additional elevate your blockchain improvement recreation. In any case, what you’ll study on this article solely scratches the floor of what’s attainable with Moralis!

Additionally, bear in mind to enroll with Moralis instantly, as you want an account to make calls to the varied APIs!

Moralis Python API for Cryptocurrency Landing Page

Is There a Python API to Pull Cryptocurrency Costs? 

So, is there a Python API to drag cryptocurrency costs, balances, and different blockchain information? Should you’re entering into Web3 Python improvement, you’ll be comfortable to listen to that the reply to the aforementioned query is a powerful sure! 

The Moralis Web3 Knowledge API suite is absolutely Python-compatible. This implies you possibly can make the most of the Python SDK to effortlessly make API calls to any of the Moralis endpoints. Consequently, you possibly can seamlessly use the Web3 Knowledge API and Python to seamlessly pull cryptocurrency costs, get all ERC-20 tokens owned by an handle, question transfers by pockets, and way more!

Python-Compatible API for Crypto

Moreover, the Web3 Knowledge API from Moralis is chain-agnostic, which means that the interface helps a wide range of completely different blockchain networks. This consists of EVM-compatible chains equivalent to Ethereum, Polygon, BNB Chain, and different networks like Aptos and Solana. As such, you possibly can seamlessly use Moralis APIs and Python to, as an example, pull cryptocurrency costs throughout a number of completely different blockchain networks. 

For instance, in case you are fascinated about Solana improvement, you may discover our article exploring the Solana Python API attention-grabbing! 

So, how precisely does this work? In order for you the reply to this query, be part of us within the subsequent part as we discover get crypto information utilizing the Moralis Knowledge API and Python!

Python API for Cryptocurrency – Get Crypto Knowledge 

Within the following sections, we are going to dive deeper into how you need to use the Web3 API to get cryptocurrency information with Python. In doing so, we are going to discover 5 distinguished examples: 

  1. Learn how to pull cryptocurrency costs
  2. Learn how to get historic ERC-20 token costs
  3. Learn how to question pockets token balances
  4. Learn how to get pockets token transfers
  5. Learn how to question ERC-20 metadata by contract

Nonetheless, earlier than leaping straight into the primary instance displaying you pull cryptocurrency costs, we should deal with just a few stipulations. These stipulations are common for every of our examples. So, ensure to cowl them earlier than persevering with! 

Moralis Web3 Knowledge API with Python – Conditions

Earlier than you progress, ensure to have the next prepared: 

  • Set up Python and PIP – Obtain and set up Python and PIP.
  • Set Up Moralis – Join with Moralis and purchase your Web3 API key. When you register, you could find your key by logging in and navigating to the ”Web3 APIs” tab:
Web3 API Landing Page - Key to for Python API to Pull Crypto Prices
  • Create a Python Venture – Create a brand new Python undertaking with an ”index.py” file. Set up the Moralis SDK by opening a brand new terminal and working the next command within the undertaking’s root folder: 
pip set up moralis

With the stipulations lined, you at the moment are able to discover the varied Moralis Web3 Knowledge API endpoint. So, allow us to begin by overlaying how one can pull cryptocurrency costs with Python!

Learn how to Pull Cryptocurrency Costs 

When utilizing the Moralis Web3 Knowledge API and Python, all you’ll want to pull cryptocurrency costs of ERC-20 tokens is a single name to the get_token_price() endpoint. So, open the ”index.py” file you created within the earlier part and add the next code snippet:  

from moralis import evm_api

api_key = "YOUR_API_KEY"
params = {
    "handle": "0x2260fac5e5542a773aa44fbcfedf7c193bc2c599",
    "chain": "eth"
}

consequence = evm_api.token.get_token_price(
    api_key=api_key,
    params=params,
)

print(consequence)

From right here, you’ll want to make just a few minor configurations to the code. First, change YOUR_API_KEY with the Moralis API key you fetched when taking good care of the stipulations. Subsequent, specify the chain and handle parameters to suit your preferences.

That’s it; you need to now be capable of run the script utilizing the next command: 

python index.py

In return, you need to end up with a response wanting one thing like this: 

{
  "nativePrice": {
    "worth": "13753134139373781549",
    "decimals": 18,
    "identify": "Ether",
    "image": "ETH"
  },
  "usdPrice": 16115.165641767926,
  "exchangeAddress": "0x1f98431c8ad98523631ae4a59f267346ea31f984",
  "exchangeName": "Uniswap v3"
}

Congratulations! It’s as simple as that to drag cryptocurrency costs when working with the Moralis Knowledge API and Python!

Get Historic ERC-20 Token Costs 

Subsequent, we are going to look nearer at how one can question historic ERC-20 token costs with the Web3 Knowledge API and Python. To take action, we are going to use the identical get_token_price() endpoint; nevertheless, this time, you’ll want to specify a spread and question one block at a time by way of a for loop. As such, you possibly can go forward and enter the next into the ”index.py” file: 

from moralis import evm_api

api_key = "YOUR_API_KEY"
historicalPrice = []

for to_block in vary(16323500, 16323550, 10):
  params = {
    "handle": "0x2260fac5e5542a773aa44fbcfedf7c193bc2c599",
    "chain": "eth",
    "to_block": to_block
  }
  consequence = evm_api.token.get_token_price(
    api_key=api_key,
    params=params,
  )

  historicalPrice.append(consequence)

print(historicalPrice)

This time, you’ll want to change YOUR_API_KEY together with your Moralis key and specify the chain and handle parameters, alongside together with your most popular vary. As soon as you might be executed with these configurations, you possibly can as soon as once more run the script with the next terminal enter:  

python index.py

In return, you get a response containing an array of various costs from varied deadlines. Right here is an instance containing two costs:

[
  {
    "nativePrice": {
      "value": "642828540698243",
      "decimals": 18,
      "name": "Ether",
      "symbol": "ETH"
    },
    "usdPrice": 0.7811524052648599,
    "exchangeAddress": "0x1f98431c8ad98523631ae4a59f267346ea31f984",
    "exchangeName": "Uniswap v3"
  },
  {
    "nativePrice": {
      "value": "642828540698243",
      "decimals": 18,
      "name": "Ether",
      "symbol": "ETH"
    },
    "usdPrice": 0.7811524052648599,
    "exchangeAddress": "0x1f98431c8ad98523631ae4a59f267346ea31f984",
    "exchangeName": "Uniswap v3"
  },
]

Learn how to Question Pockets Token Balances 

For the third instance, we are going to present you question pockets token balances. To get this info, we are going to make an API name to the get_wallet_token_balances() endpoint. As such, copy the snippet beneath and enter the code into your ”index.py” file: 

from moralis import evm_api

api_key = "YOUR_API_KEY"
params = {
    "handle": "0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d",
    "chain": "eth",
}

consequence = evm_api.token.get_wallet_token_balances(
    api_key=api_key,
    params=params,
)

print(consequence)

You may observe the identical sample right here and add your Moralis API key by changing YOUR_API_KEY. Subsequent, modify the handle and chain parameters to suit your improvement wants. From there, all that is still is working the code with the identical terminal enter: 

python index.py

In return, that is what the response ought to seem like: 

[
  {
    "token_address": "0xefd6c64533602ac55ab64442307f6fe2c9307305",
    "name": "APE",
    "symbol": "APE",
    "logo": null,
    "thumbnail": null,
    "decimals": 18,
    "balance": "101715701444169451516503179"
  },
  {
    "token_address": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
    "name": "Wrapped Ether",
    "symbol": "WETH",
    "logo": "https://cdn.moralis.io/eth/0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2.webp",
    "thumbnail": "https://cdn.moralis.io/eth/0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2_thumb.webp",
    "decimals": 18,
    "balance": "85000000000000000"
  }
]

Learn how to Get Pockets Token Transfers 

Subsequent, allow us to present you get pockets token transfers. Getting this information is straightforward when working with the Moralis Web3 Knowledge API and Python. In actual fact, all you want is a single API name to the get_wallet_token_transfers() endpoint: 

from moralis import evm_api

api_key = "YOUR_API_KEY"
params = {
    "handle": "0x2260fac5e5542a773aa44fbcfedf7c193bc2c599",
    "chain": "eth"
}

consequence = evm_api.token.get_wallet_token_transfers(
    api_key=api_key,
    params=params,
)

print(consequence)

When you add the snippet above to your ”index.py” file, you will need to alter the code by including your API key and configuring the parameters to suit your wants. You may then run the script with the command beneath: 

python index.py

If you run the code, you need to end up with a response just like the one proven beneath in your terminal: 

{
  "complete": 126,
  "web page": 0,
  "page_size": 100,
  "cursor": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhZGRyZXNzIjoiMHgyMjYwZmFjNWU1NTQyYTc3M2FhNDRmYmNmZWRmN2MxOTNiYzJjNTk5IiwiY2hhaW4iOiJldGgiLCJhcGlLZXlJZCI6MTkwNjU5LCJsaW1pdCI6MTAwLCJ0b3BpYzMiOiI9Om51bGwiLCJ0b19ibG9jayI6IjExMTAwMDQ1IiwicGFnZSI6MSwidG90YWwiOjEyNiwib2Zmc2V0IjoxLCJ1YyI6dHJ1ZSwiaWF0IjoxNjY5NjQ2ODMzfQ.NIWg35DjoTMlaE6JaoJld24p9zBgGL56Zp8PPzQnJk4",
  "consequence": [
    {
      "transaction_hash": "0x1f1c8971dec959d38bcaa5606eb474d028617752240727692cd5ef21a435d847",
      "address": "0x2260fac5e5542a773aa44fbcfedf7c193bc2c599",
      "block_timestamp": "2022-11-19T20:01:23.000Z",
      "block_number": "16006313",
      "block_hash": "0x415f96c01f32d38046e250a357e471000c0876cc2122167056cf4c4c1113a522",
      "to_address": "0x2260fac5e5542a773aa44fbcfedf7c193bc2c599",
      "from_address": "0xa9d1e08c7793af67e9d92fe308d5697fb81d3e43",
      "value": "4489517",
      "transaction_index": 48,
      "log_index": 66
    }
  ]
}

Learn how to Question ERC-20 Metadata by Contract 

For our final instance of how you need to use the Moralis Web3 Knowledge API with Python, we are going to present you question ERC-20 metadata based mostly on a contract handle. To get this information, Moralis offers you with the get_token_metadata() endpoint. To name this endpoint, enter the next code into your ”index.py” file:

from moralis import evm_api

api_key = "YOUR_API_KEY"
params = {
    "addresses": ["0x1f9840a85d5af5bf1d1762f925bdaddc4201f984"],
    "chain": "eth"
}

consequence = evm_api.token.get_token_metadata(
    api_key=api_key,
    params=params,
)

print(consequence)

Subsequent, swap out YOUR_API_KEY to your Moralis Web3 API key. From there, acquire the contract handle you wish to question and change the parameters accordingly. You may then run the script by inputting python index.py into your terminal and hitting enter.  

Because of this, you need to obtain a JSON response just like this:

[
  {
    "address": "0x1f9840a85d5af5bf1d1762f925bdaddc4201f984",
    "name": "Uniswap",
    "symbol": "UNI",
    "decimals": "18",
    "logo": "https://cdn.moralis.io/eth/0x1f9840a85d5af5bf1d1762f925bdaddc4201f984.webp",
    "logo_hash": "064ee9557deba73c1a31014a60f4c081284636b785373d4ccdd1b3440df11f43",
    "thumbnail": "https://cdn.moralis.io/eth/0x1f9840a85d5af5bf1d1762f925bdaddc4201f984_thumb.webp",
    "block_number": null,
    "validated": null,
    "created_at": "2022-01-20T10:39:55.818Z"
  }
]

Utilizing the Moralis Streams API with Python 

Along with querying information, Moralis options one other enterprise-grade, Python-compatible Web3 API for monitoring on-chain occasions: Moralis Streams. With this industry-leading interface, you possibly can seamlessly arrange your individual streams to watch blockchain occasions based mostly in your filters! 

To study extra concerning the Moralis Streams API and monitor blockchain occasions utilizing Python, try the Moralis YouTube video beneath. On this clip, you initially get to discover the variations between working with Moralis Streams vs Web3 Py. At ”06:45”, our proficient software program engineer begins the Moralis Streams API Python tutorial, displaying you monitor USDT transactions effortlessly: 

It’s also possible to discover the code used within the video above by testing the next GitHub repository: 

Full Moralis Streams API Python Repository – https://github.com/MoralisWeb3/youtube-tutorials/tree/most important/moralis-streams-vs-web3-py 

Exploring Python for Cryptocurrency Improvement 

There are a lot of similarities between standard Web2 and Web3 Python improvement. Nonetheless, in case you are new to the blockchain area, there are just a few minor variations so that you can think about. As an illustration, mastering libraries equivalent to Web3.py will be extremely helpful. This can be a library making it simpler to work together with the Ethereum blockchain. 

What’s extra, there are further improvement instruments, equivalent to Web3 infrastructure suppliers like Moralis, that may make your life as a Web3 Python developer simpler. Moralis is an industry-leading Web3 infrastructure supplier, supplying Python-compatible instruments like Web3 APIs, SDKs, and extra. 

You probably have adopted alongside this far, you’ve got now familiarized your self with the Web3 Knowledge API and Streams API. Nonetheless, in doing so, we’ve solely scratched the floor of what’s attainable with these interfaces, and there are numerous different endpoints and use circumstances to find! 

With these improvement assets, you possibly can effortlessly begin constructing extra complicated initiatives. If that is your ambition, try both of our articles on create a DAO or construct a Web3 Amazon clone!

So, in case you are fascinated about taking your Web3 initiatives to the subsequent step, bear in mind to enroll with Moralis. In doing so, you can begin utilizing the total energy of the blockchain {industry} and scale your initiatives effortlessly! 

Abstract – Finest Python API for Cryptocurrency 

On this tutorial, we taught you use the Python-compatible Web3 Knowledge API to drag cryptocurrency costs, balances, and different blockchain information. Extra particularly, we lined the next 5 examples: 

  1. Learn how to pull cryptocurrency costs
  2. Learn how to get historic ERC-20 token costs
  3. Learn how to question pockets token balances
  4. Learn how to get pockets token transfers
  5. Learn how to question ERC-20 metadata by contract

You probably have adopted alongside this far, you now know use a Python API for cryptocurrency improvement. From right here, it’s now as much as you to be inventive and begin constructing extra subtle initiatives utilizing the Web3 Knowledge API! 

Should you discovered this tutorial useful, you may also wish to try different improvement content material right here on the Web3 weblog. As an illustration, learn to create an ERC-721 NFT, discover the Arbitrum testnet, or get tokens utilizing an Arbitrum Goerli faucet. As well as, if you wish to take your Web3 improvement abilities to the subsequent degree, ensure to take a look at Moralis Academy. There, you could find programs geared toward novices and extra skilled Web3 devs. For instance, take the Ethereum Good Contract Programming 201 course – good for anybody who desires to develop a profession as a wise contract developer!

Keep in mind to enroll with Moralis proper now. Creating an account is solely free, and you can begin leveraging Moralis’ industry-leading APIs and the total energy of blockchain expertise in a heartbeat!



Source link

- Advertisement - spot_img

Latest stories

You might also like...