firstock-developer-sdk-golang

module
v1.4.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 28, 2026 License: MIT

README

The Firstock Developer API Golang client -

To communicate with the Firstock Developer API using Golang, you can use the official Golang client library provided by Firstock. Licensed under the MIT License.

[Version - 1.4.5]

Documentation

  • Golang client documentation

Installing the client

go get firstock

API usage


// Login
loginRequest := Firstock.LoginRequest{
		UserId:     userId,
		Password:   password,
		TOTP:       totp,
		VendorCode: vendorCode,
		APIKey:     apiKey,
	}
login, err := Firstock.Login(loginRequest)
fmt.Println("Error:", err)
fmt.Println("Result:", login)

// Logout
logout, err := Firstock.Logout(userId)
fmt.Println("Error:", err)
fmt.Println("Result:", logout)

// UserDetails
userDetails, err := Firstock.UserDetails(userId)
fmt.Println("Error:", err)
fmt.Println("Result:", userDetails)

// Place Order
placeOrderRequest := Firstock.PlaceOrderRequest{
		UserId:          userId,
		Exchange:        exchange,
		Retention:       retention,
		Product:         product,
		PriceType:       priceType,
		TradingSymbol:   tradingSymbol,
		TransactionType: transactionType,
		Price:           price,
		TriggerPrice:    triggerPrice,
		Quantity:        quantity,
		Remarks:         remarks,
	}
placeOrder, err := Firstock.PlaceOrder(placeOrderRequest)
fmt.Println("Error:", err)
fmt.Println("Result:", placeOrder)

//Place After Market Order
placeAMORequest := Firstock.PlaceAMORequest{
	UserId:          userId,
	ExchangeSegment: "NSE",
	OrdDuration:     "DAY",
	Product:         "C",
	OrderType:       "LMT",
	TrdSymbol:       "IDEA-EQ",
	TransType:       "B",
	Price:           "9.20",
	TriggerPrice:    "0",
	Quantity:        "1",
	OrdRemarks:      "AMO Order",
}
placeAmo, err := Firstock.PlaceAMO(placeAMORequest)
fmt.Println("Error:", err)
fmt.Println("Result:", placeAmo)

modifyAMORequest := Firstock.ModifyAMORequest{
	UserId:      userId,
	NorenOrdNum: "26031900017331",
	OrderType:   "LMT",
}
modifyAmo, err := Firstock.ModifyAMO(modifyAMORequest)
fmt.Println("Error:", err)
fmt.Println("Result:", modifyAmo)

placeGttRequest := Firstock.GTT_Params{
		UserId:       userId,
		TrdSymbol:    "IDEA-EQ",
		Exchange:     "NSE",
		Validity:     "GTT",
		Value:        "880",
		VariableName: "x",
		Token:        "14366",
		Remarks:      "GTT",
		Ltp:          "8.80",
		OrderParams: &Firstock.PlaceOrderParams{
			ExchangeSegment: "NSE",
			OrdDuration:     "DAY",
			Product:         "C",
			OrderType:       "SL-LMT",
			TrdSymbol:       "IDEA-EQ",
			TransType:       "B",
			Price:           "8.90",
			TriggerPrice:    "8.00",
			Quantity:        "10",
			OrdRemarks:      "Test",
		},
	}
placeGttOrder, err := Firstock.PlaceGttOrder(placeGttRequest)
fmt.Println("Error:", err)
fmt.Println("Result:", placeGttOrder)

modifyGttRequest := Firstock.GTT_Params{
	UserId:       userId,
	TrdSymbol:    "IDEA-EQ",
	Exchange:     "NSE",
	Validity:     "GTT",
	Value:        "880",
	VariableName: "x",
	Token:        "14366",
	Remarks:      "GTT",
	Ltp:          "8.80",
	GTTid:        "26032400000074",
	OrderParams: &Firstock.PlaceOrderParams{
		ExchangeSegment: "NSE",
		OrdDuration:     "DAY",
		Product:         "C",
		OrderType:       "SL-LMT",
		TrdSymbol:       "IDEA-EQ",
		TransType:       "B",
		Price:           "8.90",
		TriggerPrice:    "8.00",
		Quantity:        "10",
		OrdRemarks:      "Test",
		},
	}
modifyGttOrder, err := Firstock.ModifyGttOrder(modifyGttRequest)
fmt.Println("Error:", err)
fmt.Println("Result:", modifyGttOrder)

cancelGttRequest := Firstock.Cancel_GTT_Params{
	UserId: userId,
	GTTid:  "26032400000074",
	}
cancelGttOrder, err := Firstock.CancelGttOrder(cancelGttRequest)
fmt.Println("Error:", err)
fmt.Println("Result:", cancelGttOrder)

