client_id و تلاش مجدد امن
فرض کنید POST /orders/ را فرستادهاید و اتصال قطع میشود یا timeout میخورید. نمیدانید سفارش ساخته شده یا نه. اگر بدون فکر دوباره بفرستید، ممکن است دو بار خرید کنید.
client_id این مشکل را حل میکند.
client_id چیست؟
یک شناسهی دلخواه که خودتان برای هر سفارش میسازید و همراه درخواست میفرستید:
- ۱ تا ۶۴ کاراکتر از
[A-Za-z0-9_-] - یکتا برای هر کاربر، بین همهی سفارشهای آنی و حد
- مثالها: یک UUID، یا
bot-2026-11-07-000123
سه حالت پاسخ
| درخواست | پاسخ |
|---|---|
client_id جدید | 201، سفارش جدید |
همان client_id با همان quote_id، base_amount و endpoint | 200، همان سفارش قبلی بدون تغییر. سفارش دوم ساخته نمیشود، حتی اگر دو درخواست همزمان برسند |
همان client_id با هر چیز متفاوت | 409 client_id_conflict |
برای POST /limit-orders/ هم دقیقاً همین قوانین برقرار است. client_id بین سفارشهای آنی و حد مشترک است: client_idای که برای سفارش آنی استفاده شده برای سفارش حد 409 میگیرد.
الگوی بازیابی پس از timeout
POST /orders/ (client_id = X)
│
├── response 201/200 → done
│
└── no response (timeout / connection error)
│
GET /orders/by-client-id/X/
├── 200 → the order exists, use it (do NOT resend)
└── 404 → it was never created; resend the same POST (same X)
دو روش هر دو امناند:
- ارسال دوبارهی همان درخواست با همان
client_id. اگر سفارش ساخته شده باشد،200و همان سفارش را میگیرید. - جستجو با
GET /orders/by-client-id/{client_id}/. اگر404گرفتید، سفارش ساخته نشده و میتوانید بفرستید.
curl -s -H "X-API-Key: $TALANO_API_KEY" \
https://service.talano.ir/api/public/v1/orders/by-client-id/bot-2026-11-07-000123/
پیشفاکتور ۳۰ ثانیه اعتبار دارد
اگر بازیابی بیش از ۳۰ ثانیه طول بکشد و سفارش ساخته نشده باشد، ارسال دوباره quote_expired میگیرد. در این حالت پیشفاکتور جدید بگیرید و client_id جدید بسازید؛ هر client_id را به یک پیشفاکتور گره بزنید.
نمونهی پایتون
idempotent_order.py
"""Safe retries with client_id and recovery with GET /orders/by-client-id/.
If the connection drops after you POST an order, you cannot know whether it was created.
Never just resend without a client_id. With one, either:
* resend the same body (same client_id + quote_id + base_amount) -> 200 with the existing order, or
* look it up by client_id -> 200 if it exists, 404 if it was never created.
"""
import os
import uuid
import requests
BASE = os.environ.get("TALANO_BASE_URL", "https://service.talano.ir/api/public/v1")
s = requests.Session()
s.headers["X-API-Key"] = os.environ["TALANO_API_KEY"]
def place_order_safely(quote: dict, client_id: str, attempts: int = 3) -> dict:
body = {
"quote_id": quote["quote_id"],
"base_amount": int(quote["base_amount"]), # response numbers are strings
"client_id": client_id,
}
for attempt in range(1, attempts + 1):
try:
r = s.post(f"{BASE}/orders/", json=body, timeout=10)
except (requests.ConnectionError, requests.Timeout):
print(f"attempt {attempt}: no response, checking whether the order exists")
found = s.get(f"{BASE}/orders/by-client-id/{client_id}/", timeout=10)
if found.status_code == 200:
return found.json() # it was created; do not send again
continue # 404: never created; resending is safe
if r.status_code in (200, 201): # 201 new, 200 = same client_id replayed
return r.json()
r.raise_for_status()
raise RuntimeError("could not place order")
market = next(m for m in s.get(f"{BASE}/markets/", timeout=10).json() if m["symbol"] == "MLG/TMN")
quote = s.post(f"{BASE}/quotes/", json={
"market_id": market["id"], "side": "buy", "base_amount": 1000,
}, timeout=10).json()
client_id = f"bot-{uuid.uuid4()}" # 1-64 chars of [A-Za-z0-9_-], unique per user
order = place_order_safely(quote, client_id)
print("order", order["id"], order["status"], "client_id", order["client_id"])
چه چیزهایی بدون client_id امن هستند؟
| درخواست | بدون client_id |
|---|---|
همهی GETها | همیشه امن |
POST /quotes/ | امن؛ پیشفاکتور اضافه ضرری ندارد و بعد از ۳۰ ثانیه منقضی میشود |
POST /limit-orders/{order_id}/cancel/ | امن؛ لغو دوباره order_not_cancellable میگیرد و با GET /orders/{order_id}/ میتوانید ببینید cancelled شده |
POST /orders/ و POST /limit-orders/ | امن نیست. همیشه client_id بفرستید |