> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/drift-labs/protocol-v2/llms.txt
> Use this file to discover all available pages before exploring further.

# Installation

> Install the Drift Protocol SDK and set up your development environment

# Installation

Get started with Drift Protocol v2 by installing the SDK and setting up your Solana wallet for trading.

## Prerequisites

Before installing the Drift SDK, ensure you have:

<Steps>
  <Step title="Node.js">
    Node.js version 24.0.0 or higher

    ```bash theme={null}
    node --version
    ```
  </Step>

  <Step title="Package Manager">
    npm, yarn, or pnpm installed

    ```bash theme={null}
    npm --version
    # or
    yarn --version
    ```
  </Step>

  <Step title="Solana CLI (Optional)">
    For wallet generation and management

    ```bash theme={null}
    solana --version
    ```

    Install from [Solana's official docs](https://docs.solana.com/cli/install-solana-cli-tools) if needed
  </Step>
</Steps>

## Install the SDK

Install the `@drift-labs/sdk` package in your project:

<CodeGroup>
  ```bash npm theme={null}
  npm install @drift-labs/sdk
  ```

  ```bash yarn theme={null}
  yarn add @drift-labs/sdk
  ```

  ```bash pnpm theme={null}
  pnpm add @drift-labs/sdk
  ```
</CodeGroup>

<Note>
  Current SDK version: **2.158.0-beta.0**
</Note>

## Core Dependencies

The Drift SDK includes these key dependencies:

* `@coral-xyz/anchor` (0.29.0 & 0.30.1) - Solana program framework
* `@solana/web3.js` (1.98.0) - Solana JavaScript API
* `@solana/spl-token` (0.4.13) - SPL token operations
* `bn.js` - BigNumber support for precise calculations

These are automatically installed with the SDK.

## Set Up a Solana Wallet

You'll need a Solana wallet to interact with Drift Protocol.

### Generate a New Keypair

<Steps>
  <Step title="Generate keypair">
    ```bash theme={null}
    solana-keygen new
    ```

    This creates a new keypair at `~/.config/solana/id.json`
  </Step>

  <Step title="Get your wallet address">
    ```bash theme={null}
    solana address
    ```

    Save this address - you'll need it to fund your wallet
  </Step>

  <Step title="Add private key to environment">
    ```bash theme={null}
    cd your-project-directory
    echo BOT_PRIVATE_KEY=`cat ~/.config/solana/id.json` >> .env
    ```
  </Step>
</Steps>

<Warning>
  **Never commit your `.env` file or share your private key!** Add `.env` to your `.gitignore` file.
</Warning>

### Fund Your Wallet

<Tabs>
  <Tab title="Devnet (Testing)">
    For testing on devnet, use the Solana faucet:

    ```bash theme={null}
    solana airdrop 2 YOUR_WALLET_ADDRESS --url devnet
    ```

    You can also get devnet USDC from Drift's faucet after connecting your wallet.
  </Tab>

  <Tab title="Mainnet (Production)">
    For mainnet:

    1. Purchase SOL from an exchange
    2. Transfer SOL to your wallet address
    3. Acquire USDC (you'll need it for collateral)

    <Note>
      You need both SOL (for transaction fees) and USDC (for trading collateral) on mainnet.
    </Note>
  </Tab>
</Tabs>

## Environment Variables

Create a `.env` file in your project root:

```bash .env theme={null}
# Your wallet's private key (as JSON array)
BOT_PRIVATE_KEY=[123,45,67,...]

# Solana RPC endpoint
ANCHOR_PROVIDER_URL=https://api.devnet.solana.com
# For mainnet: https://api.mainnet-beta.solana.com

# Wallet path (optional, defaults to ~/.config/solana/id.json)
ANCHOR_WALLET=/path/to/your/keypair.json
```

<Accordion title="Finding RPC Endpoints">
  You can use public RPC endpoints or premium providers for better performance:

  **Public Endpoints:**

  * Devnet: `https://api.devnet.solana.com`
  * Mainnet: `https://api.mainnet-beta.solana.com`

  **Premium Providers (Recommended for Production):**

  * [Helius](https://www.helius.dev/)
  * [QuickNode](https://www.quicknode.com/)
  * [Triton](https://triton.one/)

  Premium RPC providers offer higher rate limits and better reliability.
</Accordion>

## Verify Installation

Create a simple test script to verify everything is set up correctly:

```typescript test-setup.ts theme={null}
import { Connection, PublicKey } from '@solana/web3.js';
import { initialize } from '@drift-labs/sdk';

const main = async () => {
  // Initialize SDK config
  const sdkConfig = initialize({ env: 'devnet' });
  console.log('SDK initialized for:', 'devnet');
  
  // Connect to Solana
  const connection = new Connection('https://api.devnet.solana.com');
  
  // Check connection
  const version = await connection.getVersion();
  console.log('Connected to Solana cluster:', version);
  
  console.log('\nDrift Program ID:', sdkConfig.DRIFT_PROGRAM_ID);
  console.log('USDC Mint:', sdkConfig.USDC_MINT_ADDRESS);
  
  console.log('\n✅ Installation verified successfully!');
};

main().catch(console.error);
```

Run the script:

<CodeGroup>
  ```bash ts-node theme={null}
  npx ts-node test-setup.ts
  ```

  ```bash tsx theme={null}
  npx tsx test-setup.ts
  ```
</CodeGroup>

<Note>
  You may need to install `ts-node` or `tsx` for running TypeScript files:

  ```bash theme={null}
  npm install -D ts-node typescript
  ```
</Note>

## TypeScript Configuration

For TypeScript projects, ensure your `tsconfig.json` includes:

```json tsconfig.json theme={null}
{
  "compilerOptions": {
    "target": "ES2020",
    "module": "commonjs",
    "lib": ["ES2020"],
    "moduleResolution": "node",
    "esModuleInterop": true,
    "skipLibCheck": true,
    "strict": true,
    "resolveJsonModule": true
  }
}
```

## Next Steps

<CardGroup cols={2}>
  <Card title="Quick Start Guide" icon="rocket" href="/quickstart">
    Place your first trade on Drift Protocol
  </Card>

  <Card title="API Reference" icon="code" href="https://drift-labs.github.io/protocol-v2/sdk/">
    Explore the complete SDK documentation
  </Card>
</CardGroup>

## Troubleshooting

<AccordionGroup>
  <Accordion title="Module not found errors">
    Ensure all peer dependencies are installed:

    ```bash theme={null}
    npm install @solana/web3.js @coral-xyz/anchor @solana/spl-token
    ```
  </Accordion>

  <Accordion title="Connection timeout errors">
    * Check your internet connection
    * Try a different RPC endpoint
    * Consider using a premium RPC provider for better reliability
  </Accordion>

  <Accordion title="BigNum/BN errors">
    The SDK uses `bn.js` for BigNumber operations. Import `BN` from the SDK:

    ```typescript theme={null}
    import { BN } from '@drift-labs/sdk';
    ```
  </Accordion>

  <Accordion title="Node version errors">
    Drift SDK requires Node.js 24.0.0 or higher. Update Node.js:

    ```bash theme={null}
    nvm install 24
    nvm use 24
    ```
  </Accordion>
</AccordionGroup>

## Building from Source

If you want to contribute or build the SDK from source:

```bash theme={null}
# Clone the repository
git clone https://github.com/drift-labs/protocol-v2.git
cd protocol-v2

# Install dependencies
yarn

# Build the SDK
cd sdk/
yarn
yarn build

# Run tests
yarn test
```

<Note>
  For M1 Mac users, set the Rust toolchain first:

  ```bash theme={null}
  rustup default stable-x86_64-apple-darwin
  ```
</Note>
