README
¶
NEPSE Go Library
A modern, type-safe Go client library for the NEPSE (Nepal Stock Exchange) API. This library provides comprehensive access to NEPSE market data with clean architecture, proper error handling, and full type safety.
Features
- ✅ Clean Architecture - Modular design with clear separation of concerns
- ✅ Broad Coverage - Major NEPSE market data endpoints supported
- ✅ Automatic Authentication - Token management handled transparently
- ✅ Retry Logic - Built-in retry with exponential backoff
- ✅ Context Support - Full context.Context support for cancellation and timeouts
- ✅ Error Handling - Structured error types with proper error chains
- ✅ Convenience Methods - High-level methods for common operations
Installation
go get github.com/voidarchive/nepseauth
Quick Start
package main
import (
"context"
"fmt"
"log"
"github.com/voidarchive/nepseauth/nepse"
)
func main() {
// Create a new NEPSE client
client, err := nepse.NewClientWithDefaults()
if err != nil {
log.Fatalf("Failed to create NEPSE client: %v", err)
}
defer client.Close(context.Background())
ctx := context.Background()
// Get market summary
summary, err := client.GetMarketSummary(ctx)
if err != nil {
log.Fatalf("Failed to get market summary: %v", err)
}
fmt.Printf("Total Turnover: Rs. %.2f\\n", summary.TotalTurnover)
fmt.Printf("Total Transactions: %.0f\\n", summary.TotalTransactions)
// Find a company by symbol
security, err := client.FindSecurityBySymbol(ctx, "NABIL")
if err != nil {
log.Fatalf("Failed to find NABIL: %v", err)
}
// Get company details - can use either ID or symbol
details, err := client.GetCompanyDetailsBySymbol(ctx, "NABIL")
if err != nil {
log.Fatalf("Failed to get company details: %v", err)
}
fmt.Printf("Company: %s, Sector: %s, LTP: Rs. %.2f\\n",
details.SecurityName, details.SectorName, details.LastTradedPrice)
}
API Coverage
Market Data
GetMarketSummary()- Overall market statisticsGetMarketStatus()- Current market open/close statusGetNepseIndex()- NEPSE main index informationGetNepseSubIndices()- All sector sub-indicesGetLiveMarket()- Live market dataGetSupplyDemand()- Supply and demand information
Securities & Companies
GetSecurityList()- All listed securitiesGetCompanyList()- All listed companiesGetCompanyDetails(securityID)/GetCompanyDetailsBySymbol(symbol)- Detailed company informationGetSectorScrips()- Securities grouped by sector (fast, no API calls needed)FindSecurity(securityID)/FindSecurityBySymbol(symbol)- Find security by ID or symbol
Price & Trading Data
GetTodaysPrices(businessDate)- Today's price dataGetPriceVolumeHistory(securityID, startDate, endDate)/GetPriceVolumeHistoryBySymbol(symbol, startDate, endDate)- Historical pricesGetMarketDepth(securityID)/GetMarketDepthBySymbol(symbol)- Market depth informationGetFloorSheet()- Complete floor sheet dataGetFloorSheetOf(securityID, businessDate)/GetFloorSheetBySymbol(symbol, businessDate)- Company-specific floor sheet
Top Lists
GetTopGainers()- Top gaining securitiesGetTopLosers()- Top losing securitiesGetTopTenTrade()- Top by trade volumeGetTopTenTransaction()- Top by transaction countGetTopTenTurnover()- Top by turnover
Graph Data (Technical Analysis) - ⚠️ Currently Non-Functional
Note: Graph endpoints currently return empty data due to NEPSE API backend issues.
GetDailyNepseIndexGraph()- NEPSE index chart dataGetDailySensitiveIndexGraph()- Sensitive index chartGetDailyFloatIndexGraph()- Float index chartGetDailyScripPriceGraph(securityID)/GetDailyScripPriceGraphBySymbol(symbol)- Individual security chart
Sector Sub-Index Graphs
GetDailyBankSubindexGraph()- Banking sector indexGetDailyFinanceSubindexGraph()- Finance sector indexGetDailyHydroSubindexGraph()- Hydro sector index- And many more...
Configuration Options
options := &nepse.Options{
BaseURL: "https://www.nepalstock.com",
TLSVerification: true, // Note: May need to be false due to NEPSE server TLS issues
HTTPTimeout: 30 * time.Second,
MaxRetries: 3,
RetryDelay: time.Second,
}
client, err := nepse.NewClient(options)
Important Security Note
The TLSVerification: false option exists due to TLS configuration issues on NEPSE's servers (nepalstock.com). This is a known limitation of the NEPSE API infrastructure, not the client library. When NEPSE fixes their TLS configuration, always use TLSVerification: true for production deployments.
Error Handling
The library provides structured error types for better error handling:
import "errors"
data, err := client.GetMarketSummary(ctx)
if err != nil {
var nepseErr *nepse.NepseError
if errors.As(err, &nepseErr) {
switch nepseErr.Type {
case nepse.ErrorTypeTokenExpired:
// Handle token expiration
case nepse.ErrorTypeNetworkError:
// Handle network issues
case nepse.ErrorTypeRateLimit:
// Handle rate limiting
}
}
}
Architecture
The library is organized into several packages:
nepse- Main package with client interface and factory functionsnepse/config.go- Configuration and static datanepse/errors.go- Structured error typesnepse/types.go- All API response typesnepse/client.go- Client interface definitionnepse/http_client.go- HTTP client implementationnepse/market_data.go- GET API methodsnepse/graphs.go- GET API methods for graph data
Key Differences from Python Version
- Type Safety - All responses are properly typed structs instead of generic maps
- Clean Architecture - Separated into logical modules instead of one giant file
- Modern Error Handling - Structured errors with proper error chaining
- Context Support - Full context.Context support throughout
- Interface-based Design - Clean interfaces for testing and extensibility
- Resource Management - Proper connection pooling and cleanup
Examples
See cmd/examples for usage examples:
cmd/examples/basic_usage.go- Basic API usage examplesmain.go- Simple NABIL summary demo
Testing
Against Real NEPSE API
# Run the main test with NABIL data
go run main.go
# Run the basic usage example
go run cmd/examples/basic_usage.go
Notes
- Real API calls require valid NEPSE tokens. The library handles token mint/refresh using an embedded WASM parser.
- A separate mock server and OpenAPI spec are not included in this repository at the moment.
API Design Philosophy
This library follows modern Go best practices:
Type-Safe Dual API Pattern
Most methods that work with securities provide both ID and symbol variants:
// Primary methods (faster, direct)
client.GetMarketDepth(ctx, int32(131)) // By ID
client.GetCompanyDetails(ctx, int32(131)) // By ID
// Convenience methods (easier to use)
client.GetMarketDepthBySymbol(ctx, "NABIL") // By symbol
client.GetCompanyDetailsBySymbol(ctx, "NABIL") // By symbol
Performance Optimizations
- Fast Sector Grouping:
GetSectorScrips()uses existing data, no API calls needed - Connection Pooling: Efficient HTTP connection reuse
- Smart Retry Logic: Exponential backoff with circuit breaking
Contributing
- Follow Go best practices and conventions
- Maintain type safety - no
interface{}oranyin public APIs - Add proper error handling for all new features
- Include examples for new functionality
- Update documentation for API changes
- Security First: All contributions must pass security review
Go Version
- Requires Go 1.23+
License
This project is licensed under the MIT License - see the LICENSE file for details.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package auth: Authenticate Nepse API
|
Package auth: Authenticate Nepse API |
|
cmd
|
|
|
examples
command
|
|
|
http-server
command
|
|
|
Package nepse provides a modern, type-safe Go client for the NEPSE (Nepal Stock Exchange) API.
|
Package nepse provides a modern, type-safe Go client for the NEPSE (Nepal Stock Exchange) API. |