0% found this document useful (0 votes)
6 views

nse-query-documentation

This document provides a guide for implementing an asynchronous JavaScript query to retrieve stock data from the National Stock Exchange (NSE). It details the request flow, code structure, API endpoints, request headers, and response data structure, along with implementation notes on cookie handling and error management. Additionally, it includes usage examples and troubleshooting tips for common issues encountered during implementation.

Uploaded by

anandkrishna1511
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

nse-query-documentation

This document provides a guide for implementing an asynchronous JavaScript query to retrieve stock data from the National Stock Exchange (NSE). It details the request flow, code structure, API endpoints, request headers, and response data structure, along with implementation notes on cookie handling and error management. Additionally, it includes usage examples and troubleshooting tips for common issues encountered during implementation.

Uploaded by

anandkrishna1511
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

NSE Stock Data Query Documentation

JavaScript Implementation Guide


1. Overview
This document outlines the implementation of an asynchronous JavaScript query to
retrieve stock data from the National Stock Exchange (NSE) website.

2. Code Structure and Implementation

2.1 Request Flow


The implementation follows a two-step process: 1. Initial visit to NSE homepage to obtain
necessary cookies 2. Subsequent API call to fetch stock data

2.2 Complete Code Implementation


// First visit NSE homepage to get cookies
fetch('https://www.nseindia.com/', {
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)',
'Accept':
'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/
*;q=0.8'
}
}).then(() => {
// Then fetch stock data
return fetch('https://www.nseindia.com/api/quote-equity?
symbol=BAJFINANCE', {
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)',
'Accept': 'application/json',
'Accept-Language': 'en-US,en;q=0.9',
'Accept-Encoding': 'gzip, deflate, br',
'Referer': 'https://www.nseindia.com/get-quotes/equity?
symbol=BAJFINANCE'
},
credentials: 'include'
});
})
.then(response => response.json())
.then(data => {
const stockInfo = {
symbol: data.info.symbol,
lastPrice: data.priceInfo.lastPrice,
change: data.priceInfo.change,
pChange: data.priceInfo.pChange,
volume: data.preOpenMarket.totalTradedVolume,
lastUpdateTime: data.lastUpdateTime
};
console.table(stockInfo);
return stockInfo;
})
.catch(error => console.error('Error:', error));

3. Technical Details

3.1 API Endpoints


• Homepage URL: https://www.nseindia.com/
• Stock Data API: https://www.nseindia.com/api/quote-equity
• Query Parameter: symbol (Example: BAJFINANCE)

3.2 Request Headers

First Request (Homepage)


{
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)',
'Accept':
'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/
*;q=0.8'
}

Second Request (Stock Data)


{
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)',
'Accept': 'application/json',
'Accept-Language': 'en-US,en;q=0.9',
'Accept-Encoding': 'gzip, deflate, br',
'Referer': 'https://www.nseindia.com/get-quotes/equity?
symbol=BAJFINANCE'
}

3.3 Response Structure


The code extracts the following data points from the API response: - symbol: Stock symbol -
lastPrice: Current stock price - change: Price change - pChange: Percentage change -
volume: Trading volume - lastUpdateTime: Last update timestamp

4. Implementation Notes

4.1 Cookie Handling


• The initial request to the homepage is crucial for obtaining session cookies
• Cookies are automatically included in the second request via credentials:
'include'

4.2 Error Handling


• The implementation includes a catch block for error handling
• Errors are logged to the console for debugging

4.3 Data Processing


• Response is parsed using response.json()
• Relevant data is extracted and structured into a stockInfo object
• Data is displayed using console.table for better readability

5. Usage Example
Expected console output:
┌─────────────────┬────────────────────────────┐
│ Property │ Value │
├─────────────────┼────────────────────────────┤
│ symbol │ BAJFINANCE │
│ lastPrice │ 6789.45 │
│ change │ 45.30 │
│ pChange │ 0.67 │
│ volume │ 1234567 │
│ lastUpdateTime │ 08-Jan-2024 15:30:00 │
└─────────────────┴────────────────────────────┘

6. Important Considerations
1. The code must be run in a browser environment with CORS support
2. Proper internet connectivity is required
3. NSE website should be accessible from the user’s location
4. The stock symbol should be valid and actively traded

7. Troubleshooting
Common issues and solutions: 1. CORS errors: Ensure the request is made from an allowed
origin 2. 401/403 errors: Check if headers are properly set 3. Undefined data: Verify the
stock symbol is correct 4. Cookie issues: Make sure the homepage request completes before
the API call

You might also like