How could I listen to the coin transfer record to my Binance account

I want to set up one listener to listen to the coin transfer record. For example, when one person A transferred 3 ICP to my account, I could know this record. Who transfer how many coins to me !
Is there any stream listener for it ? I checked the document and seems not found it. In user stream listener, it could listen to the account change but could not know who / how may coins to me.

Not sure if below is the solution you are happy with, but at this time, this only i can suggest!

Currently, Binance’s API does not provide a direct method to identify who transferred coins to your account or the exact number of coins transferred by each individual. The available User Data Streams can notify you about changes in your account balances, including deposits and withdrawals, but they don’t specify the sender of the funds.

To achieve the functionality you desire, you can set up a simple system to monitor changes in your account balances using the User Data Stream provided by Binance. Here’s a basic approach using Python:

  1. Create and Maintain Initial Balances List: When you start your application, fetch and store the current balances of all coins in your account.

  2. Set Up a Listener: Using the Binance API, create a listener on your account using the listenKey to receive updates about any changes in your account balances.
    User Data Stream | Binance Open Platform

  3. Compare Balances: Whenever you receive an update through the listener, compare the new balances with your stored balances. If there’s a change, this could indicate a deposit or withdrawal.

  4. Calculate Changes and Output: If there’s an increase in balance, calculate the change and convert the added amount to BTC and USDT to understand the value change. Print this information in a formatted manner.

Here’s a simplified version of how you might set this up in code in python:

import asyncio
from binance import AsyncClient, BinanceSocketManager

async def main():
client = await AsyncClient.create()
bm = BinanceSocketManager(client)

# Fetch and store initial balances
initial_balances = {asset['asset']: float(asset['free']) for asset in await client.get_account()['balances']}

async with bm.user_socket() as stream:
    while True:
        res = await stream.recv()
        if res['e'] == 'balanceUpdate':
            asset = res['a']
            delta = float(res['d'])
            new_balance = initial_balances[asset] + delta
            initial_balances[asset] = new_balance  # Update stored balance
            
            # Calculate value in BTC and USDT (simplified, assumes APIs for conversion)
            value_in_btc = convert_to_btc(asset, delta)
            value_in_usdt = convert_to_usdt(asset, delta)
            
            print(f"Received {delta} {asset}, now worth {value_in_btc} BTC or {value_in_usdt} USDT.")

Placeholder functions for conversion, implement according to actual availability

def convert_to_btc(asset, amount):
# Implementation needed
return 0

def convert_to_usdt(asset, amount):
# Implementation needed
return 0

if name == “main”:
loop = asyncio.get_event_loop()
loop.run_until_complete(main())

This code uses asynchronous programming to listen for balance updates and then calculates and prints changes in your account. Remember, you would need to handle the actual conversion calculations based on current market values, which could involve additional API calls.

Hi Kumar,

Thanks a lot for your quick answer and the example. Our goal is to know who has transferred how much coins to our account not only the account changes, is it possible to know it ?

Thanks.
Hui

Hi Hui,

Currently, Binance’s API does not provide a direct method to identify who transferred coins to your account or the exact number of coins transferred by each individual.

Hi Kumar,

I noticed here there is one API. Can I use this one ?
https://binance-docs.github.io/apidocs/spot/en/#pay-endpoints

Yes, if you are thinking to use the listen key and rest API in combination then you can get realt time update in the following manner.

Use the listenKey to monitor real-time updates in your Binance account and then triggering the REST API Get Pay Trade History upon detecting changes is a feasible approach. This strategy allows you to combine the immediacy of WebSocket updates with the detailed transaction information provided by the REST API. Here’s a detailed Python code example that implements this strategy:

Requirements

python-binance library: Install it using pip install python-binance.
API Key and Secret: Ensure you have your Binance API Key and Secret configured securely.

import asyncio
import hmac
import hashlib
from urllib.parse import urlencode
import time
import requests
from binance import AsyncClient, BinanceSocketManager

Your API keys

API_KEY = ‘your_api_key’
API_SECRET = ‘your_secret_key’

async def handle_socket_message(msg):
if msg[‘e’] == ‘outboundAccountPosition’:
# Detected a change in account position
print(“Change in account detected, fetching transaction history…”)
get_recent_transactions()

