Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions binance/async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from typing import Any, Dict, List, Optional, Union
from urllib.parse import urlencode, quote
import time
import warnings
import aiohttp
import yarl

Expand Down Expand Up @@ -1417,6 +1418,14 @@ async def get_open_margin_oco_orders(self, **params):
get_open_margin_oco_orders.__doc__ = Client.get_open_margin_oco_orders.__doc__

async def margin_stream_get_listen_key(self):
warnings.warn(
"POST /sapi/v1/userDataStream is deprecated and will be removed on 2026-02-20. "
"Use the WebSocket API subscription method instead (create listenToken via POST /sapi/v1/userListenToken, "
"then subscribe with userDataStream.subscribe.listenToken). "
"The margin_socket() method now uses WebSocket API by default.",
DeprecationWarning,
stacklevel=2
)
res = await self._request_margin_api(
"post", "userDataStream", signed=False, data={}
)
Expand All @@ -1425,6 +1434,14 @@ async def margin_stream_get_listen_key(self):
margin_stream_get_listen_key.__doc__ = Client.margin_stream_get_listen_key.__doc__

async def margin_stream_keepalive(self, listenKey):
warnings.warn(
"PUT /sapi/v1/userDataStream is deprecated and will be removed on 2026-02-20. "
"Use the WebSocket API subscription method instead (create listenToken via POST /sapi/v1/userListenToken, "
"then subscribe with userDataStream.subscribe.listenToken). "
"The margin_socket() method now uses WebSocket API by default.",
DeprecationWarning,
stacklevel=2
)
params = {"listenKey": listenKey}
return await self._request_margin_api(
"put", "userDataStream", signed=False, data=params
Expand All @@ -1433,16 +1450,65 @@ async def margin_stream_keepalive(self, listenKey):
margin_stream_keepalive.__doc__ = Client.margin_stream_keepalive.__doc__

async def margin_stream_close(self, listenKey):
warnings.warn(
"DELETE /sapi/v1/userDataStream is deprecated and will be removed on 2026-02-20. "
"Use the WebSocket API subscription method instead (create listenToken via POST /sapi/v1/userListenToken, "
"then subscribe with userDataStream.subscribe.listenToken). "
"The margin_socket() method now uses WebSocket API by default.",
DeprecationWarning,
stacklevel=2
)
params = {"listenKey": listenKey}
return await self._request_margin_api(
"delete", "userDataStream", signed=False, data=params
)

async def margin_create_listen_token(self, symbol: Optional[str] = None, is_isolated: bool = False, validity: Optional[int] = None):
"""Create a listenToken for margin account user data stream

https://developers.binance.com/docs/margin_trading/trade-data-stream/Create-Margin-Account-listenToken

:param symbol: Trading pair symbol (required when is_isolated=True)
:type symbol: str
:param is_isolated: Whether it is isolated margin (default: False for cross-margin)
:type is_isolated: bool
:param validity: Validity in milliseconds (default: 24 hours, max: 24 hours)
:type validity: int
:returns: API response with token and expirationTime

.. code-block:: python

{
"token": "6xXxePXwZRjVSHKhzUCCGnmN3fkvMTXru+pYJS8RwijXk9Vcyr3rkwfVOTcP2OkONqciYA",
"expirationTime": 1758792204196
}
"""
params = {}
if is_isolated:
if not symbol:
raise ValueError("symbol is required when is_isolated=True")
params["symbol"] = symbol
params["isIsolated"] = "true"
if validity is not None:
params["validity"] = validity

return await self._request_margin_api(
"post", "userListenToken", signed=True, data=params
)

# Isolated margin

margin_stream_close.__doc__ = Client.margin_stream_close.__doc__

async def isolated_margin_stream_get_listen_key(self, symbol):
warnings.warn(
"POST /sapi/v1/userDataStream/isolated is deprecated and will be removed on 2026-02-20. "
"Use the WebSocket API subscription method instead (create listenToken via POST /sapi/v1/userListenToken "
"with isIsolated=true, then subscribe with userDataStream.subscribe.listenToken). "
"The isolated_margin_socket() method now uses WebSocket API by default.",
DeprecationWarning,
stacklevel=2
)
params = {"symbol": symbol}
res = await self._request_margin_api(
"post", "userDataStream/isolated", signed=False, data=params
Expand All @@ -1452,6 +1518,14 @@ async def isolated_margin_stream_get_listen_key(self, symbol):
isolated_margin_stream_get_listen_key.__doc__ = Client.isolated_margin_stream_get_listen_key.__doc__

async def isolated_margin_stream_keepalive(self, symbol, listenKey):
warnings.warn(
"PUT /sapi/v1/userDataStream/isolated is deprecated and will be removed on 2026-02-20. "
"Use the WebSocket API subscription method instead (create listenToken via POST /sapi/v1/userListenToken "
"with isIsolated=true, then subscribe with userDataStream.subscribe.listenToken). "
"The isolated_margin_socket() method now uses WebSocket API by default.",
DeprecationWarning,
stacklevel=2
)
params = {"symbol": symbol, "listenKey": listenKey}
return await self._request_margin_api(
"put", "userDataStream/isolated", signed=False, data=params
Expand All @@ -1460,6 +1534,14 @@ async def isolated_margin_stream_keepalive(self, symbol, listenKey):
isolated_margin_stream_keepalive.__doc__ = Client.isolated_margin_stream_keepalive.__doc__

async def isolated_margin_stream_close(self, symbol, listenKey):
warnings.warn(
"DELETE /sapi/v1/userDataStream/isolated is deprecated and will be removed on 2026-02-20. "
"Use the WebSocket API subscription method instead (create listenToken via POST /sapi/v1/userListenToken "
"with isIsolated=true, then subscribe with userDataStream.subscribe.listenToken). "
"The isolated_margin_socket() method now uses WebSocket API by default.",
DeprecationWarning,
stacklevel=2
)
params = {"symbol": symbol, "listenKey": listenKey}
return await self._request_margin_api(
"delete", "userDataStream/isolated", signed=False, data=params
Expand Down
84 changes: 84 additions & 0 deletions binance/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import requests
import time
import warnings
from urllib.parse import urlencode, quote

from .base_client import BaseClient
Expand Down Expand Up @@ -5667,6 +5668,14 @@ def margin_stream_get_listen_key(self):
:raises: BinanceRequestException, BinanceAPIException

"""
warnings.warn(
"POST /sapi/v1/userDataStream is deprecated and will be removed on 2026-02-20. "
"Use the WebSocket API subscription method instead (create listenToken via POST /sapi/v1/userListenToken, "
"then subscribe with userDataStream.subscribe.listenToken). "
"The margin_socket() method now uses WebSocket API by default.",
DeprecationWarning,
stacklevel=2
)
res = self._request_margin_api("post", "userDataStream", signed=False, data={})
return res["listenKey"]

Expand All @@ -5687,6 +5696,14 @@ def margin_stream_keepalive(self, listenKey):
:raises: BinanceRequestException, BinanceAPIException

"""
warnings.warn(
"PUT /sapi/v1/userDataStream is deprecated and will be removed on 2026-02-20. "
"Use the WebSocket API subscription method instead (create listenToken via POST /sapi/v1/userListenToken, "
"then subscribe with userDataStream.subscribe.listenToken). "
"The margin_socket() method now uses WebSocket API by default.",
DeprecationWarning,
stacklevel=2
)
params = {"listenKey": listenKey}
return self._request_margin_api(
"put", "userDataStream", signed=False, data=params
Expand All @@ -5709,11 +5726,54 @@ def margin_stream_close(self, listenKey):
:raises: BinanceRequestException, BinanceAPIException

"""
warnings.warn(
"DELETE /sapi/v1/userDataStream is deprecated and will be removed on 2026-02-20. "
"Use the WebSocket API subscription method instead (create listenToken via POST /sapi/v1/userListenToken, "
"then subscribe with userDataStream.subscribe.listenToken). "
"The margin_socket() method now uses WebSocket API by default.",
DeprecationWarning,
stacklevel=2
)
params = {"listenKey": listenKey}
return self._request_margin_api(
"delete", "userDataStream", signed=False, data=params
)

def margin_create_listen_token(self, symbol: Optional[str] = None, is_isolated: bool = False, validity: Optional[int] = None):
"""Create a listenToken for margin account user data stream

https://developers.binance.com/docs/margin_trading/trade-data-stream/Create-Margin-Account-listenToken

:param symbol: Trading pair symbol (required when is_isolated=True)
:type symbol: str
:param is_isolated: Whether it is isolated margin (default: False for cross-margin)
:type is_isolated: bool
:param validity: Validity in milliseconds (default: 24 hours, max: 24 hours)
:type validity: int
:returns: API response with token and expirationTime

.. code-block:: python

{
"token": "6xXxePXwZRjVSHKhzUCCGnmN3fkvMTXru+pYJS8RwijXk9Vcyr3rkwfVOTcP2OkONqciYA",
"expirationTime": 1758792204196
}

:raises: BinanceRequestException, BinanceAPIException
"""
params = {}
if is_isolated:
if not symbol:
raise ValueError("symbol is required when is_isolated=True")
params["symbol"] = symbol
params["isIsolated"] = "true"
if validity is not None:
params["validity"] = validity

return self._request_margin_api(
"post", "userListenToken", signed=True, data=params
)

# Isolated margin

def isolated_margin_stream_get_listen_key(self, symbol):
Expand All @@ -5739,6 +5799,14 @@ def isolated_margin_stream_get_listen_key(self, symbol):
:raises: BinanceRequestException, BinanceAPIException

"""
warnings.warn(
"POST /sapi/v1/userDataStream/isolated is deprecated and will be removed on 2026-02-20. "
"Use the WebSocket API subscription method instead (create listenToken via POST /sapi/v1/userListenToken "
"with isIsolated=true, then subscribe with userDataStream.subscribe.listenToken). "
"The isolated_margin_socket() method now uses WebSocket API by default.",
DeprecationWarning,
stacklevel=2
)
params = {"symbol": symbol}
res = self._request_margin_api(
"post", "userDataStream/isolated", signed=False, data=params
Expand All @@ -5764,6 +5832,14 @@ def isolated_margin_stream_keepalive(self, symbol, listenKey):
:raises: BinanceRequestException, BinanceAPIException

"""
warnings.warn(
"PUT /sapi/v1/userDataStream/isolated is deprecated and will be removed on 2026-02-20. "
"Use the WebSocket API subscription method instead (create listenToken via POST /sapi/v1/userListenToken "
"with isIsolated=true, then subscribe with userDataStream.subscribe.listenToken). "
"The isolated_margin_socket() method now uses WebSocket API by default.",
DeprecationWarning,
stacklevel=2
)
params = {"symbol": symbol, "listenKey": listenKey}
return self._request_margin_api(
"put", "userDataStream/isolated", signed=False, data=params
Expand All @@ -5788,6 +5864,14 @@ def isolated_margin_stream_close(self, symbol, listenKey):
:raises: BinanceRequestException, BinanceAPIException

"""
warnings.warn(
"DELETE /sapi/v1/userDataStream/isolated is deprecated and will be removed on 2026-02-20. "
"Use the WebSocket API subscription method instead (create listenToken via POST /sapi/v1/userListenToken "
"with isIsolated=true, then subscribe with userDataStream.subscribe.listenToken). "
"The isolated_margin_socket() method now uses WebSocket API by default.",
DeprecationWarning,
stacklevel=2
)
params = {"symbol": symbol, "listenKey": listenKey}
return self._request_margin_api(
"delete", "userDataStream/isolated", signed=False, data=params
Expand Down
Loading
Loading