gttOrderBook, err := Firstock.GttOrderBook(userId)
fmt.Println("Error:", err)
fmt.Println("Result:", gttOrderBook)

// Order Margin
orderMarginRequest := Firstock.OrderMarginRequest{
		UserId:          userId,
		Exchange:        exchange,
		TransactionType: transactionType,
		Product:         product,
		TradingSymbol:   tradingSymbol,
		Quantity:        quantity,
		PriceType:       priceType,
		Price:           price,
	}
orderMargin, err := Firstock.OrderMargin(orderMarginRequest)
fmt.Println("Error:", err)
fmt.Println("Result:", orderMargin)

// Order Book
orderBookDetails, err := Firstock.OrderBook(userId)
fmt.Println("Error:", err)
fmt.Println("Result:", orderBookDetails)

// Cancel Order
cancel_order := Firstock.OrderRequest{
		UserId:      userId,
		OrderNumber: order_number,
	}
cancelOrder, err := Firstock.CancelOrder(cancel_order)
fmt.Println("Error:", err)
fmt.Println("Result:", cancelOrder)

// Modify Order
modify_order := Firstock.ModifyOrderRequest{
	UserId:         userId,
	OrderNumber:    "25060500005017",
	PriceType:      "MKT",
	TradingSymbol:  "SAWACA",
	Price:          "",
	TriggerPrice:   "",
	Quantity:       "2",
	Product:        "C",
	Retention:      "DAY",
	Mkt_protection: "0.5",
}
modifyOrder, err := Firstock.ModifyOrder(modify_order)
fmt.Printf("Modify Order:\n%v\n", modifyOrder)

// Single Order History
single_order_history := Firstock.OrderRequest{
		UserId:      userId,
		OrderNumber: order_number,
	}
singleOrderHistory, err := Firstock.SingleOrderHistory(single_order_history)
fmt.Println("Error:", err)
fmt.Println("Result:", singleOrderHistory)

// Trade Book
tradeBook, err := Firstock.TradeBook(userId)
fmt.Println("Error:", err)
fmt.Println("Result:", tradeBook)

// Position Book
positionBookDetails, err := Firstock.PositionBook(userId)
fmt.Println("Error:", err)
fmt.Println("Result:", positionBookDetails)

// Product Conversion
productConversionRequest := Firstock.ProductConversionRequest{
		UserId:          userId,
		TradingSymbol:   "AVANCE",
		Exchange:        "BSE",
		PreviousProduct: "I", // B = Buy, S = Sell
		Product:         "C", // C = Delivery, I = Intraday, M = Margin Intraday (MIS)
		Quantity:        "1", // As string
		 MessageFlag:     "1",
	}

productConversion, err := Firstock.ProductConversion(productConversionRequest)
fmt.Println("Error:", err)
fmt.Println("Result:", productConversion)

// Holdings
holdingsDetails, err := Firstock.Holdings(userId)
fmt.Println("Error:", err)
fmt.Println("Result:", holdingsDetails)

// Holdings Details
holdingsDetails, err := Firstock.HoldingsDetails(userId)
fmt.Println("Error:", err)
fmt.Println("Result:", holdingsDetails)

// Limit
rmsLimitDetails, err := Firstock.RMSLmit(userId)
fmt.Println("Error:", err)
fmt.Println("Result:", rmsLimitDetails)

// Basket Margin
basketMarginRequest := Firstock.BasketMarginRequest{
		UserId:          userId,
		Exchange:        "NSE",
		TransactionType: "B",           // B = Buy, S = Sell
		Product:         "C",           // C = Delivery, I = Intraday, M = Margin Intraday (MIS)
		TradingSymbol:   "RELIANCE-EQ", // Ensure it's the correct symbol
		Quantity:        "1",           // As string
		PriceType:       "MKT",         // Example: "LMT" for Limit, "MKT" for Market
		Price:           "0",           // As string
		BasketListParams: []Firstock.BasketListParam{
			{
				Exchange:        "NSE",
				TransactionType: "B",
				Product:         "C",
				TradingSymbol:   "IDEA-EQ",
				Quantity:        "1",
				PriceType:       "MKT",
				Price:           "0",
			},
		},
	}

basketMargin, err := Firstock.BasketMargin(basketMarginRequest)
fmt.Println("Error:", err)
fmt.Println("Result:", basketMargin)

// Brokerage Calculator 
brokerageCalculatorRequest := Firstock.BrokerageCalculatorRequest{
		UserId:          userId,
		Exchange:        "NSE",
		TradingSymbol:   "SAWACA",
		TransactionType: "B",
		Product:         "C",
		Quantity:        "1",
		Price:           "0.50",
		StrikePrice:     "0.00",
		InstName:        "EQ",
		LotSize:         "1",
	}