def get_recent_transactions():
“”“Fetch recent transactions using the Pay Trade History API”“”
endpoint = “https://api.binance.com/sapi/v1/pay/transactions
params = {
‘timestamp’: int(time.time() * 1000),
‘limit’: 1 # Fetch only the most recent transaction
}
query_string = urlencode(params)
signature = hmac.new(API_SECRET.encode(), query_string.encode(), hashlib.sha256).hexdigest()
params[‘signature’] = signature
response = requests.get(endpoint, params=params, headers={‘X-MBX-APIKEY’: API_KEY})
data = response.json()
print(“Recent Transaction Data:”, data)

async def main():
client = await AsyncClient.create(API_KEY, API_SECRET)
bm = BinanceSocketManager(client)
# Start user data stream
stream = bm.user_socket()
async with stream as socket:
while True:
res = await socket.recv()
handle_socket_message(res)

if name == “main”:
loop = asyncio.get_event_loop()
loop.run_until_complete(main())

Websocket Connection: The script starts a WebSocket connection using the listenKey to receive real-time updates about your Binance account.

Handling Messages: Whenever a relevant event (outboundAccountPosition) indicating a change in account balances is received, the script triggers a function to fetch recent transaction details.

Fetching Transactions: The get_recent_transactions function makes a REST API call to Get Pay Trade History, fetching the details of the most recent transaction based on the change detected.

This setup ensures you get real-time notifications via WebSocket and detailed transaction information via REST API, leveraging both technologies effectively.

Hi Kumar,

Thanks a lot for the solution, Let me try.
By the way , do you have nodejs code example ?
Another question, for this rest api: Binance API Documentation, it said its response will differs according to different orderType.
In our case, we care about who transfered money to our account. Do you know which orderType I should check ?
Is it C2C ? I don’t need to care about other orderTypes, right ?

Node.js Code Example

A similar solution in Node.js, here’s a basic example using axios for HTTP requests and ws for WebSocket communication:

const axios = require(‘axios’);
const crypto = require(‘crypto’);
const WebSocket = require(‘ws’);

// Your Binance API keys
const API_KEY = ‘your_api_key’;
const API_SECRET = ‘your_secret_key’;

function getRecentTransactions() {
const endpoint = ‘https://api.binance.com/sapi/v1/pay/transactions’;
const timestamp = Date.now();
const params = timestamp=${timestamp}&limit=1;
const signature = crypto.createHmac(‘sha256’, API_SECRET)
.update(params)
.digest(‘hex’);
const url = ${endpoint}?${params}&signature=${signature};

axios.get(url, { headers: { 'X-MBX-APIKEY': API_KEY } })
    .then(response => {
        const transaction = response.data[0];
        const sender = transaction.sender || 'Unknown';
        const amount = transaction.amount || 'Unknown';
        const asset = transaction.asset || 'Unknown';

        console.log(`Transaction detected: ${amount} ${asset} transferred by ${sender}.`);
    })
    .catch(error => console.error('Error fetching transactions:', error));

}

