Skip to content

Instantly share code, notes, and snippets.

@ifthenelse
Last active January 30, 2024 14:02
Show Gist options
  • Save ifthenelse/0783974546862d6eb28f2bfaae069e7f to your computer and use it in GitHub Desktop.
Save ifthenelse/0783974546862d6eb28f2bfaae069e7f to your computer and use it in GitHub Desktop.
Small script that uses jq to check the success of an authenticated request to Binance API key with the API key and Secret used in a specific Freqtrade configuration file. Usage ./check_binance_authenticated_api_request.zsh path/to/your-ftbot-config-file.json
#!/bin/zsh
# Check if jq is installed
if ! command -v jq &> /dev/null; then
echo "Error: jq is not installed. Please install it before continuing."
exit 1
fi
# Check if a configuration file name is provided
if [[ -z $1 ]]; then
echo "Error: Please provide the JSON configuration file name as a parameter."
exit 1
fi
# Read API key and API secret from the specified configuration file
api_key=$(jq -r '.exchange.key' "$1")
api_secret=$(jq -r '.exchange.secret' "$1")
# Check if API key and API secret are present
if [[ -z $api_key || -z $api_secret ]]; then
echo "Error: API key or API secret missing in the configuration file."
exit 1
fi
# Define the API endpoint
api_url="https://api.binance.com/api/v3/account"
# Current timestamp in milliseconds
timestamp=$(($(date +%s%N) / 1000000))
# Concatenate API key and timestamp
query_string="timestamp=$timestamp"
# Calculate HMAC signature using openssl
signature=$(echo -n "$query_string" | openssl dgst -sha256 -hmac "$api_secret" | cut -d' ' -f2)
# Make the request to Binance authenticated API using curl
curl -H "X-MBX-APIKEY: $api_key" \
-X GET \
"$api_url?$query_string&signature=$signature"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment