Binance official derivatives trading portfolio margin

From jiayaoqijia/cryptoskill · View interactive version

Binance official derivatives trading portfolio margin is an AI agent skill from the GitHub repository jiayaoqijia/cryptoskill. Binance Derivatives-trading-portfolio-margin request using the Binance API. Authentication requires API key and secret key. Supports testnet and mainnet.

How to use this skill

Skill file

Binance Derivatives-trading-portfolio-margin Skill

Derivatives-trading-portfolio-margin request on Binance using authenticated API endpoints. Requires API key and secret key for certain endpoints. Return the result in JSON format.

Quick Reference

| Endpoint | Description | Required | Optional | Authentication |

|----------|-------------|----------|----------|----------------|

| /papi/v1/balance (GET) | Account Balance(USER_DATA) | None | asset, recvWindow | Yes |

| /papi/v1/account (GET) | Account Information(USER_DATA) | None | recvWindow | Yes |

| /papi/v1/bnb-transfer (POST) | BNB transfer (TRADE) | amount, transferSide | recvWindow | Yes |

| /papi/v1/cm/leverageBracket (GET) | CM Notional and Leverage Brackets(USER_DATA) | None | symbol, recvWindow | Yes |

| /papi/v1/repay-futures-switch (POST) | Change Auto-repay-futures Status(TRADE) | autoRepay | recvWindow | Yes |

| /papi/v1/repay-futures-switch (GET) | Get Auto-repay-futures Status(USER_DATA) | None | recvWindow | Yes |

| /papi/v1/cm/leverage (POST) | Change CM Initial Leverage (TRADE) | symbol, leverage | recvWindow | Yes |

| /papi/v1/cm/positionSide/dual (POST) | Change CM Position Mode(TRADE) | dualSidePosition | recvWindow | Yes |

| /papi/v1/cm/positionSide/dual (GET) | Get CM Current Position Mode(USER_DATA) | None | recvWindow | Yes |

| /papi/v1/um/leverage (POST) | Change UM Initial Leverage(TRADE) | symbol, leverage | recvWindow | Yes |

| /papi/v1/um/positionSide/dual (POST) | Change UM Position Mode(TRADE) | dualSidePosition | recvWindow | Yes |

| /papi/v1/um/positionSide/dual (GET) | Get UM Current Position Mode(USER_DATA) | None | recvWindow | Yes |

| /papi/v1/auto-collection (POST) | Fund Auto-collection(TRADE) | None | recvWindow | Yes |

| /papi/v1/asset-collection (POST) | Fund Collection by Asset(TRADE) | asset | recvWindow | Yes |

| /papi/v1/cm/account (GET) | Get CM Account Detail(USER_DATA) | None | recvWindow | Yes |

| /papi/v1/cm/income (GET) | Get CM Income History(USER_DATA) | None | symbol, incomeType, startTime, endTime, page, limit, recvWindow | Yes |

| /papi/v1/um/order/asyn (GET) | Get Download Id For UM Futures Order History (USER_DATA) | startTime, endTime | recvWindow | Yes |

| /papi/v1/um/trade/asyn (GET) | Get Download Id For UM Futures Trade History (USER_DATA) | startTime, endTime | recvWindow | Yes |

| /papi/v1/um/income/asyn (GET) | Get Download Id For UM Futures Transaction History (USER_DATA) | startTime, endTime | recvWindow | Yes |

| /papi/v1/margin/marginInterestHistory (GET) | Get Margin Borrow/Loan Interest History(USER_DATA) | None | asset, startTime, endTime, current, size, archived, recvWindow | Yes |

| /papi/v2/um/account (GET) | Get UM Account Detail V2(USER_DATA) | None | recvWindow | Yes |

| /papi/v1/um/account (GET) | Get UM Account Detail(USER_DATA) | None | recvWindow | Yes |

| /papi/v1/um/accountConfig (GET) | UM Futures Account Configuration(USER_DATA) | None | recvWindow | Yes |

| /papi/v1/um/order/asyn/id (GET) | Get UM Futures Order Download Link by Id(USER_DATA) | downloadId | recvWindow | Yes |

| /papi/v1/um/symbolConfig (GET) | UM Futures Symbol Configuration(USER_DATA) | None | symbol, recvWindow | Yes |

| /papi/v1/um/trade/asyn/id (GET) | Get UM Futures Trade Download Link by Id(USER_DATA) | downloadId | recvWindow | Yes |

| /papi/v1/um/income/asyn/id (GET) | Get UM Futures Transaction Download Link by Id(USER_DATA) | downloadId | recvWindow | Yes |