brokerageCalculator, err := Firstock.BrokerageCalculator(brokerageCalculatorRequest)
fmt.Println("Error:", err)
fmt.Println("Result:", brokerageCalculator)

// Get Security Info
getSecurityInfo := Firstock.GetInfoRequest{
		UserId:        userId,
		Exchange:      "NSE",
		TradingSymbol: "NIFTY",
	}

getSecurityInfoDetails, err := Firstock.GetSecurityInfo(getSecurityInfo)
fmt.Println("Error:", err)
fmt.Println("Result:", getSecurityInfoDetails)

// Get Quote
getQuoteReq := Firstock.GetInfoRequest{
		UserId:        userId,
		Exchange:      "NSE",
		TradingSymbol: "NIFTY",
	}

	getQuoteDetails, err := Firstock.GetQuote(getQuoteReq)
	fmt.Println("Error:", err)
	fmt.Println("Result:", getQuoteDetails)

// Get Quote LTP
getQuoteLtpReq := Firstock.GetInfoRequest{
		UserId:        userId,
		Exchange:      "NSE",
		TradingSymbol: "NIFTY",
	}

getQuoteDetails, err := Firstock.GetQuoteLtp(getQuoteLtpReq)
fmt.Println("Error:", err)
fmt.Println("Result:", getQuoteDetails)

// Get Multi Quotes
	getMultiQuotesReq := Firstock.GetMultiQuotesRequest{
		UserId: userId, // replace with actual value
		Data: []Firstock.MultiQuoteData{
			{
				Exchange:      "NSE",
				TradingSymbol: "Nifty 50", // Ensure this matches the broker’s expected format
			},
			{
				Exchange:      "NFO",
				TradingSymbol: "NIFTY03APR25C23500",
			},
		},
	}

getMultiQuotes, err := Firstock.GetMultiQuotes(getMultiQuotesReq)
fmt.Println("Error:", err)
fmt.Println("Result:", getMultiQuotes)

// Get Multi Quote LTP
getMultiQuotesLtpReq := Firstock.GetMultiQuotesRequest{
		UserId: userId, // replace with actual value
		Data: []Firstock.MultiQuoteData{
			{
				Exchange:      "NSE",
	TradingSymbol: "Nifty 50", // Ensure this matches the broker’s expected format
			},
			{
				Exchange:      "NFO",
				TradingSymbol: "NIFTY03APR25C23500",
			},
		},
	}
getMultiQuotesLtp, err := Firstock.GetMultiQuotesLtp(getMultiQuotesLtpReq)
fmt.Println("Error:", err)
fmt.Println("Result:", getMultiQuotesLtp)

// Index List
indexList, err := Firstock.IndexList(userId)
fmt.Println("Error:", err)
fmt.Println("Result:", indexList)

// Get Expiry
getExpiryReq := Firstock.GetInfoRequest{
		UserId:        userId,
		Exchange:      "NSE",
		TradingSymbol: "NIFTY",
	}
getExpiryDetails, err := Firstock.GetExpiry(getExpiryReq)
fmt.Println("Error:", err)
fmt.Println("Result:", getExpiryDetails)

// Option Chain
optionChainRequest := Firstock.OptionChainRequest{
		UserId:      userId,
		Exchange:    "NFO",
		Symbol:      "NIFTY",
		Expiry:      "12JUN25", 
		Count:       "5",       
		StrikePrice: "23150",   
	}
optionChain, err := Firstock.OptionChain(optionChainRequest)
fmt.Println("Error:", err)
fmt.Println("Result:", optionChain)

// Option Chain Greeks
optionChainGreeksRequest := Firstock.OptionChainGreeksRequest{
	UserId:      userId,
	Exchange:    "NFO",
	Symbol:      "RELIANCE",
	Expiry:      "30MAR26", 
	Count:       "1",       
	StrikePrice: "1420",    
}
optionChainGreeks, err := Firstock.OptionChainGreeks(optionChainGreeksRequest)
fmt.Println("Error:", err)
fmt.Println("Result:", optionChainGreeks)

// Basket Order
basketOrderRequest := Firstock.BasketOrderParams{
	UserId: userId,
	Legs: []Firstock.LegDetails{
		{
			ExchangeSegment:    "NFO",
			Retention:          "DAY",
			Product:            "M",
			PriceType:          "MKT",
			TradingSymbol:      "NIFTY28APR26C23700",
			TransactionType:    "S",
			Price:              "70.00",
			TriggerPrice:       "0",
			Quantity:           "65",
			MktProtectionPrice: "1",
			Remarks:            "seq-leg1-buy-call",
		},
	},
}
basketOrder, err := Firstock.BasketOrder(basketOrderRequest)
fmt.Println("Error:", err)
fmt.Println("Result:", basketOrder)

