Retrieve prices from Binance API PowerShell

In this post blog, I will explain how to retrieve the price of any crypto with a function Get-Price from Binance platform using the API with PowerShell.

This is the first post blog of a series about how to query the Binance API. This article is intended for people who are not familiar when it comes to requesting an API. I didn’t find much information or resource in PowerShell, especially when authentification is needed. Therefore, I thought, there was an opportunity to share what I find and how to handle it.

Binance is a global cryptographic exchange platform that allows the exchange of hundreds of cryptos. If this series of blog posts are useful for you and you plan to create a Binance account, you can create your account using my Referral and benefit from 10% for trading fees 🙂

Let’s assume that you familiar with PowerShell and you have an idea of what an API is. If not, in a few words, the API contains Endpoints which allow you to interact with your Binance account. As a result, you can check the prices of any crypto, place an order to buy or cancel it at anytime and more. The Binance official API documentation is well documented. Have a look, it will give you an idea.

Working for an IT department in a mainly windows environment, I have choosed to use
PowerShell, far away the one I am the most familiarized with, event if not the most common one.

A clear decision was to only create functions in PowerShell, for every task I needed, to have a modular environment. Down below you can see, how to query the API by using the price ticker endpoint to retrieve the price from Binance.

Function Get-Price

function Get-Price{
    <#
		.SYNOPSIS
		Return the price of the symbol passed in paramater

        .PARAMETER Symbol
        Indicate the pair of which you wish to obtain the price
        
        .PARAMETER Decimal
        The number of decimal places in the price of the crypto

        .EXAMPLE
        Get-Price -Symbol CVCUSDT -Decimal 5

        .EXAMPLE
        $Tickers = Get-Price
	#>

    [cmdletbinding()]
    param(
        [Parameter(Mandatory=$false)] 
        [string]$Symbol,

        [Parameter(Mandatory=$false)]
        [ValidateSet(0,1,2,3,4,5,6,7,8)]
        [string]$Decimal = 3
    )

    BEGIN{
        if($PSBoundParameters.ContainsKey('Symbol')){
	        $URL = "https://api.binance.com/api/v3/ticker/price?symbol=" + $Symbol
        }
        else{
            $URL = "https://api.binance.com/api/v3/ticker/price"
        }
    }
    
    PROCESS{
	    $Ticker = curl $URL -Method get 
        $Ticker = $Ticker.Content | ConvertFrom-Json
    }

    END{
        if($PSBoundParameters.ContainsKey('Symbol')){
            return [math]::Round($Ticker.Price, $Decimal)
        }
        else{
            return $Ticker
        }
    }
}

About this function

  • EndPoint /api/v3/ticker/price
    An endpoint is simply an end of a communication channel. This one is part of the “Market Data endpoints” and in comparison with the “Account endpoints”, it doesn’t require any authentication.

  • $PSBoundParameters.ContainsKey(‘Symbol’)
    Check the value of the parameter Symbol

  • Curl is an alias of Invoke-WebRequest
    This is the command used to request an API. The $Ticker variable is an HtmlWebResponsObject which contains a property Content, and this content is in JSON format. This is why | ConvertFrom-Json is used.

  • [math]::Round($Ticker.Price, $Decimal)
    Round off to a decimal number according to the variable “number”.

Examples

Example Get-Price

Serie of post blog Crypto Binance API PowerShell

In the next post, we will see how to use the API with PowerShell to get information about the pairs with which we want to make transactions.

  1. Retrieve prices from Binance API PowerShell
  2. Get trading info from Binance API PowerShell
  3. Interact with Binance using API PowerShell
  4. New-Order API Binance PowerShell
  5. Retrieve Binance Balance API PowerShell
  6. Open Order Binance PowerShell API

4 thoughts on “Retrieve prices from Binance API PowerShell”

Leave a Comment

*

code