| /papi/v1/um/income (GET) | Get UM Income History(USER_DATA) | None | symbol, incomeType, startTime, endTime, page, limit, recvWindow | Yes |

| /papi/v1/cm/commissionRate (GET) | Get User Commission Rate for CM(USER_DATA) | symbol | recvWindow | Yes |

| /papi/v1/um/commissionRate (GET) | Get User Commission Rate for UM(USER_DATA) | symbol | recvWindow | Yes |

| /papi/v1/margin/maxBorrowable (GET) | Margin Max Borrow(USER_DATA) | asset | recvWindow | Yes |

| /papi/v1/um/apiTradingStatus (GET) | Portfolio Margin UM Trading Quantitative Rules Indicators(USER_DATA) | None | symbol, recvWindow | Yes |

| /papi/v1/cm/positionRisk (GET) | Query CM Position Information(USER_DATA) | None | marginAsset, pair, recvWindow | Yes |

| /papi/v1/margin/marginLoan (GET) | Query Margin Loan Record(USER_DATA) | asset | txId, startTime, endTime, current, size, archived, recvWindow | Yes |

| /papi/v1/margin/maxWithdraw (GET) | Query Margin Max Withdraw(USER_DATA) | asset | recvWindow | Yes |

| /papi/v1/margin/repayLoan (GET) | Query Margin repay Record(USER_DATA) | asset | txId, startTime, endTime, current, size, archived, recvWindow | Yes |

| /papi/v1/portfolio/interest-history (GET) | Query Portfolio Margin Negative Balance Interest History(USER_DATA) | None | asset, startTime, endTime, size, recvWindow | Yes |

| /papi/v1/um/positionRisk (GET) | Query UM Position Information(USER_DATA) | None | symbol, recvWindow | Yes |

| /papi/v1/portfolio/negative-balance-exchange-record (GET) | Query User Negative Balance Auto Exchange Record (USER_DATA) | startTime, endTime | recvWindow | Yes |

| /papi/v1/rateLimit/order (GET) | Query User Rate Limit (USER_DATA) | None | recvWindow | Yes |

| /papi/v1/repay-futures-negative-balance (POST) | Repay futures Negative Balance(USER_DATA) | None | recvWindow | Yes |

| /papi/v1/um/leverageBracket (GET) | UM Notional and Leverage Brackets (USER_DATA) | None | symbol, recvWindow | Yes |

| /papi/v1/ping (GET) | Test Connectivity | None | None | No |

| /papi/v1/cm/userTrades (GET) | CM Account Trade List(USER_DATA) | None | symbol, pair, startTime, endTime, fromId, limit, recvWindow | Yes |

| /papi/v1/cm/adlQuantile (GET) | CM Position ADL Quantile Estimation(USER_DATA) | None | symbol, recvWindow | Yes |

| /papi/v1/cm/conditional/allOpenOrders (DELETE) | Cancel All CM Open Conditional Orders(TRADE) | symbol | recvWindow | Yes |

| /papi/v1/cm/allOpenOrders (DELETE) | Cancel All CM Open Orders(TRADE) | symbol | recvWindow | Yes |

| /papi/v1/um/conditional/allOpenOrders (DELETE) | Cancel All UM Open Conditional Orders (TRADE) | symbol | recvWindow | Yes |

| /papi/v1/um/allOpenOrders (DELETE) | Cancel All UM Open Orders(TRADE) | symbol | recvWindow | Yes |

| /papi/v1/cm/conditional/order (DELETE) | Cancel CM Conditional Order(TRADE) | symbol | strategyId, newClientStrategyId, recvWindow | Yes |

| /papi/v1/cm/conditional/order (POST) | New CM Conditional Order(TRADE) | symbol, side, strategyType | positionSide, timeInForce, quantity, reduceOnly, price, workingType, priceProtect, newClientStrategyId, stopPrice, activationPrice, callbackRate, recvWindow | Yes |

| /papi/v1/cm/order (DELETE) | Cancel CM Order(TRADE) | symbol | orderId, origClientOrderId, recvWindow | Yes |

| /papi/v1/cm/order (PUT) | Modify CM Order(TRADE) | symbol, side, quantity, price | orderId, origClientOrderId, priceMatch, recvWindow | Yes |

| /papi/v1/cm/order (POST) | New CM Order(TRADE) | symbol, side, type | positionSide, timeInForce, quantity, reduceOnly, price, priceMatch, newClientOrderId, newOrderRespType, recvWindow | Yes |

| /papi/v1/cm/order (GET) | Query CM Order(USER_DATA) | symbol | orderId, origClientOrderId, recvWindow | Yes |