// Search Scrips
searchScripsRequest := Firstock.SearchScripsRequest{
		UserId: userId,
		SText:  "RELIANCE",
	}
searchScrips, err := Firstock.SearchScrips(searchScripsRequest)
fmt.Println("Error:", err)
fmt.Println("Result:", searchScrips)

// Time Price Series Regular Interval
timePriceSeriesRegularIntervalRequest := Firstock.TimePriceSeriesIntervalRequest{
		UserId:        userId,
		Exchange:      "NSE",
		TradingSymbol: "NIFTY",
		Interval:      "1mi", // 5 minutes interval
		StartTime:     "09:15:00 23-04-2025",
		EndTime:       "15:29:00 23-04-2025",
	}
timePriceSeriesRegularInterval, err := Firstock.TimePriceSeriesRegularInterval(timePriceSeriesRegularIntervalRequest)
fmt.Println("Error:", err)
fmt.Println("Result:", timePriceSeriesRegularInterval)

// Time Price Series Day Interval
timePriceSeriesDayIntervalRequest := Firstock.TimePriceSeriesIntervalRequest{
		UserId:        userId,
		Exchange:      "NSE",
		TradingSymbol: "NIFTY",
		Interval:      "1d", // 5 minutes interval
		StartTime:     "09:15:00 20-04-2025",
		EndTime:       "15:29:00 23-04-2025",
	}
timePriceSeriesDayInterval, err := Firstock.TimePriceSeriesDayInterval(timePriceSeriesDayIntervalRequest)
fmt.Println("Error:", err)
fmt.Println("Result:", timePriceSeriesDayInterval)

//Websockets
model := Firstock.WebSocketModel{
		OrderData:                   orderBookData,
		PositonData:                 positionBookData,
		SubscribeFeedData:           subscribeFeedData,
		SubscribeOptionGreeksData:   subscribeOptionGreeksData,
		SubscribeFeedTokens:         []string{},
		WebSocketConection:          webSocketConnection1,
		SubscribeOptionGreeksTokens: []string{"NSE:26000", "BSE:1"},
	}

err = Firstock.InitializeWebSockets(userId, model)
fmt.Println("Error:", err)
if conn1 != nil {
	err = Firstock.Subscribe(conn1, []string{"NSE:26000", "BSE:1"})
}
fmt.Println("Error:", err)
fmt.Println("Waiting:")
time.Sleep(25 * time.Second)
err = Firstock.Unsubscribe(conn1, []string{"NSE:26000", "BSE:1", "NSE:2885"})
fmt.Println("Unsubscribed")
fmt.Println("Error:", err)
err = Firstock.CloseWebSocket(conn1)
fmt.Println(err)

//CallBack Methods Example

var conn1 *websocket.Conn

func webSocketConnection1(conn *websocket.Conn) {
	if conn != nil {
		conn1 = conn
	}
}

func subscribeFeedData(data Firstock.SubscribeFeedModel) {
	logFile, err := os.OpenFile("websocket.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
	if err != nil {
		fmt.Printf("Error opening log file: %v\n", err)
		return
	}
	defer logFile.Close()

	log.SetOutput(logFile)

	// Now write to the log file
	log.Println(data)
}

func subscribeOptionGreeksData(data Firstock.OptionGreeksModel) {

	logFile, err := os.OpenFile("option_greeks.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
	if err != nil {
		fmt.Printf("Error opening log file: %v\n", err)
		return
	}
	defer logFile.Close()

	log.SetOutput(logFile)

	// Now write to the log file
	log.Println(data)
}

func orderBookData(data map[string]string) {

	logFile, err := os.OpenFile("order_detail.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
	if err != nil {
		fmt.Printf("Error opening log file: %v\n", err)
		return
	}
	defer logFile.Close()

	log.SetOutput(logFile)

	// Now write to the log file
	log.Println(data)
}

func positionBookData(data map[string]interface{}) {

	logFile, err := os.OpenFile("position_detail.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
	if err != nil {
		fmt.Printf("Error opening log file: %v\n", err)
		return
	}
	defer logFile.Close()

	log.SetOutput(logFile)

	// Now write to the log file
	log.Println(data)
}

Refer to the [Firstock Connect Documentation](https://firstock.in/api/docs/)  for the complete list of supported methods.

## Changelog

Check release notes.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL