Installing pulsar-sdk as a dependency
Python
Before installing, make sure your environment is running Python >=3.10
To start, open up your favorite terminal and install the Pulsar Python SDK using the following command:
pip install pulsar-sdk-py
Or optionally, if you use poetry:
poetry add pulsar-sdk-py
Once the dependency is installed, you can start integrating it into your code.
NodeJS
To start, open up your favorite terminal and install the Pulsar NodeJS SDK using the following command:
npm i pulsar_sdk_js
Once the dependency is installed, you can start integrating it into your code.
Using the SDK
To start, you should instantiate the class using your private API key you can fetch from our dashboard.
import PulsarSDK from 'pulsar_sdk_js'
const API_KEY = 'ENTER YOUR API KEY HERE'
const sdk = new PulsarSDK(API_KEY)
from pulsar_sdk_py import PulsarSDK
API_KEY = "ENTER YOUR API KEY HERE"
sdk = PulsarSDK(API_KEY)
Once you've initialized your SDK object with your API key, you're ready to begin calling its functions. The PulsarSDK class automatically handles the details of making requests and parsing responses, so you can easily manipulate the resulting objects and access their attributes. This abstraction layer shields you from having to worry about the intricacies of the API response format.
For instance, let's say you wanted to retrieve a paginated list of tokens from a specific blockchain. With PulsarSDK, this can be done with a single method call, which takes care of all the necessary API requests and response parsing.
import PulsarSDK, { ChainKeys, ListTokensOptions } from 'pulsar_sdk_js'
const sdk = new PulsarSDK(API_KEY)
const options: ListTokensOptions = {
chains: [ChainKeys.TERRA2],
}
sdk.tokens.list_tokens(options).then((tokens) => {
for (let token in tokens) {
console.log(token)
}
})
from pulsar_sdk_py import PulsarSDK
from pulsar_sdk_py.enums import ChainKeys
sdk = PulsarSDK(API_KEY)
tokens = sdk.tokens.list_tokens(chains=[ChainKeys.TERRA])
for token in tokens.response:
print(token)
When calling a Token endpoint, the SDK accesses the tokens attribute. Once the request is made, the SDK parses the response and returns a PaginatedTokenWithStats
object. For more information on the schemas used in our SDKs, please refer to our subpage.
If an error occurs, the SDK will throw a HTTPError
exception. To avoid failure of your code, it is recommended that you error handle your function calls.
When fetching balances of a wallet, you will have to utilize an async generator. This is different from other method calls, as the balances will be returned as they are being fetched from WebSocket endpoints. The objects will be automatically parsed, so all you need to do is call the functions inside an async for loop to save the objects to a list or use them in other ways.
For instance, to get the balances of a TERRA 2 wallet and save them to a list, you can use the following function call:
import PulsarSDK, { ChainKeys } from 'pulsar_sdk_js'
const chain = ChainKeys.ETHEREUM
const responses_list: any[] = []
const wallet_addr = 'YOUR WALLET ADDR'
const API_KEY =
'YOUR API KEY'
const sdk = new PulsarSDK(API_KEY)
async function getWalletBalances() {
const balances = sdk.balances.getWalletBalances(wallet_addr, chain)
for await (const balance of balances) {
responses_list.push(balance)
}
}
getWalletBalances().then(() => {
console.log(responses_list)
})
import asyncio
from pulsar_sdk_py import PulsarSDK
from pulsar_sdk_py.enums import ChainKeys
sdk = PulsarSDK(api_key=API_KEY)
response_list = []
async def main():
global response_list
async for response in sdk.balances.get_wallet_balances("YOUR WALLET ADDRESS", chain=ChainKeys.TERRA2):
response_list.append(response)
asyncio.run(main())
Wrap this inside an async
function, await
it, and you'll get all the balances that wallet holds, both tokens, protocol positions and NFTs.
The balance
functions also raise Exceptions if they encounter errors. These include:
WrongResponseFormat
when the SDK receives an answer from the WebSocket endpoint in a format it isn't expecting.WebSocketClosed
when the connection to the WebSocket endpoint is closed unexpectadly. This might happen if you have run out of credits or your API key is invalid.
In the case of the Python SDK, we additionally have:SerializationError
when an error occurs while parsing the responses the endpoint returns.
As our SDK progresses and matures, we will introduce new exceptions and better ways for you to understand any encountered issues. Please refer to this page if you come across an unfamiliar exception to stay informed about its details and how to handle it.