| /papi/v1/margin/allOpenOrders (DELETE) | Cancel Margin Account All Open Orders on a Symbol(TRADE) | symbol | recvWindow | Yes |

| /papi/v1/margin/orderList (DELETE) | Cancel Margin Account OCO Orders(TRADE) | symbol | orderListId, listClientOrderId, newClientOrderId, recvWindow | Yes |

| /papi/v1/margin/orderList (GET) | Query Margin Account's OCO (USER_DATA) | None | orderListId, origClientOrderId, recvWindow | Yes |

| /papi/v1/margin/order (DELETE) | Cancel Margin Account Order(TRADE) | symbol | orderId, origClientOrderId, newClientOrderId, recvWindow | Yes |

| /papi/v1/margin/order (POST) | New Margin Order(TRADE) | symbol, side, type | quantity, quoteOrderQty, price, stopPrice, newClientOrderId, newOrderRespType, icebergQty, sideEffectType, timeInForce, selfTradePreventionMode, autoRepayAtCancel, recvWindow | Yes |

| /papi/v1/margin/order (GET) | Query Margin Account Order (USER_DATA) | symbol | orderId, origClientOrderId, recvWindow | Yes |

| /papi/v1/um/conditional/order (DELETE) | Cancel UM Conditional Order(TRADE) | symbol | strategyId, newClientStrategyId, recvWindow | Yes |

| /papi/v1/um/conditional/order (POST) | New UM Conditional Order (TRADE) | symbol, side, strategyType | positionSide, timeInForce, quantity, reduceOnly, price, workingType, priceProtect, newClientStrategyId, stopPrice, activationPrice, callbackRate, priceMatch, selfTradePreventionMode, goodTillDate, recvWindow | Yes |

| /papi/v1/um/order (DELETE) | Cancel UM Order(TRADE) | symbol | orderId, origClientOrderId, recvWindow | Yes |

| /papi/v1/um/order (PUT) | Modify UM Order(TRADE) | symbol, side, quantity, price | orderId, origClientOrderId, priceMatch, recvWindow | Yes |

| /papi/v1/um/order (POST) | New UM Order (TRADE) | symbol, side, type | positionSide, timeInForce, quantity, reduceOnly, price, newClientOrderId, newOrderRespType, priceMatch, selfTradePreventionMode, goodTillDate, recvWindow | Yes |

| /papi/v1/um/order (GET) | Query UM Order (USER_DATA) | symbol | orderId, origClientOrderId, recvWindow | Yes |

| /papi/v1/um/feeBurn (GET) | Get UM Futures BNB Burn Status (USER_DATA) | None | recvWindow | Yes |

| /papi/v1/um/feeBurn (POST) | Toggle BNB Burn On UM Futures Trade (TRADE) | feeBurn | recvWindow | Yes |

| /papi/v1/marginLoan (POST) | Margin Account Borrow(MARGIN) | asset, amount | recvWindow | Yes |

| /papi/v1/margin/order/oco (POST) | Margin Account New OCO(TRADE) | symbol, side, quantity, price, stopPrice | listClientOrderId, limitClientOrderId, limitIcebergQty, stopClientOrderId, stopLimitPrice, stopIcebergQty, stopLimitTimeInForce, newOrderRespType, sideEffectType, recvWindow | Yes |

| /papi/v1/margin/repay-debt (POST) | Margin Account Repay Debt(TRADE) | asset | amount, specifyRepayAssets, recvWindow | Yes |

| /papi/v1/repayLoan (POST) | Margin Account Repay(MARGIN) | asset, amount | recvWindow | Yes |

| /papi/v1/margin/myTrades (GET) | Margin Account Trade List (USER_DATA) | symbol | orderId, startTime, endTime, fromId, limit, recvWindow | Yes |

| /papi/v1/cm/conditional/allOrders (GET) | Query All CM Conditional Orders(USER_DATA) | None | symbol, strategyId, startTime, endTime, limit, recvWindow | Yes |

| /papi/v1/cm/allOrders (GET) | Query All CM Orders (USER_DATA) | symbol | pair, orderId, startTime, endTime, limit, recvWindow | Yes |

| /papi/v1/cm/conditional/openOrders (GET) | Query All Current CM Open Conditional Orders (USER_DATA) | None | symbol, recvWindow | Yes |

| /papi/v1/cm/openOrders (GET) | Query All Current CM Open Orders(USER_DATA) | None | symbol, pair, recvWindow | Yes |

| /papi/v1/um/conditional/openOrders (GET) | Query All Current UM Open Conditional Orders(USER_DATA) | None | symbol, recvWindow | Yes |

