Tradingbot
JavaScript · FREE RESOURCE

TradingView Webhook Receiver

A complete Node.js educational example with error handling and no order execution.

ICODERX Editorialv1.0.0Updated 11/20/20260 saves

Source code

v1.0.0
// ICODERX Trading — MIT. Node.js 22+. Receives messages only; no trading.
import http from 'node:http';
import { timingSafeEqual } from 'node:crypto';
const secret = process.env.WEBHOOK_TOKEN;
if (!secret || secret.length < 32) throw new Error('Set a random WEBHOOK_TOKEN (32+ chars)');
const server = http.createServer((req, res) => {
  const supplied = Buffer.from(String(req.headers.authorization || '').replace(/^Bearer /, ''));
  const expected = Buffer.from(secret);
  if (req.method !== 'POST' || req.url !== '/webhook' || supplied.length !== expected.length || !timingSafeEqual(supplied, expected)) {
    res.writeHead(403).end(); return;
  }
  let body = '';
  req.on('data', chunk => { body += chunk; if (Buffer.byteLength(body) > 4096) req.destroy(); });
  req.on('end', () => {
    try {
      const event = JSON.parse(body);
      if (typeof event.symbol !== 'string' || !/^[A-Z0-9]{3,20}$/.test(event.symbol)) throw new Error('symbol');
      if (!['buy', 'sell'].includes(event.signal)) throw new Error('signal');
      console.log(JSON.stringify({symbol: event.symbol, signal: event.signal, receivedAt: Date.now()}));
      res.writeHead(202).end('Accepted for logging only');
    } catch { res.writeHead(400).end('Invalid payload'); }
  });
});
server.requestTimeout = 10000;
server.listen(8080, '127.0.0.1');
3 files · Read-only · MIT · Source is not executed here

Features and architecture

Loopback HTTP server → Bearer authentication → JSON validation → signal log. No exchange API keys or order execution modules.

Installation

  1. Download ZIP and review every file before running locally.
  2. Install Node.js 22+. Standard library only; no third-party dependencies.
  3. Follow README.md and set the required environment variables. Keep secrets out of source.

Configuration and security

Generate a random token of at least 32 characters. This example expects an Authorization header; a separate relay is needed if the webhook origin cannot set one. Before Internet exposure add TLS, rate limits, replay protection and a durable queue.

Known limitations

No durable state, retry queue or production monitoring. APIs can disconnect or impose regional limits. Do not use this sample as a real-order execution system. This website never executes uploaded source.

Changelog

1.0.0 — first educational release, MIT license.

Educational content, not investment advice. Trading can result in capital loss. No profit is guaranteed.

FAQ

Is the source code free?

Yes. Examples use the MIT license and can be modified and shared with attribution.

Does this guarantee profit?

No. This is educational material. Independently validate assumptions, costs and risks.

Join the discussion