How to Get Ethereum Price Information Using Safepub API
As part of its effort to make its services more accessible and user-friendly, the Ethereum network provides an official API (Application Programming Interface) that allows developers to access various information about the blockchain. In this article, we will explore how to use the Safepub API to retrieve price information for different assets stored in vaults.
Safepub API Overview
The Safepub API is part of the Ethereum network’s decentralized data storage and retrieval system. It provides a secure way to store and retrieve sensitive information about users, vaults, and assets on the blockchain. The Safepub API offers endpoints for various types of data, including balances, transaction records, and more.
Balances Endpoint
The endpoint we will focus on is /api/v1/safes/{SAFE_ADDRESS}/balances/usd
, which provides a fiat balance and conversion rate for each asset stored in a vault. Below is a breakdown of this endpoint:
SAFE_ADDRESS
: This specifies the address of the vault where you want to retrieve data about the assets.
usd
: The endpoint returns information about balances in USD (United States dollars).
Retrieving Price Information Using the Safepub API
To get information about the price of an asset, you will need to make a GET request to the /api/v1/safes/{SAFE_ADDRESS}/balances/usd
endpoint. Here is an example of how to do this in Python:
`python
import requests
def get_safe_balance(safe_address):
url = f"
response = requests. get(url)
if response. status_code == 200:
data = response. json()
return data
else:
print(f"Error: {response. text}")
return None
Replace YOUR_PROJECT_ID with your actual Infura project ID.
safe_address = "0x...your_safe_address_here"
balance_data = get_safe_balance(safe_address)
If balance_data is not None:
for item in balance_data["items"]:
asset_name = item["asset"]
price = float(item["quantity"])
Convert to float
print(f"Asset: {asset_name}, Price (USD): ${price:.2f}")
`
Note: This example assumes you have an Infura project ID and the necessary credentials set up. You'll need to replaceYOUR_PROJECT_IDwith your actual project ID.
In this code, we use therequests.get()` method to send a GET request to the Safepub API endpoint. We then parse the JSON response and print the pricing information for each asset stored in the vault.
Conclusion
The Safepub API provides an efficient way to retrieve price information for Ethereum assets stored in vaults. By following these steps, you will be able to easily access data about your vaults and make informed decisions based on market trends or other factors affecting their value.