Your gateway to seamless integration with 5Paisa APIs using .NET
Built with modern tools:
- Overview
- Features
- Installation
- API Key Configuration
- Generate Access Token
- Get TOTP Login
- Usage
- API Reference
- Contributing
The 5Paisa Connect .NET Library offers a convenient way for developers to integrate with 5Paisa trading APIs. This library is designed for seamless stock order placement, retrieval of market data, managing portfolios, and more, empowering developers to build robust trading applications in .NET.
- Stock Order Placement: Easy and efficient way to place, modify, and cancel orders.
- Market Data: Retrieve detailed information about market depth for various scrips.
- Algo Orders Support: Seamless integration with algo trading, allowing algorithmic order placement.
- Real-Time Data: Access real-time stock market data for effective decision-making.
- WebSocket Support: Stream live market data with minimal latency.
- Scrip Master: Fetch scrip details such as codes and instrument information.
Before using the 5Paisa Connect .NET Library, ensure that you have the following installed on your machine:
- .NET SDK: .NET Core 3.1 or higher / .NET 6.0 (recommended). You can download it from here.
- Visual Studio: Version 2019 or later (recommended). You can download it from here.
- Make sure to install the ".NET Desktop Development" workload.
- Git: For version control and to clone/download the library from GitHub. You can download it from here.
-
Clone the repository:
- Open your terminal (Command Prompt, PowerShell, or Git Bash).
- Run the following command to clone the repository:
git clone https://github.com/OpenApi-5p/5pdotnet_new.git cd 5pdotnet_new
-
Build the project to generate the DLL:
- Open the solution file (
5pdotnet_new.sln
) in Visual Studio. - Build the solution by going to Build > Build Solution or pressing
Ctrl+Shift+B
. - The
5Paisaapi-dotnet.dll
will be generated in thebin/Debug
orbin/Release
folder.
- Open the solution file (
-
Add the DLL to your project:
- In Visual Studio, right-click on your project in Solution Explorer.
- Select Add > Reference....
- Browse to the folder where the
5Paisaapi-dotnet.dll
was built (e.g.,5pdotnet_new/bin/Release
). - Select and add the
5Paisaapi-dotnet.dll
file.
-
Download the repository as a ZIP:
- Visit the GitHub repository: 5Paisa Connect .NET.
- Click the green Code button and select Download ZIP.
- Extract the ZIP file to a folder on your system.
-
Build the project:
- Open the solution file (
5pdotnet_new.sln
) in Visual Studio. - Build the solution by going to Build > Build Solution or pressing
Ctrl+Shift+B
. - The
5Paisaapi-dotnet.dll
will be generated in thebin/Debug
orbin/Release
folder.
- Open the solution file (
-
Add the DLL to your project:
- In Visual Studio, right-click on your project in Solution Explorer.
- Select Add > Reference....
- Browse to the folder where the
5Paisaapi-dotnet.dll
was built (e.g.,5pdotnet_new/bin/Release
). - Select and add the
5Paisaapi-dotnet.dll
file.
To use the 5Paisa APIs, you need to configure API keys. Follow these steps to set up your API keys:
- Sign up or log in to your 5Paisa account here.
- Navigate to the developer section in your account settings and generate the following keys:
- API Key
- Encryption Key
- Client Code
- Use the keys in your .NET project:
string APIKey = "your_api_key";
string EncryptionKey = "your_encryption_key";
string EncryptUserId = "your_encrypted_user_id";
string RequestToken = "your_RequestToken";
string ClientCode = "your_ClientCode";
OutputBaseClass obj = new OutputBaseClass();
connect = new _5PaisaAPI(APIKey, EncryptionKey, EncryptUserId);
Token agr = new Token();
To generate an access token through OAUTH GetOuthLogin
method with your request token. Request Token can be obtained via OAUTH login approach mentioned here Here's how you can do it:
// Generate access token using Request Token
var authResponse = connect.GetOuthLogin(RequestToken);
var accessToken = authResponse.TokenResponse;
To generate an access token through TOTP, use the GetOuthLogin
method with your TOTP and PIN and the method will generate access token for you. Here's how you can do it:
// Perform TOTP login using the provided ClientCode, TOTP, and Pin
var response = connect.TOTPLogin(ClientCode, TOTP, Pin);
if (response.status == "0")
{
string requestTokenOTP = response.TokenResponse.RequestToken;
response = connect.GetOuthLogin(requestTokenOTP);
var accessToken = response.TokenResponse;
}
The Place Order API enables users to execute trades through their demat accounts. This API is essential for placing various types of orders securely.
To use this API, you must provide an Access Token (Bearer Token) for authentication.
The API supports the following order types:
- Limit Order
- Market Order
- Stop Loss Order
- Stop Loss – Market Order
- After Market Order
- IOC Order
Orders can be placed as either intraday or delivery.
The API can work with either ScripCode or ScripData. For more details on this api refer to the documentation.
Here’s how to perform an order placement:
// Perform Order placement
OrderInfo order = new OrderInfo();
order.Exchange = '';
order.ExchangeType = '';
order.ScripCode = 0;
order.ScripData = "";
order.Price = 236;
order.OrderType = "";
order.Qty = 1;
order.DisQty = 0;
order.StopLossPrice = 0;
order.IsIntraday = false;
order.iOrderValidity = 0;
order.AppSource = 10345;
order.RemoteOrderID = "";
obj = connect.placeOrder(order);
OrderResponse resOrderbook = obj.PlaceOrderResponse;
The Modify Order API allows users to modify any order that has not yet been successfully executed. You can update fields such as price, quantity, stop loss price, and even change the order type from limit to market or vice versa.
To use this API, you must provide an Access Token (Bearer Token) for authentication, either through OAuth or TOTP.
- You only need to pass the fields that require modification along with the ExchangeOrderID.
- Passing all other fields is optional.
- The ExchangeOrderID is required to identify the order and can be obtained from the order book.
The API requires the ExchangeOrderID for order identification. For more information, refer to the documentation.
Here’s how to modify an order:
// Modify Order
OrderInfo Modifyorder = new OrderInfo();
Modifyorder.Qty = 2;
// Modifyorder.StopLossPrice = "";
// Modifyorder.Price = "";
Modifyorder.ExchOrderID = "";
obj = connect.ModifyOrder(Modifyorder);
OrderResponse Modifyres = obj.PlaceOrderResponse;
The Cancel Order API allows users to cancel an order that has not yet been successfully executed.
To use this API, you must provide an Access Token (Bearer Token) for authentication.
- The API can cancel an order by passing just the ExchangeOrderID.
- The ExchangeOrderID for any order can be fetched from the Order Status, Order Book, or Order WebSocket.
You only need to provide the ExchangeOrderID to cancel an order. For more details, refer to the documentation.
Here’s how to cancel an order:
// Cancel Order
OrderInfo Cancelorder = new OrderInfo();
Cancelorder.ExchOrderID = ClientCode;
obj = connect.CancelOrder(order);
OrderResponse Cancelres = obj.PlaceOrderResponse;
The Order Book API enables users to track orders placed throughout the day. This API provides details on various types of orders, including cash, derivatives, currency, and commodity orders.
To use this API, you must provide an Access Token (Bearer Token) for authentication.
The API responds with a detailed list of orders, including various parameters such as:
- Average Price
- Trigger Rate
- Quantity Traded
- Pending Quantity
Additionally, it provides the status of the API execution along with:
- Remote Order ID
- Broker Order ID
- Exchange Order ID
For more details, refer to the documentation.
Here’s how to fetch the order book:
// Fetch Order Book
OrderInfo orderBook = new OrderInfo();
orderBook.ClientCode = ClientCode;
obj = connect.OrderBook(orderBook);
OrderBookResponse res = obj.OrderBook;
The Trade Book API allows users to track trades executed throughout the day. It fetches the trade book, which contains details for cash, derivatives, currency, and commodity trades across multiple exchanges (NSE, BSE, MCX).
The API provides a detailed list of trades, including:
- Order Rate
- Order Quantity
- Traded Quantity
Since one order can be executed in multiple trades, you can map them using ExchangeOrderId
and ExchangeTradeID
.
For more details, refer to the documentation.
Here’s how to fetch the trade book:
// Fetch Trade Book
OrderInfo TradeBook = new OrderInfo();
TradeBook.ClientCode = ClientCode;
obj = connect.TradeBook(TradeBook);
TradeBookResponse resTradeBook = obj.TradeBook;
The Trade History API helps clients track their trade history, providing a detailed list of trades with various parameters.
To use this API, you must provide an Access Token (Bearer Token) for authentication.
The API returns a list of trade history records, allowing users to view details associated with each trade.
Since this API provides detailed trade history, you can map orders using the ExchOrderID
.
Here’s how to fetch trade history:
// Fetch Trade History
OrderInfo TradeHistory = new OrderInfo()
{
ExchOrderList = new List<ExchOrderIDList>()
};
var ExOrderId = new string[] { "", "", "" };
foreach (var item in ExOrderId)
{
ExchOrderIDList ExchOrderId = new ExchOrderIDList();
ExchOrderId.ExchOrderID = item;
TradeHistory.ExchOrderList.Add(ExchOrderId);
}
TradeHistory.ClientCode = ClientCode;
obj = connect.TradeHistory(TradeHistory);
TradeHistoryResponse resTradeHistory = obj.TradeHistory;
The NetPosition API provides information about open positions in derivatives contracts and intraday stock positions. Overnight derivative positions can be identified using the BodQty
flag, while overnight stock positions will not appear as they are converted into holdings.
To use this API, you must provide an Access Token (Bearer Token) for authentication.
The API returns details about open positions for the client. Overnight derivative positions can be distinguished using the BodQty
flag. Note that overnight stock positions are not displayed as they are converted to holdings.
For more details on this API, refer to the documentation.
Here’s how to fetch net positions:
// Call NetPositionNetWise API
OrderInfo netPositionRequest = new OrderInfo();
netPositionRequest.ClientCode = ClientCode; // Make sure to set the appropriate ClientCode here
// Call the NetPositionNetWise method from your backend API
OutputBaseClass obj = connect.NetPositionNetWise(netPositionRequest);
// Retrieve the response from the API call
NetPositionNetWiseRes res = obj.NetPositionNetWise;
The Historical Data API provides historical candle data for various scrip codes, facilitating strategy deployment based on past trading activity.
Clients must log in to use this API. Upon successful login, a token is generated in the response, which needs to be validated using a JWT validation API.
The API returns the following data:
- OHLC Data: Open, high, low, and close rates.
- Volume Data: Information about the trading volume.
- Timestamps: Time information associated with the provided data.
- Maximum Permissible Interval Size: 6 months.
- Day Wise Data: No restrictions on the size of the interval; maximum data can be fetched when the interval is a day.
- 1 minute
- 5 minutes
- 10 minutes
- 15 minutes
- 30 minutes
- 60 minutes
- Day-based interval
Note: The API allows fetching data for any time duration within the specified interval limits.
For more details on this API, refer to the documentation.
Here’s how to fetch historical data:
string Exch = "";
string ExchType = "";
int Scripcode = 0;
string day = "";
DateTime FromDate = DateTime.Today;
DateTime EndDate = DateTime.Today;
obj = connect.historical(Exch, ExchType, Scripcode, day, FromDate, EndDate);
The Market Feed API is used to fetch the market feed of a particular scrip or a set of scrips.
The response of the API includes details such as:
- LTP: Last Traded Price
- High: Highest price of the requested scrip
- Low: Lowest price of the requested scrip
- Previous Close: Closing price of the previous trading session
The response also contains the status and messages based on the execution of the API.
For more details on this API, refer to the documentation.
Here’s how to fetch the market feed:
OrderInfo MarketFeed = new OrderInfo()
{
MarketFeedData = new List<MarketFeedDataListReq>()
};
string[][] arr = new string[2][];
// Initialize the elements.
arr[0] = new string[4] { "N", "C", "0", "RELIANCE_EQ" };
arr[1] = new string[4] { "N", "C", "0", "RELIANCE_EQ" };
for (int i = 0; i < arr.Length; i++)
{
MarketFeedDataListReq a1 = new MarketFeedDataListReq();
a1.Exch = arr[i][0];
a1.ExchType = arr[i][1];
a1.ScripCode = Convert.ToInt32(arr[i][2]);
a1.ScripData = arr[i][3];
MarketFeed.MarketFeedData.Add(a1);
}
obj = connect.MarketFeed(MarketFeed);
MarketFeedResponse resMarketFeed = obj.MarketFeed;
//End
The Scrip Master API allows users to fetch the scrip details of all equity, derivatives, and commodities for NSE, BSE, and MCX. The data is provided in the form of a CSV dump, which can be imported into a database. The scrip master is regularly updated and can be accessed through the following segments:
- all: Scrips across all segments
- bse_eq: BSE Equity
- nse_eq: NSE Equity
- nse_fo: NSE Derivatives
- bse_fo: BSE Derivatives
- ncd_fo: NSE Currency
- mcx_fo: MCX Commodities
For more details on this API, refer to the documentation.
Here’s how to fetch data from the Scrip Master API:
// Retrieving data from ScripMaster API
string segment = "nse_eq"; // Replace with the desired segment
string scripMasterResponse = string.Empty;
// Call the ScripMaster method
try
{
scripMasterResponse = connect.ScripMaster(segment);
// Check if the response is not null or empty
if (!string.IsNullOrEmpty(scripMasterResponse))
{
// Process the response here (You can do it as per your convenience)
Console.WriteLine("Scrip Master Data:");
Console.WriteLine(scripMasterResponse);
}
else
{
Console.WriteLine("No data received from ScripMaster API.");
}
}
catch (Exception ex)
{
// Handle any exceptions that may occur during the API call
Console.WriteLine($"Error calling ScripMaster API: {ex.Message}");
}
The Web Socket facilitates the integration of live streaming functionality for market data and trade confirmations. It simplifies the trading experience for clients by providing real-time updates and confirmations. This can be easily integrated into your application and operates in an authenticated environment.
- Live Streaming: Fetch market data and trade confirmations in real-time.
- Subscription Model: Clients can subscribe to or unsubscribe from various types of live data streams.
- Authentication: Requires access tokens and client codes for establishing a connection.
To connect to the Web Socket, clients must:
- Connect to the server.
- Subscribe to the desired data types.
The connection request requires the access token and client code as query parameters
For more details on Web Socket, refer to the documentation.
Here’s how to retrieve data from the Web Socket:
// Retrieving data from websocket
WebSocket _WS = new WebSocket();
var exitEvent = new ManualResetEvent(false);
string Acc = "{{AccessToken}}"; // Replace with actual access token
// Connect to the WebSocket for market feed
_WS.ConnectForFeed(Acc, ClientCode);
if (_WS.IsConnected())
{
_WS.MessageReceived += WriteResult;
WebsocketInfo MarketFeed = new WebsocketInfo()
{
MarketFeedData = new List<WebSocketMarketFeedDataListReq>()
};
string[][] arr = new string[3][];
// Initialize the elements
arr[0] = new string[3] { "N", "C", "11536" };
arr[1] = new string[3] { "N", "D", "57919" };
arr[2] = new string[3] { "B", "C", "500325" };
for (int i = 0; i < arr.Length; i++)
{
WebSocketMarketFeedDataListReq a1 = new WebSocketMarketFeedDataListReq();
a1.Exch = arr[i][0];
a1.ExchType = arr[i][1];
a1.ScripCode = Convert.ToInt32(arr[i][2]);
MarketFeed.MarketFeedData.Add(a1);
}
MarketFeed.Method = "MarketFeedV3";
MarketFeed.Operation = "Subscribe";
MarketFeed.ClientCode = ClientCode;
_WS.FetchFeed(MarketFeed);
//_WS.Close();
}
exitEvent.WaitOne();
static void WriteResult(object sender, MessageEventArgs e)
{
Console.WriteLine("Received: " + e.Message);
}
For a complete reference to all available APIs, please refer to the official 5Paisa API documentation:
- 5Paisa API Documentation Explore detailed guides, request and response structures, and example use cases.
We welcome all types of contributions to make this project better. Whether you're reporting issues, suggesting new features, or improving documentation, your feedback helps us grow. Here's how you can contribute:
If you come across any bugs, errors, or inconsistencies in the library or documentation, you can help us by:
- Opening an Issue:
- Go to the Issues tab of the repository.
- Click on New Issue and provide detailed information, including:
- A clear description of the problem.
- Steps to reproduce the issue (if applicable).
- Screenshots, logs, or error messages that illustrate the issue (if possible).
- Your environment setup, such as the .NET version and any relevant configurations.
If you have an idea for a new feature or enhancement, we encourage you to share it with us:
- Open an Enhancement Request:
- Create a new issue with the Enhancement label and describe the feature in detail.
- Explain the benefit of the feature and how it can improve the library's functionality.
- Provide example use cases or relevant documentation links that could help clarify the feature.
We value contributions that improve the clarity and comprehensiveness of our documentation:
- Propose Documentation Improvements:
- If you find sections of the documentation that are unclear, outdated, or lacking in detail, open an issue to suggest improvements.
- You can also recommend additional sections or topics you feel should be covered in the documentation.
- If you'd like to provide more structured feedback on the documentation (e.g., clarity, readability, and technical accuracy), feel free to include that in your issue description.
Your feedback on the overall user experience is important to us:
- General Feedback:
- If you have suggestions for improving how users interact with the library (e.g., the ease of setup, API usability, or overall experience), please share them by opening a new issue or participating in existing discussions.
- You can also leave comments on how the project can be more accessible to different skill levels.
We encourage users to actively participate in discussions within the repository:
- Join Ongoing Discussions:
- Participate in open issues or discussions to share your insights and suggestions.
- Help other users by providing your perspective or suggesting potential solutions to the problems they raise.
To foster a positive and collaborative environment, we ask all contributors to:
- Be respectful and constructive in your feedback and suggestions.
- Keep discussions focused on improving the project and addressing real issues.
By contributing in any of the above ways, you're helping us create a better experience for everyone!