Daily Zerodha re-authentication runs at 6:00 AM IST via HTTP-only login (no browser). This aligns with Kite Connect’s daily access token expiry. A single scheduled job disconnects streaming, removes the old token, and obtains a fresh access token for the new trading day.

Always-on market data streaming

Whenever a valid Zerodha access token exists in the database, ensureMarketDataRunning() starts (or restarts) the WebSocket feed and financial interval. This runs:
  • After Zerodha login (manual callback, auto-login, or revisiting /login with a valid token)
  • On server restart (via initializeMarketServices()initializeMarketDataIfNeeded())
  • At 6:00 AM auto-login
If streaming is already healthy, later calls (e.g. 9:00 MCX session open) skip redundant restarts. Streaming is not gated on market hours. Ticks may show last-close prices when exchanges are closed. Streaming stops only on:
  • Manual Zerodha logout
  • The 6:00 AM daily token refresh job (or manual logout)
  • Server shutdown (handleZerodhaDisconnect())
Token validation failures during startup or session checks no longer delete the DB token automatically; only explicit logout and the 6 AM pre-login cleanup remove tokens. Order services (scheduled orders, recurring buys) remain gated on exchange session hours and are unaffected by this streaming behavior. After every successful Zerodha connect, handlePostZerodhaLogin() delegates to ensureMarketDataRunning({ forceRestart: true }), which:
  • Validates the access token (with retries on transient network errors; no destructive cleanup on failure)
  • Restarts the WebSocket (restartMarketData)
  • Starts the financial interval (with a fresh financial WebSocket — stale clients are cleared on disconnect)
On disconnect (handleZerodhaDisconnect()), market data, the financial interval, and the financial WebSocket singleton stop immediately.

Startup recovery

If the server restarts (e.g. PM2) and no valid streaming session exists, initializeMarketDataIfNeeded() attempts autoZerodhaLogin() once when credentials are configured. Look for log lines:
  • Startup auto-login succeeded
  • Startup auto-login failed: ...
This does not run while the 6:00 AM refresh job is in progress.

Required environment variables

Add these to the server .env file. Do not commit real values to git. See TOTP setup for configuring ZERODHA_TOTP_SECRET.

Schedule

See Daily refresh schedule for operational context.

Deployment (Ubuntu / PM2)

Environment is loaded via src/env.ts before other modules. PM2 must use the server directory as working directory.
Verify PM2 cwd and env:
Alternative PM2 start (optional): node -r dotenv/config dist/src/app.js with cwd set to the server folder. See PM2 deployment for full backend deploy steps.

Manual test script

This runs auto-login once and restarts streaming immediately on success. Check logs for Market data streaming ensured and no repeating Error in startFinacialInterval: Incorrect api_key or access_token.

Manual test endpoint

Admin-only endpoint for testing without waiting for cron:
Success response:
Verify connection:

Log verification and fallback

See Zerodha troubleshooting for log grep patterns, fallback manual login, and security notes.

Security notes

  • Store credentials only in environment variables or a secrets manager
  • Never log password or TOTP secret values
  • Auto-login uses unofficial Kite web login endpoints; keep manual login as fallback