JSforce Salesforce API Library

3.10.14 · active · verified Sun Apr 19

JSforce (formerly Node-Salesforce) is an isomorphic JavaScript library designed for interacting with the Salesforce API, capable of running in both web browsers and Node.js environments. The current stable version is 3.10.14, with releases primarily focusing on bug fixes and dependency updates within the 3.x series, indicating an active maintenance cadence. It provides comprehensive access to various Salesforce APIs including REST, Apex REST, Analytics, Bulk, Chatter, Metadata, SOAP, Streaming, and Tooling APIs. A key differentiator is its broad API coverage and isomorphic design, allowing developers to use a single library across different JavaScript runtimes. It also offers a command-line interface with a REPL for interactive exploration and learning. This library is a mature and widely used solution for integrating JavaScript applications with Salesforce.

Common errors

Warnings

Install

Imports

Quickstart

This code snippet demonstrates how to establish a connection to Salesforce using jsforce, authenticate with username-password flow (suitable for scripts or backend services), perform a SOQL query to fetch account data, create a new Lead record, and then retrieve it. It highlights basic CRUD operations and error handling, showcasing the core interaction pattern with the Salesforce API.

import jsforce from 'jsforce';

async function connectAndQuery() {
  const conn = new jsforce.Connection({
    // When using OAuth2, configure it here. For username/password flow, loginUrl is sufficient.
    // oauth2: {
    //   loginUrl: process.env.SF_LOGIN_URL ?? 'https://login.salesforce.com',
    //   clientId: process.env.SF_CLIENT_ID ?? '',
    //   clientSecret: process.env.SF_CLIENT_SECRET ?? '',
    //   redirectUri: process.env.SF_REDIRECT_URI ?? '',
    // }
    loginUrl: process.env.SF_LOGIN_URL ?? 'https://login.salesforce.com'
  });

  try {
    // Authenticate using username-password flow (convenient for scripts, but consider OAuth for broader applications)
    await conn.login(
      process.env.SF_USERNAME ?? '',
      process.env.SF_PASSWORD_TOKEN ?? '' // password + security token (if enabled)
    );
    console.log('Successfully connected to Salesforce!');
    console.log('Instance URL:', conn.instanceUrl);
    console.log('User ID:', conn.userInfo?.id);

    // Perform a SOQL query to fetch the first 5 Account records
    const result = await conn.query<{ Id: string; Name: string }>('SELECT Id, Name FROM Account LIMIT 5');
    console.log('Query Results (first 5 Accounts):');
    result.records.forEach(record => {
      console.log(`  ID: ${record.Id}, Name: ${record.Name}`);
    });

    // Example: Create a new Lead record
    const createResult = await conn.sobject('Lead').create({
      LastName: `JSforce Lead ${Date.now()}`,
      Company: 'JSforce Corp',
      Status: 'Open - Not Contacted'
    });
    console.log(`Created Lead with ID: ${createResult.id}, success: ${createResult.success}`);

    // Example: Retrieve the newly created Lead record
    if (createResult.success) {
      const retrievedLead = await conn.sobject('Lead').retrieve(createResult.id);
      console.log(`Retrieved Lead: ${retrievedLead.LastName} from ${retrievedLead.Company}`);
    }

  } catch (err: any) {
    console.error('Error connecting or performing Salesforce operations:', err.message);
  }
}

connectAndQuery();

view raw JSON →