Skip to main content

Margin Type

Description​

Query or change the margin type for the caller (cross / isolated).

Supports both Cross and Isolated margin per symbol

/fapi/v1/marginType supports querying and switching between Cross (CROSSED) and Isolated (ISOLATED) margin per trading symbol. Switching requires no open positions or active orders on that symbol. POST requires an idempotent requestId (UUID) and may return 200 OK or 202 Accepted.

HTTP Request​

  • Query: GET /fapi/v1/marginType (HMAC SHA256)
  • Change: POST /fapi/v1/marginType (HMAC SHA256)

GET Request Parameters​

NameTypeRequiredDescription
symbolSTRINGYESTrading pair (e.g. BTCUSDT).
recvWindowLONGNOIgnored — the server enforces a fixed ±60 second timestamp window regardless of this value.
timestampLONGYESTimestamp

POST Request Parameters​

NameTypeRequiredDescription
symbolSTRINGYESTrading pair
marginTypeSTRINGYESCROSSED (or CROSS) or ISOLATED. Case-insensitive.
requestIdSTRINGYESIdempotent request UUID (alias: request_id).
recvWindowLONGNOIgnored — the server enforces a fixed ±60 second timestamp window regardless of this value.
timestampLONGYESTimestamp

Response Example (GET)​

{
"symbol": "BTCUSDT",
"marginType": "CROSSED"
}

Response Example (POST)​

{
"code": 200,
"msg": "success"
}

Errors​

CodeMessageCause
-1102Mandatory parameter missing.Missing required parameter (e.g. symbol or requestId).
-1128Invalid marginType. Expected CROSSED or ISOLATED.Any value other than CROSSED/CROSS/ISOLATED.
-2010Cannot change margin mode while open positions or orders exist.Open positions or active orders exist on the symbol.

Code Examples​

curl (query)​

API_KEY="your_api_key"
API_SECRET="your_api_secret"
TIMESTAMP=$(date +%s%3N)
QUERY_STRING="symbol=BTCUSDT&timestamp=${TIMESTAMP}"
SIGNATURE=$(echo -n "${QUERY_STRING}" | openssl dgst -sha256 -hmac "${API_SECRET}" | awk '{print $2}')
curl -s -H "X-MBX-APIKEY: ${API_KEY}" \
"https://api.ztdxtest.xyz/fapi/v1/marginType?${QUERY_STRING}&signature=${SIGNATURE}"

Python​

import time, hmac, hashlib, requests

API_KEY = "your_api_key"
API_SECRET = "your_api_secret"
BASE_URL = "https://api.ztdxtest.xyz"

def sign(qs: str) -> str:
return hmac.new(API_SECRET.encode(), qs.encode(), hashlib.sha256).hexdigest()

# Query margin type
ts = int(time.time() * 1000)
qs = f"symbol=BTCUSDT&timestamp={ts}"
r = requests.get(
f"{BASE_URL}/fapi/v1/marginType?{qs}&signature={sign(qs)}",
headers={"X-MBX-APIKEY": API_KEY},
)
print(r.json()) # {"symbol":"BTCUSDT","marginType":"CROSSED"}

# Change margin type (supports CROSSED / ISOLATED)
import json, uuid
ts = int(time.time() * 1000)
body = json.dumps(
{"symbol": "BTCUSDT", "marginType": "ISOLATED", "requestId": str(uuid.uuid4())},
separators=(",", ":"),
)
qs = f"timestamp={ts}"
r = requests.post(
f"{BASE_URL}/fapi/v1/marginType?{qs}&signature={sign(qs + body)}",
data=body,
headers={"X-MBX-APIKEY": API_KEY, "Content-Type": "application/json"},
)
print(r.json()) # {"code":200,"msg":"success"}