Skip to main content

Quick Start Guide

This guide will walk you through placing your first perpetual trade on Drift Protocol, from initialization to order execution.
This quickstart uses devnet for testing. No real funds are required!

Overview

You’ll learn how to:
  1. Initialize the Drift SDK
  2. Connect your wallet
  3. Create a Drift user account
  4. Deposit collateral (USDC)
  5. Place a perpetual order
  6. Monitor your position

Complete Example

Here’s a complete example that places a 1 SOL-PERP long order:
trade-example.ts

Step-by-Step Breakdown

Let’s break down each step in detail:

1. Initialize the SDK

The initialize function returns configuration for the specified environment:
  • DRIFT_PROGRAM_ID - The Drift program address
  • USDC_MINT_ADDRESS - The USDC token mint
  • Market configurations and more

2. Set Up Connection and Wallet

Keep your private key secure! Never hardcode it or commit it to version control.

3. Initialize Drift Client

The DriftClient is your main interface to the Drift Protocol.
Drift supports multiple subscription types:
  • Polling: Regular interval updates (good for bots)
  • Websocket: Real-time updates (good for UIs)
  • gRPC: High-performance streaming (requires additional setup)
For most use cases, polling is sufficient and easiest to set up.

4. Create User Account

The first time you use Drift, you need to create a user account by depositing collateral.

5. Place an Order

BASE_PRECISION is 10^9, so new BN(1).mul(BASE_PRECISION) equals 1 SOL

Order Types

Drift supports multiple order types:
Execute immediately at the best available price:

Working with Positions

After placing orders, monitor and manage your positions:

Closing Positions

To close a position, place an order in the opposite direction:
Always use reduceOnly: true when closing positions to avoid accidentally opening a new position in the opposite direction.

Getting Market Data

Access real-time market information:

Best Practices

Never use regular JavaScript numbers for token amounts:
Wrap operations in try-catch blocks:
Always fetch the latest account data:
Prevent accidentally opening opposite positions:

Running Your First Trade

1

Save the code

Copy the complete example to trade-example.ts
2

Set environment variables

Ensure your .env file contains:
3

Get devnet SOL

4

Run the script

What’s Next?

Advanced Trading

Learn about limit orders, stop losses, and advanced strategies

Build a Trading Bot

Explore example bot implementations

API Reference

Complete TypeScript SDK documentation

Risk Management

Learn about margin, liquidations, and risk controls

Common Issues

Ensure you have enough USDC deposited:
Deposit more if needed:
Common causes:
  • Insufficient SOL for transaction fees
  • Slippage too high on large orders
  • Market conditions changed
Try reducing order size or checking your SOL balance.
The user account doesn’t exist yet. Create it: