Bond

Checking network...

Code Examples

Working code examples for common tasks using the Bond API in multiple languages.

Quick Start: Get Market Price

Python

import requests url = 'https://api.bond.xyz/api/v1/ticker/price' response = requests.get(url, params={'symbol': 'BTCUSDT'}) price = response.json()['price'] print(f"BTC Price: ${price}")

JavaScript

const axios = require('axios'); async function getPrice() { const response = await axios.get('https://api.bond.xyz/api/v1/ticker/price', { params: { symbol: 'BTCUSDT' } }); console.log(`BTC Price: $${response.data.price}`); } getPrice();

cURL

curl 'https://api.bond.xyz/api/v1/ticker/price?symbol=BTCUSDT'

Authentication: HMAC Signature

Python

import requests, time, hmac, hashlib API_KEY = 'your_api_key' SECRET_KEY = 'your_secret_key' def get_account(): timestamp = int(time.time() * 1000) params = {'timestamp': timestamp} query_string = '&'.join([f"{k}={v}" for k, v in params.items()]) signature = hmac.new( SECRET_KEY.encode('utf-8'), query_string.encode('utf-8'), hashlib.sha256 ).hexdigest() params['signature'] = signature headers = {'X-MBX-APIKEY': API_KEY} response = requests.get( 'https://api.bond.xyz/api/v1/account', params=params, headers=headers ) return response.json()

JavaScript

const axios = require('axios'); const crypto = require('crypto'); const API_KEY = 'your_api_key'; const SECRET_KEY = 'your_secret_key'; function createSignature(queryString) { return crypto.createHmac('sha256', SECRET_KEY) .update(queryString).digest('hex'); } async function getAccount() { const timestamp = Date.now(); const queryString = `timestamp=${timestamp}`; const signature = createSignature(queryString); const response = await axios.get('https://api.bond.xyz/api/v1/account', { params: { timestamp, signature }, headers: { 'X-MBX-APIKEY': API_KEY } }); return response.data; }

cURL

#!/bin/bash API_KEY="your_api_key" SECRET_KEY="your_secret_key" TIMESTAMP=$(date +%s000) QUERY_STRING="timestamp=${TIMESTAMP}" SIGNATURE=$(echo -n "${QUERY_STRING}" | openssl dgst -sha256 -hmac "${SECRET_KEY}" | cut -d' ' -f2) curl -H "X-MBX-APIKEY: ${API_KEY}" \ "https://api.bond.xyz/api/v1/account?timestamp=${TIMESTAMP}&signature=${SIGNATURE}"

Trading: Place Orders

Python: Limit Order

def place_limit_order(symbol, side, quantity, price): timestamp = int(time.time() * 1000) params = { 'symbol': symbol, 'side': side, 'type': 'LIMIT', 'timeInForce': 'GTC', 'quantity': str(quantity), 'price': str(price), 'timestamp': timestamp } query_string = '&'.join([f"{k}={v}" for k, v in sorted(params.items())]) signature = hmac.new(SECRET_KEY.encode(), query_string.encode(), hashlib.sha256).hexdigest() params['signature'] = signature response = requests.post('https://api.bond.xyz/api/v1/order', params=params, headers={'X-MBX-APIKEY': API_KEY}) return response.json() order = place_limit_order('BTCUSDT', 'BUY', 0.001, 40000)

JavaScript: Market Order

async function placeMarketOrder(symbol, side, quantity) { const timestamp = Date.now(); const params = { symbol, side, type: 'MARKET', quantity: quantity.toString(), timestamp }; const queryString = Object.keys(params).sort() .map(key => `${key}=${params[key]}`).join('&'); params.signature = createSignature(queryString); const response = await axios.post('https://api.bond.xyz/api/v1/order', null, { params, headers: { 'X-MBX-APIKEY': API_KEY } }); return response.data; }

Futures: Positions & Stop-Loss

Python: Open Long Position

def open_long_position(symbol, quantity, leverage=10): set_leverage(symbol, leverage) timestamp = int(time.time() * 1000) params = { 'symbol': symbol, 'side': 'BUY', 'positionSide': 'BOTH', 'type': 'MARKET', 'quantity': str(quantity), 'timestamp': timestamp } query_string = '&'.join([f"{k}={v}" for k, v in sorted(params.items())]) signature = hmac.new(SECRET_KEY.encode(), query_string.encode(), hashlib.sha256).hexdigest() params['signature'] = signature response = requests.post('https://fapi.bond.xyz/fapi/v1/order', params=params, headers={'X-MBX-APIKEY': API_KEY}) return response.json()

Python: Set Stop-Loss

def set_stop_loss(symbol, stop_price): timestamp = int(time.time() * 1000) params = { 'symbol': symbol, 'side': 'SELL', 'type': 'STOP_MARKET', 'stopPrice': str(stop_price), 'closePosition': 'true', 'timestamp': timestamp } query_string = '&'.join([f"{k}={v}" for k, v in sorted(params.items())]) signature = hmac.new(SECRET_KEY.encode(), query_string.encode(), hashlib.sha256).hexdigest() params['signature'] = signature response = requests.post('https://fapi.bond.xyz/fapi/v1/order', params=params, headers={'X-MBX-APIKEY': API_KEY}) return response.json()

WebSocket: Real-time Data

Python

import websocket, json def on_message(ws, message): data = json.loads(message) print(f"Received: {data}") ws = websocket.WebSocketApp( "wss://ws-api.bond.xyz:443/ws-api/v3", on_message=on_message, on_ping=lambda ws, msg: ws.pong(msg) ) # Get price via WebSocket request = { "id": "price-1", "method": "ticker.price", "params": {"symbol": "BTCUSDT"} } ws.send(json.dumps(request)) ws.run_forever()

JavaScript

const WebSocket = require('ws'); const ws = new WebSocket('wss://ws-api.bond.xyz:443/ws-api/v3'); ws.on('open', () => { ws.send(JSON.stringify({ id: 'price-1', method: 'ticker.price', params: { symbol: 'BTCUSDT' } })); }); ws.on('message', (data) => { const response = JSON.parse(data); console.log('Price:', response.result.price); }); ws.on('ping', () => ws.pong());

Advanced: Order Manager Class

Python

class OrderManager: def __init__(self, api_key, secret_key): self.api_key = api_key self.secret_key = secret_key self.base_url = 'https://api.bond.xyz' def _sign(self, params): query_string = '&'.join([f"{k}={v}" for k, v in sorted(params.items())]) return hmac.new(self.secret_key.encode(), query_string.encode(), hashlib.sha256).hexdigest() def place_order(self, symbol, side, order_type, quantity, price=None): timestamp = int(time.time() * 1000) params = {'symbol': symbol, 'side': side, 'type': order_type, 'quantity': str(quantity), 'timestamp': timestamp} if order_type == 'LIMIT': params['timeInForce'] = 'GTC' params['price'] = str(price) params['signature'] = self._sign(params) response = requests.post(f"{self.base_url}/api/v1/order", params=params, headers={'X-MBX-APIKEY': self.api_key}) return response.json() def cancel_order(self, symbol, order_id): timestamp = int(time.time() * 1000) params = {'symbol': symbol, 'orderId': order_id, 'timestamp': timestamp} params['signature'] = self._sign(params) response = requests.delete(f"{self.base_url}/api/v1/order", params=params, headers={'X-MBX-APIKEY': self.api_key}) return response.json() # Usage manager = OrderManager(API_KEY, SECRET_KEY) order = manager.place_order('BTCUSDT', 'BUY', 'LIMIT', 0.001, 40000)