Newer Version Available

This content describes an older version of this product. View Latest

Step 2: Authentication

The first action in an API-based integration is authenticating requests with your Salesforce org. Bulk API 2.0 and Bulk API use different authentication methods.

Bulk API 2.0 is a REST-based API that supports all OAuth 2.0 flows supported by other Salesforce REST APIs. Bulk API 2.0 requires an access token (also known as a “bearer token”) for authentication. This topic, and the remainder of this Quick Start, describe getting an access token and using it to make Bulk API 2.0 requests with cURL.

In contrast, Bulk API uses a session ID obtained with an X-SFDC-Session header fetched with SOAP API’s login() call. For an example, see Step 1: Log In Using the SOAP API.

These examples use an access token. Any API call that requires a session ID doesn’t work with these instructions.

Note

While it’s possible to create and authenticate against your own connected app, Salesforce CLI is used in these Quick Start examples for convenience. Effectively, Salesforce CLI is a connected app with which you can authenticate and requires no work to configure.

The examples in this Quick Start use the cURL tool to send HTTP requests that access, create, and manipulate resources in Salesforce. If you use a different tool to send requests, you can use the same elements from the cURL examples to send requests. Although these instructions describe a scenario with a Developer org, they work in the same way with any type of Salesforce org. The cURL tool is pre-installed on many Linux and macOS systems. Windows users can download a version at curl.se. When using HTTPS on Windows, ensure that your system meets the cURL requirements for SSL.

cURL is an open-source tool and isn’t supported by Salesforce.

Note

Get an Access Token with Salesforce CLI

Use the access token (also known as a “bearer token”) that you get from Salesforce CLI to authenticate cURL requests.

  1. Install or update Salesforce CLI. .
    1. If you already have Salesforce CLI installed, update it using the instructions in Update Salesforce CLI.
    2. If you need to Install Salesforce CLI, install the latest version for your operating system.
    3. Verify Your Installation.
  2. Log in to your Developer org with Salesforce CLI.
    1sfdx auth:web:login
    A browser opens to https://login.salesforce.com.
  3. In the browser, log in to your Developer org with your user’s credentials.
  4. In the browser, click Allow to allow access.
    At the command line, you see a similar confirmation message.
    1Successfully authorized juliet.capulet@empathetic-wolf-g5qddtr.com with org ID 00D5fORGIDEXAMPLE
  5. At the command line, get the access token by viewing authentication information about your org.
    1sfdx force:org:display --targetusername <username>
    For example:
    1sfdx force:org:display --targetusername juliet.capulet@empathetic-wolf-g5qddtr.com
    Example command output:
    1=== Org Description
    2KEY               VALUE
    3────────────────  ────────────────────────────────────────────────────────────────────────────────────────────────────────────────
    4Access Token      00DE0X0A0M0PeLE!AQcAQH0dMHEXAMPLEzmpkb58urFRkgeBGsxL_QJWwYMfAbUeeG7c1EXAMPLEDUkWe6H34r1AAwOR8B8fLEz6nEXAMPLE
    5Client Id         PlatformCLI
    6Connected Status  Connected
    7Id                00D5fORGIDEXAMPLE
    8Instance Url      https://MyDomainName.my.salesforce.com
    9Username          juliet.capulet@empathetic-wolf-g5qddtr.com
    In the command output, make note of the long Access Token string and the Instance Url string. You need both to make cURL requests.

    To get a new token after your access token expires, repeat this step of viewing your authentication information.

    Note

Optional Salesforce CLI Shortcuts

After you’ve authenticated successfully with the SFDX command line, try out these optional shortcuts in your cURL workflow to streamline future authentication with the SDFX CLI.

List My Orgs
1sfdx force:org:list
Lists all the orgs that you’ve created or authenticated to.
Open My Org
1sfdx force:org:open -u <user>
Opens the specified org (identified by user) in your browser. Because you’ve successfully authenticated with this org previously using SFDX, it’s not required to provide your credentials again.
Display the Access Token for My Org
1sfdx force:org:display -u <user>
Output includes your access token, client ID, connected status, org ID, instance URL, username, and alias, if applicable.

Set an Alias for My Username

For convenience, create an alias for your username so that you don’t have to enter the entire Salesforce string. For example, instead of
1juliet.capulet@empathetic-wolf-g5qddtr.com
Create an alias like
1dev
To set the alias in this example, you invoke
1sfdx alias:set dev=juliet.capulet@empathetic-wolf-g5qddtr.com

Use These Commands in a Script

Use the CLI’s JSON output by invoking the --json flag. Requesting JSON output provides a consistent output format, which is ideal for running scripts. Without the --json flag, the CLI can change the output format.

See Also