You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have two scripts written in powershell and basically one authenticates perfectly and responds with the markets and the other calling for balances is getting an authentication error using the same code call for the signature.
I created and API Key that was Read & Write for all three sections also.
If you would like to test the code it simply needs your own APIKey and APISecret for each script and send it.
####################### Script #1 #######################
# Set your API key and secret
$ApiKey = "<Your Key>"
$ApiSecret = '<Your Secret>'
# BTC Markets API Base URL
$BaseUrl = "https://api.btcmarkets.net"
# Function to create the API authentication signature
function Get-Signature {
param (
[string]$HttpMethod,
[string]$Path,
[string]$Timestamp,
[string]$ApiSecret
)
Write-Host " --- Executing Get-Signature ---"
$Payload = "$HttpMethod$Path$Timestamp"
$KeyBytes = [Convert]::FromBase64String($ApiSecret)
$PayloadBytes = [System.Text.Encoding]::UTF8.GetBytes($Payload)
$HashBytes = [System.Security.Cryptography.HMACSHA256]::New($KeyBytes).ComputeHash($PayloadBytes)
[Convert]::ToBase64String($HashBytes)
}
# Generate the signature
#$Signature = Get-Signature -HttpMethod $HttpMethod -Path $Path -Timestamp $Timestamp -ApiSecret $ApiSecret
# Function to fetch market data
function Get-Markets {
Write-Host "--- Executing Get-Markets ---"
$Timestamp = [int64]([DateTimeOffset]::UtcNow.ToUnixTimeMilliseconds())
$Path = "/v3/markets"
$HttpMethod = "GET"
# Generate the signature
$Signature = Get-Signature -HttpMethod $HttpMethod -Path $Path -Timestamp $Timestamp -ApiSecret $ApiSecret
# Headers
$Headers = @{
"BM-AUTH-APIKEY" = $ApiKey
"BM-AUTH-TIMESTAMP" = $Timestamp
"BM-AUTH-SIGNATURE" = $Signature
}
$Headers
# Send the request
$Response = Invoke-RestMethod -Uri "$BaseUrl$Path" -Method $HttpMethod -Headers $Headers
return $Response
}
# Execute the function
Get-Markets
####################### Script #2 #######################
# Set your API key and secret
$ApiKey = "<Your Key>"
$ApiSecret = '<Your Secret>'
# BTC Markets API Base URL
$BaseUrl = "https://api.btcmarkets.net"
# Function to create the API authentication signature
function Get-Signature {
param (
[string]$HttpMethod,
[string]$Path,
[string]$Timestamp,
[string]$ApiSecret
)
Write-Host " --- Executing Get-Signature ---"
$Payload = "$HttpMethod$Path$Timestamp"
$KeyBytes = [Convert]::FromBase64String($ApiSecret)
$PayloadBytes = [System.Text.Encoding]::UTF8.GetBytes($Payload)
$HashBytes = [System.Security.Cryptography.HMACSHA256]::New($KeyBytes).ComputeHash($PayloadBytes)
[Convert]::ToBase64String($HashBytes)
}
# Generate the signature
#$Signature = Get-Signature -HttpMethod $HttpMethod -Path $Path -Timestamp $Timestamp -ApiSecret $ApiSecret
# Function to fetch account balances
function Get-Balances {
Write-Host "--- Executing Get Balances ---"
$Timestamp = [int64]([DateTimeOffset]::UtcNow.ToUnixTimeMilliseconds())
$Path = "/v3/accounts/me/balances"
$HttpMethod = "GET"
# Generate the signature
$Signature = Get-Signature -HttpMethod $HttpMethod -Path $Path -Timestamp $Timestamp -ApiSecret $ApiSecret
# Headers
$Headers = @{
"BM-AUTH-APIKEY" = $ApiKey
"BM-AUTH-TIMESTAMP" = $Timestamp
"BM-AUTH-SIGNATURE" = $Signature
}
$Headers # Outing headers
# Send the request
try {
$Response = Invoke-RestMethod -Uri "$BaseUrl$Path" -Method $HttpMethod -Headers $Headers -Verbose
return $Response
} catch {
Write-Error "Failed to fetch balances: $_"
}
}
# Execute the function
Get-Balances
The text was updated successfully, but these errors were encountered:
We cover sample code for many programming languages and to further debug your application please run any of the samples and try to reproduce the same output in PowerShell (please make sure to use the same timestamps in the input).
In general, there are not many API customers who use PowerShell for trading applications hence we have not invested in creating an official sample/demo code.
If you are keen to make PowerShell works and are interested to share your integration with other community members, we are happy to link your open source sample PowerShell project in our API docs so other people can use.
If possible, we encourage you to consider using Python or other languages that provide more flexibility and perhaps are more mainstream for this sort of integration. Choosing those languages (e.g. creating a Python app and then run it via command line) has the benefit of having larger support community and perhaps easier for integration (e.g. if you decide to use WebSocket then it will be perhaps even more difficult in PowerShell).
We will be adding a sample in Rust language very soon and perhaps that may also help also.
I have two scripts written in powershell and basically one authenticates perfectly and responds with the markets and the other calling for balances is getting an authentication error using the same code call for the signature.
I created and API Key that was Read & Write for all three sections also.
If you would like to test the code it simply needs your own APIKey and APISecret for each script and send it.
The text was updated successfully, but these errors were encountered: