Place orders, stream quotes, and manage portfolios using Node.js. Start with these minimal examples.
// Node 18+ (native fetch)
const tokenRes = await fetch('https://api.paperinvest.io/v1/auth/token', {
method: 'POST', headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ apiKey: 'YOUR_API_KEY' })
});
const { access_token } = await tokenRes.json();
await fetch('https://api.paperinvest.io/v1/orders', {
method: 'POST',
headers: { 'Authorization': `Bearer ${access_token}`, 'Content-Type': 'application/json' },
body: JSON.stringify({
accountId: 'your-account-id', portfolioId: 'your-portfolio-id',
symbol: 'TSLA', quantity: 5, side: 'buy', type: 'market', timeInForce: 'day'
})
});
import WebSocket from 'ws';
const ws = new WebSocket('wss://api.paperinvest.io/v1/market-data/stream', {
headers: { Authorization: 'Bearer YOUR_JWT' }
});
ws.on('open', () => {
ws.send(JSON.stringify({ action: 'subscribe', symbols: ['AAPL','MSFT'] }));
});
ws.on('message', (msg) => {
const data = JSON.parse(msg.toString());
if (data.type === 'quote') console.log(data);
});
ws.on('close', () => { /* reconnect */ });
ws.on('error', (e) => console.error('ws error', e));
Node 18+ is recommended for built-in fetch and modern WebSocket support.
SDK is planned. Until then, use native fetch/WebSocket as shown in the examples.
Implement exponential backoff and resubscribe logic on socket close/error.