npm package: 5paisa-ts-sdk
Description: Modern TypeScript SDK for 5paisa Trading APIs
Latest version: 1.0.0

# 5paisa TypeScript SDK

A **modern, lightweight, TypeScript-first SDK** for interacting with **5paisa Trading APIs**.
Designed for **Node.js 18+**, with strong typing, modular services, and zero deprecated dependencies.

---

## ✨ Features

- ✅ TypeScript-first with `.d.ts` support
- ✅ No deprecated libraries (`axios`, `request`, `node-pandas` removed)
- ✅ Clean service-based API (`auth`, `orders`, `market`)
- ✅ Native `fetch` (Node.js 18+)
- ✅ Safe payload builders (no shared mutable state)
- ✅ GitHub & npm install support

---

## 📦 Installation

### From GitHub (recommended during development)

```bash
npm install git+https://github.com/AmitManna99/5paisa-ts-sdk.git
```

### From npm (once published)

```bash
npm install 5paisa-ts-sdk
```

---

## ⚙️ Requirements

- **Node.js >= 18**
- 5paisa API credentials
  (Get them from: [https://tradestation.5paisa.com/apidoc](https://tradestation.5paisa.com/apidoc))

---

## 🚀 Quick Start

### 1️⃣ Create Client

```ts
import { FivePaisaClient } from "5paisa-ts-sdk";

const client = new FivePaisaClient({
  appName: "my-app",
  userId: "YOUR_USER_ID",
  password: "YOUR_PASSWORD",
  userKey: "YOUR_VENDOR_KEY",
  encryptionKey: "YOUR_ENCRYPTION_KEY",
  clientCode: "YOUR_CLIENT_CODE",
  appSource: 1234,
});
```

---

## 🔐 Authentication

### Option 1: Login using TOTP + PIN

```ts
const requestToken = await client.auth.getRequestToken("TOTP_CODE", "PIN");

await client.auth.getAccessToken(requestToken);

// Client is now authenticated
```

---

### Option 2: Login using Access Token (recommended for automation)

```ts
await client.auth.setAccessToken("EXISTING_ACCESS_TOKEN");
```

---

### Option 3: OAuth Login (Browser Flow)

1. Redirect user to:

```
https://dev-openapi.5paisa.com/WebVendorLogin/VLogin/Index
  ?VendorKey=<YOUR_USER_KEY>
  &ResponseURL=<YOUR_REDIRECT_URL>
```

2. Extract `RequestToken` from redirect URL
3. Exchange it for access token:

```ts
await client.auth.getAccessToken(requestToken);
```

---

## 📊 Holdings & Positions

### Fetch Holdings

```ts
const holdings = await client.orders.getHoldings();
console.log(holdings);
```

---

### Fetch Positions

```ts
const positions = await client.orders.getPositions();
console.log(positions);
```

---

## 🧾 Orders

### Place Order

```ts
await client.orders.placeOrder({
  scripCode: 1660,
  qty: 1,
  orderType: "BUY",
  exchange: "N",
  price: 208,
});
```

---

### Modify Order

```ts
await client.orders.modifyOrder({
  exchangeOrderID: "1100000007628729",
  qty: 1,
  price: 210,
  exchange: "N",
  exchangeType: "C",
  isIntraday: false,
});
```

---

### Cancel Order

```ts
await client.orders.cancelOrder("1100000007973827");
```

---

## 🧠 BO / CO Orders

### Place BO-CO Order

```ts
await client.orders.placeBOCOOrder({
  scripCode: 1660,
  qty: 1,
  limitPrice: 205,
  triggerPrice: 0,
  targetPrice: 217,
  side: "BUY",
  exchange: "N",
  exchangeType: "C",
});
```

---

### Modify BO-CO Order (Profit / SL Leg)

```ts
await client.orders.modifyBOCOOrder({
  exchOrderId: "1100000008697274",
  price: 215,
});
```

---

## 📈 Market Data

### Market Feed

```ts
const feed = await client.market.getMarketFeed([
  {
    Exch: "N",
    ExchType: "D",
    Symbol: "NIFTY 27 MAY 2021 CE 14500.00",
    Expiry: "20210527",
    StrikePrice: "14500",
    OptionType: "CE",
  },
]);

console.log(feed);
```

---

### Market Feed by Scrip

```ts
const feed = await client.market.getMarketFeedByScrip([
  {
    Exch: "N",
    ExchType: "C",
    ScripCode: 1660,
    ScripData: "RELIANCE_EQ",
  },
]);
```

---

### Market Depth

```ts
const depth = await client.market.getMarketDepth([
  { Exchange: "N", ExchangeType: "C", ScripCode: 1660 },
]);
```

---

## ⏱ Historical Data

```ts
const candles = await client.market.getHistoricalData({
  exchange: "N",
  exchangeType: "C",
  scripCode: 1660,
  timeframe: "1m",
  from: "2021-05-31",
  to: "2021-06-01",
});
```

Supported timeframes:

```
['1m', '5m', '10m', '15m', '30m', '60m', '1d']
```

---

## 📚 API Structure Overview

```ts
client.auth; // authentication & tokens
client.orders; // holdings, orders, positions
client.market; // market feed, depth, historical data
```

---

## 🛡️ Notes & Best Practices

- Always **store access tokens securely**
- Prefer **access-token login** for long-running services
- SDK expects **Node.js 18+**
- This SDK does **not manage retries / rate limits** yet

---

## 🧪 Development

```bash
npm run dev
```

---

## 📄 License

MIT

---

## 🚧 Roadmap

- Typed API responses
- Runtime validation (Zod)
- Retry & rate-limit handling
- WebSocket feed support
- npm public release