A promise-based Node.js REST API wrapper for Salesforce, a modernized fork of Kevin O'Hara's nforce library.
- Node.js >= 22.4.0 — uses built-in
fetchand a stable built-inWebSocket(Node’s globalWebSocketwas experimental in 22.0–22.3)
- Promise-based API (no callback support)
- Intelligent sObjects with field change tracking
- CRUD operations (insert, update, upsert, delete, getRecord)
- SOQL queries with automatic pagination (
fetchAll) - SOSL search
- Streaming API support (PushTopics, Platform Events, Change Data Capture)
- Built-in CometD/Bayeux client with long-polling and WebSocket transports
- Replay ID support for event replay
- No external streaming dependencies
- Binary content retrieval (Attachments, Documents, ContentVersions)
- Apex REST endpoint support
- OAuth authentication (authorization code, username/password, SAML assertion)
- Automatic token refresh on session expiration
- Single-user and multi-user modes
- Plugin system for extending the Connection prototype
npm install nforce8
const nforce = require('nforce8'); const org = nforce.createConnection({ clientId: 'YOUR_CLIENT_ID', clientSecret: 'YOUR_CLIENT_SECRET', redirectUri: 'http://localhost:3000/oauth/_callback', apiVersion: 'v62.0', // optional, defaults to current API version environment: 'production', // optional, 'production' or 'sandbox' mode: 'multi' // optional, 'single' or 'multi' user mode });
// Multi-user mode const oauth = await org.authenticate({ username: '[email protected]', password: 'password' }); // Single-user mode — OAuth is cached on the connection await org.authenticate({ username: '[email protected]', password: 'password' });
// Insert const acc = nforce.createSObject('Account'); acc.set('Name', 'ACME Corporation'); acc.set('Phone', '800-555-2345'); const result = await org.insert({ sobject: acc, oauth }); console.log('Created:', result.id); // Query const resp = await org.query({ query: 'SELECT Id, Name FROM Account WHERE Name = \'ACME Corporation\' LIMIT 1', oauth }); // Update (only changed fields are sent) const record = resp.records[0]; record.set('Name', 'ACME Coyote'); record.set('Industry', 'Explosives'); await org.update({ sobject: record, oauth }); // Delete await org.delete({ sobject: record, oauth });
Subscribe to PushTopics, Platform Events, or Change Data Capture events:
const oauth = await org.authenticate(creds); const client = org.createStreamClient(); const sub = client.subscribe({ topic: '/data/ChangeEvents' }); sub.on('data', (event) => console.log(event)); sub.on('connect', () => console.log('Subscribed')); sub.on('error', (err) => { console.error(err); client.disconnect(); });
Replay support — resume from a specific event replay ID:
const sub = client.subscribe({ topic: '/event/MyPlatformEvent__e', replayId: -2 // -1 = new only, -2 = all available });
const result = await org.apexRest({ uri: 'MyCustomEndpoint', method: 'POST', body: { key: 'value' }, oauth });
// Retrieve attachment, document, or content version binary data const buffer = await org.getBinaryContent({ sobject: attachmentRecord, oauth });
API versions must be fully-qualified strings like 'v62.0'. Bare numbers (42, 42.0) and short strings ('v42') are rejected.
- Multi-User Mode (default): pass
oauthwith each API call - Single-User Mode: OAuth is cached on the connection after
authenticate(), no need to pass it
- Promise-only API, no callback support
- API version must be fully-qualified (
'v45.0', not42or'42') - Streaming subscriptions require the full topic path (e.g.
/topic/MyTopic) - Requires Node.js >= 22.4.0 (stable built-in
WebSocket; experimental in 22.0–22.3) - Built-in CometD client replaces the faye dependency
- Changelog
- Streaming API guide
- Original nforce documentation (for inherited API details)
# Run tests npm test # Lint npm run lint
See LICENSE file.