const ws = new WebSocket(‘wss://stream.binance.com:9443/ws/YOUR_LISTEN_KEY’);

ws.on(‘message’, (data) => {
const message = JSON.parse(data);
if (message.e === ‘outboundAccountPosition’) {
console.log(‘Change in account detected, fetching transaction history…’);
getRecentTransactions();
}
});

ws.on(‘open’, () => {
console.log(‘WebSocket connection opened.’);
});

ws.on(‘close’, () => {
console.log(‘WebSocket connection closed.’);
});

Relevant orderType for Transfers

For tracking who transferred money to your account, the most relevant orderType would be C2C (Customer to Customer). This is commonly used for peer-to-peer transactions, where one user transfers funds directly to another. You can focus on this orderType when filtering transactions.

In this case, when you fetch the transaction details, look for transactions where the orderType is C2C, as this will likely represent the transfers you’re interested in.

Many developers share their Binance API integrations on GitHub, where you can find working examples of WebSocket and REST API usage in Node.js. A commonly used library for interacting with Binance’s API in Node.js is node-binance-api.

This provides examples of how to connect to WebSocket streams and make API calls. You can adapt these examples to fit the specific use case of monitoring transactions and fetching details.

Example from a Reliable Source

const Binance = require(‘node-binance-api’);
const binance = new Binance().options({
APIKEY: ‘your_api_key’,
APISECRET: ‘your_secret_key’
});

binance.websockets.userData(balance_update => {
console.log("Balance Update: ", balance_update);
getRecentTransactions(); // Call your function to get recent transactions
}, execution_update => {
console.log("Execution Update: ", execution_update);
});

function getRecentTransactions() {
binance.sapiRequest(‘v1/pay/transactions’, { timestamp: Date.now(), limit: 1 }, (error, response) => {
if (error) return console.error(error);
console.log("Recent Transaction: ", response);
});
}

This example uses the node-binance-api package to connect to the WebSocket stream and listen for user data events, then it triggers a REST API call to fetch recent transactions. The structure is similar to what was provided earlier, but here, the node-binance-api package simplifies some of the setup.

Additional References

npm package: node-binance-api on npm: https://www.npmjs.com/package/node-binance-api
This provides the official package that can be used for more straightforward interactions with the Binance API in Node.js.

This information and the examples provided should help you implement a reliable solution. Do verify everything by testing the code in your specific environment.

Thank you so much ~ ~
Let me try !

Hi Kumar,

I tried with the following code and found I failed to receive the balance update message.
Do you know what could be missing ? Permission issue or other reason I may miss ?

========================================

import Binance from “node-binance-api”;

export const handler = async () => {
console.log(“test start3>>”)

try{

const binance = new Binance().options({
    APIKEY: "api key of my account ",
    APISECRET: "secret key of my account "
});   

binance.websockets.userData(balance_update => {
    console.log("Balance Update: ", balance_update);
    transactions = getRecentTransactions(binance); // Call your function to get recent transactions        
}, execution_update => {
    console.log("Execution Update: ", execution_update);
});

}
catch(error) {
console.error("get error : "+ error)
}

}

async function getRecentTransactions(binance) {

await binance.sapiRequest('v1/pay/transactions', { timestamp: Date.now(), limit: 1 }, (error, response) => {
    if (error) return console.error(error);
    console.log("Recent Transaction: ", response);
    return response.data;
    // parseTransactionResponse(response);
});

}

console.log(“test start1>>”)
handler();
console.log(“test start2>>”)

Another question is about the following API (binance.sapiRequest), When I run it, it will report the error:

TypeError: binance.sapiRequest is not a function
at getRecentTransactions (file:///D:/index.mjs:43:19)

I put the last two questions in the following new issue:

Hi, I think we have got multiple issues here… let me try these one by one…

  1. failed to receive the balance update message

Check API keys used have the correct permissions enabled on Binance. Specifically, permissions for reading balance updates and other account-related information must be enabled.
Ensure WebSocket connection remains open and is not being closed unexpectedly. Implementing reconnection logic might be necessary if the connection drops.
Code seems ok to me, issue might come from permission settings on the Binance API or an error in how the WebSocket connection is managed in the code. Can we do more error check on confirmation on this…

  1. binance.sapiRequest is not a function when trying to fetch transaction details

Method might not exist in the version of node-binance-api or might be incorrectly referenced. I will have to check a bit more on this…

Would you do a quick check on the latest documentation of node-binance-api to ensure the method is still supported… GitHub - jaggedsoft/node-binance-api: Node Binance API is an asynchronous node.js library for the Binance API designed to be easy to use.

Just make sure that for filtering by orderType, you should specifically check for ‘C2C’ transactions in the fetched data, which represent Customer-to-Customer transfers, as these are directly relevant to transfers to/from your account.

// Assuming response.data is an array of transactions
const c2cTransactions = response.data.filter(tx => tx.orderType === ‘C2C’);
console.log("C2C Transactions: ", c2cTransactions);

  1. failed to receive the balance update message,
    I checked the permission of API, I can set API read is allowed. Other permissions do not allow me to edit.
    For web socket connection, I think it is managed in “node-binance-api”. I just invoked that method: binance.websockets.userData and did not set anything.

  1. binance.sapiRequest is not a function when trying to fetch transaction details
    I checked this doc.
    In this file: node-binance-api/node-binance-api.d.ts at master · jaggedsoft/node-binance-api · GitHub
    I only find publicRequest/signedRequest/promiseRequest.

I tried to use publicRequest

let sapi = ‘https://api.binance.com/sapi/’;

await binance.publicRequest(sapi + 'v1/pay/transactions', { timestamp: Date.now(), limit: 1 }, (error, response) => {
    if (error) return console.error(error);
    console.log("Recent Transaction: ", response);
    return response.data;
    // parseTransactionResponse(response);
});

And running it will get the following error:

get error : ReferenceError: transactions is not defined
TypeError [ERR_INVALID_ARG_VALUE]: The property ‘options.family’ must be one of: 0, 4, 6. Received false
at lookup (node:dns:179:11)
at emitLookup (node:net:1405:5)
at defaultTriggerAsyncIdScope (node:internal/async_hooks:464:18)
at lookupAndConnectMultiple (node:net:1404:3)
at node:net:1350:7
at defaultTriggerAsyncIdScope (node:internal/async_hooks:464:18)
at lookupAndConnect (node:net:1349:5)
at Socket.connect (node:net:1246:5)
at Object.connect (node:_tls_wrap:1786:13)
at Agent.createConnection (node:https:170:22) {
code: ‘ERR_INVALID_ARG_VALUE’
}

I found something and suggesting to try, more of based on undertsanding and doing hit and trial method…To correct the issues with the Binance API in Node.js, particularly around using the binance.sapiRequest which doesn’t exist in the node-binance-api package, here’s an updated approach using the available methods from the library, try this and let me knwo…

const Binance = require(‘node-binance-api’);
const binance = new Binance().options({
APIKEY: ‘your_api_key’,
APISECRET: ‘your_secret_key’
});

// Function to get recent transactions
async function getRecentTransactions() {
try {
const params = {
timestamp: Date.now(),
limit: 1
};
// Using publicRequest as sapiRequest is not available
const response = await binance.publicRequest(‘https://api.binance.com/sapi/v1/pay/transactions’, params);
console.log("Recent Transaction: ", response);
} catch (error) {
console.error("Error in fetching transactions: ", error);
}
}

// Function to handle incoming WebSocket messages
function handleWebSocketMessage(data) {
const message = JSON.parse(data);
if (message.e === ‘outboundAccountPosition’) {
console.log(‘Change in account detected, fetching transaction history…’);
getRecentTransactions();
}
}

// Set up WebSocket connection
const ws = new WebSocket(‘wss://stream.binance.com:9443/ws/YOUR_LISTEN_KEY’);
ws.on(‘message’, handleWebSocketMessage);
ws.on(‘open’, () => {
console.log(‘WebSocket connection opened.’);
});
ws.on(‘close’, () => {
console.log(‘WebSocket connection closed.’);
});

Modus OPerandi:
The function getRecentTransactions uses publicRequest method because sapiRequest is not a function in node-binance-api. The URL and parameters are directly passed to publicRequest.
A WebSocket connection is set up to listen for changes in account positions.
When a message indicates a change in account position, it triggers fetching recent transactions.

For using the node-binance-api, the library offers a range of functionalities including fetching current prices, account balances, and interacting with WebSocket streams for real-time updates. You can make both authenticated and public API calls using methods like binance.prices() for getting the latest market prices or binance.balance() for retrieving your account balances.

When interacting with the Binance API, specifically the SAPi endpoints for transactions like sapi/v1/pay/transactions, you can utilize binance.publicRequest or binance.signedRequest to make the required API calls. These methods allow for interaction with the Binance server by providing necessary parameters such as the API key, secret, and additional request parameters.

For more details on the node.js library functions try the below:
Node Binance API: https://www.npmjs.com/package/node-binance-api

Hi Kumar,

I tried your example in my local PC and it still failed to receive the message after I transferred the money.
I could only see log “WebSocket connection opened”.
I guess I need to generate one listen key to replace the one in ws URL ?
const ws = new WebSocket(‘wss://stream.binance.com:9443/ws/YOUR_LISTEN_KEY’);

For that publicRequest, I tried seperately with the following code,

  1. first run, it raised error: Error in fetching transactions: TypeError [ERR_INVALID_ARG_VALUE]: The property ‘options.family’ must be one of: 0, 4, 6. Received false

To fix it, I added parameter “family:0” in options.

  1. second run, it still raised error: body: ‘{“code”:-2014,“msg”:“API-key format invalid.”}’
    In the error log, I could see X-MBX-APIKEY, not sure if it is the reason.

headers: {
‘User-Agent’: ‘Mozilla/4.0 (compatible; Node Binance API)’,
‘Content-type’: ‘application/x-www-form-urlencoded’,
‘X-MBX-APIKEY’: ‘’
},

========example code======================
import Binance from “node-binance-api”;
import WebSocket from “ws”;

const binance = new Binance().options({
APIKEY: ‘xxxx’,
APISECRET: ‘xxxx’,
family: 0
});

getRecentTransactions()

// Function to get recent transactions
async function getRecentTransactions() {
try {
const params = {
timestamp: Date.now(),
limit: 1
};
// Using publicRequest as sapiRequest is not available
const response = await binance.publicRequest(“https://api.binance.com/sapi/v1/pay/transactions”, params);
console.log("Recent Transaction: ", response);
} catch (error) {
console.error("Error in fetching transactions: ", error);
}
}