Firma de Solicitud

Ver como Markdown

Formato de Solicitud

UbicaciónCampoObligatorioDescripción
EncabezadoX-API-KEYapiKey regresa cuando se crea el API Key
ConsultatimestampMarca de tiempo de milisegundos de Unix
ConsultasignatureHMAC-SHA256 firma, cuerda hexagonal minúscula
CarroceríaJSONDepende del endpointPOST / PUT / DELETE cuerpo de solicitud

Carga útil característica

payload = queryStringWithoutSignature + requestBody
signature = HMAC-SHA256(apiSecret, payload)

Reglas clave:

  • timestamp debe ser una marca de tiempo de milisegundos de Unix. La ventana de tolerancia del servidor es de ±10 segundos.
  • signature se coloca en la cadena de consulta URL , pero no se incluye en la carga útil de la firma.
  • El servidor verifica la cadena de consulta original después de eliminar signature; no reordena los parámetros de consulta.
  • El orden del parámetro de consulta usado para firmar debe coincidir con la solicitud real URL.
  • Si existe un cuerpo de solicitud, la cadena de JSON usada para firmar debe coincidir exactamente con el cuerpo transmitido realmente.
  • Las rutas relacionadas con el orden realizan pruebas de repetición de firma. Reutilizar la misma firma en un corto periodo devuelve Signature replay detected.

Hora del servidor

GET /v1/time

Ejemplo de respuesta:

{
"code": 0,
"message": "success",
"data": {
"timestamp": 1780473256,
"timestampMs": 1780473256708,
"iso": "2026-06-03T07:54:16Z",
"timezone": "UTC"
},
"requestId": "req-7cad3113"
}

Los clientes deben calcular timeOffsetMs = serverTimestampMs - localTimestampMs, luego usar timestamp = nowMs + timeOffsetMs para solicitudes firmadas posteriores. Si el servidor regresa Timestamp outside of tolerance window, inmediatamente resincroniza el tiempo y regenera la firma.

GET Ejemplo de firma

Solicitud real:

GET /v1/private/order/current?symbol=BTCUSDT&timestamp=1772710377808&signature=...

Carga útil de firma:

symbol=BTCUSDT&timestamp=1772710377808

POST Ejemplo de firma

Solicitud real:

POST /v1/private/order/place?timestamp=1772710377808&signature=...

Cuerpo de la solicitud:

{"symbol":"BTCUSDT","type":"LIMIT","side":"BUY","price":"85000","quantity":"0.1","timeInForce":"GTC","makerOnly":true,"clientOrderId":"ext-1772710377808-001"}

Carga útil de firma:

timestamp=1772710377808{"symbol":"BTCUSDT","type":"LIMIT","side":"BUY","price":"85000","quantity":"0.1","timeInForce":"GTC","makerOnly":true,"clientOrderId":"ext-1772710377808-001"}

Ejemplo de firma en Python

import hashlib
import hmac
import json
import time
from urllib.parse import urlencode
import requests
BASE_URL = "https://api.6mm.com"
API_KEY = "YOUR_API_KEY"
API_SECRET = "YOUR_API_SECRET"
TIME_OFFSET_MS = 0
def sign(payload: str) -> str:
return hmac.new(
API_SECRET.encode("utf-8"),
payload.encode("utf-8"),
hashlib.sha256,
).hexdigest()
def sync_time_offset():
global TIME_OFFSET_MS
before = int(time.time() * 1000)
resp = requests.get(f"{BASE_URL}/v1/time", timeout=5)
after = int(time.time() * 1000)
resp.raise_for_status()
server_ts = int(resp.json()["data"]["timestampMs"])
local_midpoint = (before + after) // 2
TIME_OFFSET_MS = server_ts - local_midpoint
def signed_request(method: str, path: str, params=None, body=None):
params = dict(params or {})
params["timestamp"] = str(int(time.time() * 1000) + TIME_OFFSET_MS)
query_string = urlencode(params)
body_string = ""
if body is not None:
body_string = json.dumps(body, separators=(",", ":"), ensure_ascii=False)
signature = sign(query_string + body_string)
url = f"{BASE_URL}{path}?{query_string}&signature={signature}"
headers = {
"X-API-KEY": API_KEY,
"Content-Type": "application/json",
}
resp = requests.request(method, url, headers=headers, data=body_string if body is not None else None, timeout=10)
resp.raise_for_status()
return resp.json()
sync_time_offset()
print(signed_request("GET", "/v1/private/order/current", {"symbol": "BTCUSDT"}))