How to Set Up API Trading on dYdX

Risk warning. Trading perpetual futures and other leveraged crypto-asset products is high risk. A large share of retail traders lose money, so never trade with money you cannot afford to lose entirely. Disclaimer

dYdX’s Python client isn’t talking to one service, it’s talking to two: a node connection for submitting orders directly to the chain, and a separate Indexer for reading market data, and mixing up which one handles which task is a common early setup mistake.

This guide is part of TradeOnDEX’s dYdX how-to series within Perpetual Futures.

Disclosure: TradeOnDEX may earn a commission from links to dYdX on this page, at no extra cost to you. Full details at the end of this article.

Why there are two separate connections, not one

dYdX v4’s architecture separates order submission (going directly to the chain, a Cosmos-based blockchain) from market data querying (served by a dedicated Indexer service, which aggregates and serves chain data more efficiently than querying the chain directly for every read). The Indexer client itself can use two different protocols, REST for standard queries and WebSockets for live updates, so a full setup involves configuring both an HTTP sub-client and a WebSocket sub-client.

This means for you, concretely: a working bot needs both pieces configured correctly, reading market data through the Indexer and submitting orders through the node connection, rather than assuming a single client instance handles everything.

How to set up, step by step

  1. Install the official Python package: dydx-v4-client, available via PyPI.
  2. Configure the Indexer client with both its REST and WebSocket endpoints, instantiating the HTTP sub-client (IndexerClient) and the WebSocket sub-client (IndexerSocket) separately.
  3. Configure your node connection separately for order submission, this is the piece that actually signs and submits transactions to the chain.
  4. Build your trading logic using the async client’s concurrency features, designed specifically to reduce latency and optimize resource usage compared to earlier client iterations.

What this setup enables

The current async Python client is built to support genuinely latency-sensitive use cases, from a high-frequency trading bot to a live DeFi dashboard pulling real-time market data. The three-layer client structure (a basic query-node client, plus the Indexer’s REST and WebSocket sub-clients) reflects this: read-heavy dashboard use cases can lean on the Indexer, while latency-sensitive trading logic needs the direct node connection working correctly too.

Risk: what to get right before trusting a bot with real capital

Mixing up the Indexer and node connection’s responsibilities is a common and consequential setup error. The Indexer is for reading data efficiently; actual order submission goes through your node connection, a bot misconfigured to expect order confirmation through the Indexer alone may behave unpredictably.

A bot with direct API access to your wallet has no manual confirmation step. As covered for API/bot trading on other platforms in this series, this is the entire point of automation, but it also means a logic error executes immediately and potentially repeatedly, test extensively with small size before scaling up.

Async, concurrent code introduces its own class of bugs beyond simple sequential logic. The latency benefits of the async client come with real complexity, race conditions and improperly awaited calls can cause behavior that’s harder to debug than a simpler synchronous script, especially under real trading conditions with time pressure.

WebSocket connections can drop, and your bot needs to handle reconnection gracefully. A live trading bot relying on the Indexer’s WebSocket sub-client for real-time data needs to detect and recover from connection drops, a bot that silently stops receiving updates after a dropped connection is a genuine operational risk, not a hypothetical edge case.

Consider isolating bot-controlled funds, similar to guidance given for other platforms in this series. Using a dedicated sub-account or a separate wallet for bot-controlled capital limits your exposure if a bug or compromised key affects the automated system specifically, rather than your full account balance.

The client’s layered structure, at a glance

LayerPurpose
Query-node clientBasic queries sent directly to a node
IndexerClient (REST)Efficient market data queries via the Indexer service
IndexerSocket (WebSocket)Live, real-time market data updates via the Indexer service
Node connection (order submission)Signing and submitting actual trade transactions to the chain

See it for yourself

Before deploying a bot with meaningful capital, run it against a small position size and deliberately test its behavior when a WebSocket connection drops, to confirm it recovers gracefully rather than trading blind on stale data. You can review the full client documentation at docs.dydx.xyz.

Frequently asked questions

Does dYdX’s Python client use a single connection for everything?

No, it separates market data (via the Indexer’s REST and WebSocket sub-clients) from order submission (via a direct node connection), and a working bot needs both configured correctly.

What’s the official Python package for dYdX API trading?

dydx-v4-client, available via PyPI, built with async support for latency-sensitive trading use cases.

What happens if my bot’s WebSocket connection drops?

It depends on how you’ve built your reconnection handling, a bot that doesn’t detect and recover from a dropped connection risks trading on stale data, so this needs explicit handling in your code.

Should I use my main wallet for bot trading?

Consider a dedicated sub-account or separate wallet instead, to limit exposure if a bug or compromised key affects the automated system specifically.

Affiliate disclosure

TradeOnDEX may earn a commission if you sign up or trade through a link on this page, at no extra cost to you. dYdX’s own affiliate/referral terms are still being confirmed for this site, so links here are direct and untracked for now. This article is educational and is not financial advice; automated trading carries its own additional risks beyond manual trading. See our disclaimer for the full risk warning.

Risk warning

Trading crypto assets, and leveraged products based on them such as perpetual futures ("perps"), is high risk. A large share of retail traders lose money on these products. Leverage magnifies gains and losses alike and can push your loss beyond the amount you put in. Positions can be closed automatically (liquidation) before you get a chance to react, and some of the platforms we cover offer no negative balance protection.

Never trade with money you cannot afford to lose entirely. TradeOnDEX gives no investment advice, no personal recommendation, and no guarantee of any outcome. Every decision you make is your own decision and your own responsibility.

Disclaimer