kite-auto-login
v1.0.0
Published
Zero-dependency automated login for Zerodha Kite Connect. Handles credentials, TOTP, and session exchange — returns an access_token.
Maintainers
Readme
kite-auto-login
Zero-dependency automated login for Zerodha Kite Connect. Handles credentials, TOTP 2FA, and session exchange — returns an access_token.
Kite tokens expire daily at 6:00 AM IST. This package automates the full re-authentication flow so you don't have to manually log in through the browser every trading day.
Install
npm install kite-auto-loginRequires Node.js 18+ (uses built-in fetch and crypto).
Usage
import { kiteLogin } from "kite-auto-login";
const session = await kiteLogin({
apiKey: "your_kite_api_key",
apiSecret: "your_kite_api_secret",
userId: "AB1234",
password: "your_zerodha_password",
totpSecret: "YOUR_BASE32_TOTP_SECRET",
});
console.log(session.accessToken); // Use with KiteConnect SDK or raw API calls
console.log(session.expiresAt); // Next 6:00 AM ISTWith the KiteConnect SDK
import { KiteConnect } from "kiteconnect";
import { kiteLogin } from "kite-auto-login";
const session = await kiteLogin({ /* credentials */ });
const kc = new KiteConnect({ api_key: "your_api_key" });
kc.setAccessToken(session.accessToken);
const holdings = await kc.getHoldings();Daily cron (e.g. with node-cron)
import cron from "node-cron";
import { kiteLogin } from "kite-auto-login";
// Run at 8:55 AM IST (3:25 UTC) every weekday
cron.schedule("25 3 * * 1-5", async () => {
try {
const session = await kiteLogin({ /* credentials */ });
console.log(`Token refreshed, expires ${session.expiresAt.toISOString()}`);
// Store session.accessToken in your DB, Redis, etc.
} catch (err) {
console.error("Auto-login failed:", err);
}
});Getting your TOTP secret
The TOTP secret is the base32 seed your authenticator app uses to generate 6-digit codes.
- Go to console.zerodha.com → Settings → Security
- Reset your TOTP (you'll need to re-setup 2FA)
- When the QR code appears, click "Can't scan? Show key"
- Copy the base32 string (looks like
MSMMH3G44BS42MI3...) - Also scan the QR with your authenticator app so manual login still works
API
kiteLogin(credentials): Promise<KiteSession>
| Parameter | Type | Description |
|-----------|------|-------------|
| apiKey | string | Kite Connect API key |
| apiSecret | string | Kite Connect API secret |
| userId | string | Zerodha client ID (e.g. "AB1234") |
| password | string | Zerodha password |
| totpSecret | string | Base32 TOTP secret from 2FA setup |
Returns:
| Field | Type | Description |
|-------|------|-------------|
| accessToken | string | Access token for Kite API calls |
| publicToken | string | Public token for non-sensitive operations |
| userId | string | Zerodha user ID |
| expiresAt | Date | Token expiry (next 6:00 AM IST) |
KiteLoginError
Thrown on failure with:
step:"login"|"twofa"|"redirect"|"session"— which step faileddetails: Raw response data from Zerodha
try {
await kiteLogin({ /* ... */ });
} catch (err) {
if (err instanceof KiteLoginError) {
console.error(`Failed at step: ${err.step}`, err.details);
}
}generateTOTP(secret): string
Exported for advanced use. Generates a 6-digit TOTP code from a base32 secret.
import { generateTOTP } from "kite-auto-login";
const code = generateTOTP("YOUR_BASE32_SECRET");License
MIT
