kitesession

package module
v1.6.0 Latest Latest
Warning

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

Go to latest
Published: May 31, 2025 License: MIT Imports: 11 Imported by: 3

README

Kite Session client for Zerodha

The Go package to generate session tokens automatically for using with KiteConnect and Kiteticker API. Can be used as a standalone library in other Go apps. If API Key and API Secret are available then generates access_token else generates enctoken

Usage Instructions as a standalone library

Required
  • KITE_USER_ID : Your kite user_id
  • KITE_PASSWORD : Your kite password
  • KITE_TOTP_SECRET : Its a value which you can copy while setting your external 2FA Authentication
  • KITE_API_KEY : API Key obtained using Kite Developer account
  • KITE_API_SECRET : API Secret obtained using Kite Developer account
Obtaining Two FA Secret
  • Set up External 2FA Auth by going to "My Profile > Settings > Account Security > External 2FA TOTP" and copy the value, while setting.

Import

go get github.com/nsvirk/gokitesession

Sample code

See `examples/main.go`

Sample .env file

KITE_USER_ID=
KITE_PASSWORD=
KITE_TOTP_SECRET=
KITE_API_KEY=
KITE_API_SECRET=

Documentation

Index

Constants

View Source
const (
	LoginURL         = "https://kite.zerodha.com/api/login"
	TwoFAURL         = "https://kite.zerodha.com/api/twofa"
	APIURL           = "https://api.kite.trade"
	ConnectLoginURL  = "https://kite.zerodha.com/connect/login"
	ConnectFinishURL = "https://kite.zerodha.com/connect/finish"
	SessionTokenURL  = "https://api.kite.trade/session/token"
	UserProfileURL   = "https://kite.zerodha.com/oms/user/profile"
	TimeFormat       = "2006-01-02 15:04:05"
	Timezone         = "Asia/Kolkata"
	Timeout          = 7 * time.Second
)

Constants for Kite API URLs and headers

Variables

This section is empty.

Functions

This section is empty.

Types

type KiteErrorResponse added in v1.6.0

type KiteErrorResponse struct {
	Status    string `json:"status"`
	Message   string `json:"message"`
	Data      any    `json:"data"`
	ErrorType string `json:"error_type"`
}

KiteErrorResponse is the error response from the kite API

type KiteSession added in v1.4.0

type KiteSession struct {
	UserID        string         `json:"user_id"`
	UserName      string         `json:"user_name,omitempty"`
	UserShortname string         `json:"user_shortname,omitempty"`
	PublicToken   string         `json:"public_token,omitempty"`
	KFSession     string         `json:"kf_session,omitempty"`
	Enctoken      string         `json:"enctoken,omitempty"`
	LoginTime     string         `json:"login_time,omitempty"`
	UserType      string         `json:"user_type,omitempty"`
	Email         string         `json:"email,omitempty"`
	Broker        string         `json:"broker,omitempty"`
	Exchanges     []string       `json:"exchanges,omitempty"`
	Products      []string       `json:"products,omitempty"`
	OrderTypes    []string       `json:"order_types,omitempty"`
	AvatarURL     string         `json:"avatar_url,omitempty"`
	APIKey        string         `json:"api_key,omitempty"`
	AccessToken   string         `json:"access_token,omitempty"`
	RefreshToken  string         `json:"refresh_token,omitempty"`
	Meta          map[string]any `json:"meta,omitempty"`
}

KiteSession represents the session data returned by the Kite API

type KiteSessionClient added in v1.6.0

type KiteSessionClient struct {
	UserID           string
	Password         string
	TOTPSecret       string
	APIKey           string
	APISecret        string
	Enctoken         string
	AccessToken      string
	KiteSessionError *KiteSessionError
	// contains filtered or unexported fields
}

KiteSessionClient represents the client for the Kite API

func NewClient added in v1.6.0

func NewClient() (*KiteSessionClient, error)

NewClient creates a new KiteSessionClient

func (*KiteSessionClient) GenerateSession added in v1.6.0

func (c *KiteSessionClient) GenerateSession(userId, password, totpSecret, apiKey, apiSecret string) (*KiteSession, error)

type KiteSessionError added in v1.6.0

type KiteSessionError struct {
	ErrorCode int    `json:"error_code"`
	ErrorType string `json:"error_type"`
	Message   string `json:"message"`
}

KiteSessionError represents an error returned by the Kite API

func (*KiteSessionError) Error added in v1.6.0

func (e *KiteSessionError) Error() string

Error returns a string representation of the error

type LoginResponse added in v1.6.0

type LoginResponse struct {
	Status string `json:"status"`
	Data   struct {
		UserID      string   `json:"user_id"`
		RequestID   string   `json:"request_id"`
		TwofaType   string   `json:"twofa_type"`
		TwofaTypes  []string `json:"twofa_types"`
		TwofaStatus string   `json:"twofa_status"`
		Profile     struct {
			UserName      string `json:"user_name"`
			UserShortname string `json:"user_shortname"`
			AvatarURL     any    `json:"avatar_url"`
		} `json:"profile"`
	} `json:"data"`
}

LoginResponse is the response from the login API

type SessionTokenResponse added in v1.6.0

type SessionTokenResponse struct {
	Status string `json:"status"`
	Data   struct {
		UserType      string   `json:"user_type"`
		Email         string   `json:"email"`
		UserName      string   `json:"user_name"`
		UserShortname string   `json:"user_shortname"`
		Broker        string   `json:"broker"`
		Exchanges     []string `json:"exchanges"`
		Products      []string `json:"products"`
		OrderTypes    []string `json:"order_types"`
		AvatarURL     any      `json:"avatar_url"`
		UserID        string   `json:"user_id"`
		APIKey        string   `json:"api_key"`
		AccessToken   string   `json:"access_token"`
		PublicToken   string   `json:"public_token"`
		RefreshToken  string   `json:"refresh_token"`
		Enctoken      string   `json:"enctoken"`
		LoginTime     string   `json:"login_time"`
		Meta          struct {
			DematConsent string `json:"demat_consent"`
		} `json:"meta"`
	} `json:"data"`
}

SessionTokenResponse is the response from the session token API

type TwoFAResponse added in v1.6.0

type TwoFAResponse struct {
	Status string `json:"status"`
	Data   struct {
		Profile struct {
		} `json:"profile"`
	} `json:"data"`
}

TwoFAResponse is the response from the twofa API

type UserProfileResponse added in v1.6.0

type UserProfileResponse struct {
	Status string `json:"status"`
	Data   struct {
		UserID        string   `json:"user_id"`
		UserType      string   `json:"user_type"`
		Email         string   `json:"email"`
		UserName      string   `json:"user_name"`
		UserShortname string   `json:"user_shortname"`
		Broker        string   `json:"broker"`
		Exchanges     []string `json:"exchanges"`
		Products      []string `json:"products"`
		OrderTypes    []string `json:"order_types"`
		AvatarURL     any      `json:"avatar_url"`
		Meta          struct {
			DematConsent string `json:"demat_consent"`
		} `json:"meta"`
	} `json:"data"`
}

UserProfileResponse is the response from the user profile API

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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