| /papi/v1/um/openOrders (GET) | Query All Current UM Open Orders(USER_DATA) | None | symbol, recvWindow | Yes |

| /papi/v1/margin/allOrders (GET) | Query All Margin Account Orders (USER_DATA) | symbol | orderId, startTime, endTime, limit, recvWindow | Yes |

| /papi/v1/um/conditional/allOrders (GET) | Query All UM Conditional Orders(USER_DATA) | None | symbol, strategyId, startTime, endTime, limit, recvWindow | Yes |

| /papi/v1/um/allOrders (GET) | Query All UM Orders(USER_DATA) | symbol | orderId, startTime, endTime, limit, recvWindow | Yes |

| /papi/v1/cm/conditional/orderHistory (GET) | Query CM Conditional Order History(USER_DATA) | symbol | strategyId, newClientStrategyId, recvWindow | Yes |

| /papi/v1/cm/orderAmendment (GET) | Query CM Modify Order History(TRADE) | symbol | orderId, origClientOrderId, startTime, endTime, limit, recvWindow | Yes |

| /papi/v1/cm/conditional/openOrder (GET) | Query Current CM Open Conditional Order(USER_DATA) | symbol | strategyId, newClientStrategyId, recvWindow | Yes |

| /papi/v1/cm/openOrder (GET) | Query Current CM Open Order (USER_DATA) | symbol | orderId, origClientOrderId, recvWindow | Yes |

| /papi/v1/margin/openOrders (GET) | Query Current Margin Open Order (USER_DATA) | symbol | recvWindow | Yes |

| /papi/v1/um/conditional/openOrder (GET) | Query Current UM Open Conditional Order(USER_DATA) | symbol | strategyId, newClientStrategyId, recvWindow | Yes |

| /papi/v1/um/openOrder (GET) | Query Current UM Open Order(USER_DATA) | symbol | orderId, origClientOrderId, recvWindow | Yes |

| /papi/v1/margin/openOrderList (GET) | Query Margin Account's Open OCO (USER_DATA) | None | recvWindow | Yes |

| /papi/v1/margin/allOrderList (GET) | Query Margin Account's all OCO (USER_DATA) | None | fromId, startTime, endTime, limit, recvWindow | Yes |

| /papi/v1/um/conditional/orderHistory (GET) | Query UM Conditional Order History(USER_DATA) | symbol | strategyId, newClientStrategyId, recvWindow | Yes |

| /papi/v1/um/orderAmendment (GET) | Query UM Modify Order History(TRADE) | symbol | orderId, origClientOrderId, startTime, endTime, limit, recvWindow | Yes |

| /papi/v1/cm/forceOrders (GET) | Query User's CM Force Orders(USER_DATA) | None | symbol, autoCloseType, startTime, endTime, limit, recvWindow | Yes |

| /papi/v1/margin/forceOrders (GET) | Query User's Margin Force Orders(USER_DATA) | None | startTime, endTime, current, size, recvWindow | Yes |

| /papi/v1/um/forceOrders (GET) | Query User's UM Force Orders (USER_DATA) | None | symbol, autoCloseType, startTime, endTime, limit, recvWindow | Yes |

| /papi/v1/um/userTrades (GET) | UM Account Trade List(USER_DATA) | symbol | startTime, endTime, fromId, limit, recvWindow | Yes |

| /papi/v1/um/adlQuantile (GET) | UM Position ADL Quantile Estimation(USER_DATA) | None | symbol, recvWindow | Yes |

| /papi/v1/listenKey (DELETE) | Close User Data Stream(USER_STREAM) | None | None | No |

| /papi/v1/listenKey (PUT) | Keepalive User Data Stream (USER_STREAM) | None | None | No |

| /papi/v1/listenKey (POST) | Start User Data Stream(USER_STREAM) | None | None | No |


Parameters

Common Parameters

Enums

Authentication

For endpoints that require authentication, you will need to provide Binance API credentials.

Required credentials:

Base URLs:

Security

Share Credentials

Users can provide Binance API credentials in different ways. The agent will try to retrieve automatically with the two first ways the credentials, but users can also explicitly tell the agent that they have set the credentials or stored them in a .env file, and the agent should re-read that file when they do. The agent can also make http requests with the two first methods without user confirmation.

  1. Environment variables

Search for the following specific variables only (never dump the full environment):

Authorized environment variables

Read and use in a single exec call so the raw key never enters the agent's context:

KEY="$BINANCE_API_KEY"
SECRET="$BINANCE_SECRET_KEY"

response=$(curl -s -X GET "$URL" \
  -H "X-MBX-APIKEY: $KEY" \
  --data-urlencode "param1=value1")

echo "$response"

Environment variables must be set before OpenClaw starts. They are inherited at process startup and cannot be injected into a running instance. If you need to add or update credentials without restarting, use a secrets file (see option 2).

  1. Secrets file (.env)

Check ~/.openclaw/secrets.env , ~/.env, or a .env file in the workspace. Read individual keys with grep, never source the full file:

# Try all credential locations in order
API_KEY=$(grep '^BINANCE_API_KEY=' ~/.openclaw/secrets.env 2>/dev/null | cut -d= -f2-)
SECRET_KEY=$(grep '^BINANCE_SECRET_KEY=' ~/.openclaw/secrets.env 2>/dev/null | cut -d= -f2-)

# Fallback: search .env in known directories (KEY=VALUE then raw line format)
for dir in ~/.openclaw ~; do
  [ -n "$API_KEY" ] && break
  env_file="$dir/.env"
  [ -f "$env_file" ] || continue

  # Read first two lines
  line1=$(sed -n '1p' "$env_file")
  line2=$(sed -n '2p' "$env_file")

  # Check if lines contain '=' indicating KEY=VALUE format
  if [[ "$line1" == *=* && "$line2" == *=* ]]; then
    API_KEY=$(grep '^BINANCE_API_KEY=' "$env_file" 2>/dev/null | cut -d= -f2-)
    SECRET_KEY=$(grep '^BINANCE_SECRET_KEY=' "$env_file" 2>/dev/null | cut -d= -f2-)
  else
    # Treat lines as raw values
    API_KEY="$line1"
    SECRET_KEY="$line2"
  fi
done

This file can be updated at any time without restarting OpenClaw, keys are read fresh on each invocation. Users can tell you the variables are now set or stored in a .env file, and you should re-read that file when they do.

  1. Inline file

Sending a file where the content is in the following format:

abc123...xyz
secret123...key

Never Disclose API Key and Secret

Never disclose the location of the API key and secret file.

Never send the API key and secret to any website other than Mainnet and Testnet.

Never Display Full Secrets

When showing credentials to users:

Example response when asked for credentials:

Account: main

API Key: su1Qc...8akf

Secret: ***...aws1

Environment: Mainnet

Listing Accounts

When listing accounts, show names and environment only — never keys:

Binance Accounts:

Transactions in Mainnet

When performing transactions in mainnet, always confirm with the user before proceeding by asking them to write "CONFIRM" to proceed.


Binance Accounts

main

testnet-dev

TOOLS.md Structure

## Binance Accounts

### main
- API Key: abc123...xyz
- Secret: secret123...key
- Testnet: false
- Description: Primary trading account

### testnet-dev
- API Key: test456...abc
- Secret: testsecret...xyz
- Testnet: true
- Description: Development/testing

### futures-keys
- API Key: futures789...def
- Secret: futuressecret...uvw
- Testnet: false
- Description: Futures trading account

Agent Behavior

  1. Credentials requested: Mask secrets (show last 5 chars only)
  2. Listing accounts: Show names and environment, never keys
  3. Account selection: Ask if ambiguous, default to main
  4. When doing a transaction in mainnet, confirm with user before by asking to write "CONFIRM" to proceed
  5. New credentials: Prompt for name, environment, signing mode
  6. When a request requires signing, if the request isn't an order and the API keys aren't described as mainnet or testnet keys, try to make request to the different base urls and see if it works, without asking the user. If it works, store the keys with the corresponding environment.

Adding New Accounts

When user provides new credentials by Inline file or message:

Signing Requests

For trading endpoints that require a signature:

  1. Detect key type first, inspect the secret key format before signing.
  2. Build query string with all parameters, including the timestamp (Unix ms).
  3. Percent-encode the parameters using UTF-8 according to RFC 3986.
  4. Sign query string with secretKey using HMAC SHA256, RSA, or Ed25519 (depending on the account configuration).
  5. Append signature to query string.
  6. Include X-MBX-APIKEY header.

Otherwise, do not perform steps 4–6.

New Client Order ID

For endpoints that include the newClientOrderId parameter, the value must always start with agent-. If the parameter is not provided, agent- followed by 18 random alphanumeric characters will be generated automatically. If a value is provided, it will be prefixed with agent-

Example: agent-1a2b3c4d5e6f7g8h9i

User Agent Header

Include User-Agent header with the following string: binance-derivatives-trading-portfolio-margin/1.1.0 (Skill)

See CODE0 for implementation details.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.