Sub topic 5
Log in to an org
The Salesforce CLI cannot deploy, retrieve, query, or run Apex until it holds an authorization for an org. This chapter covers how sf org login web creates that authorization, how to log in to sandboxes, and how to check and remove stored logins.
This chapter assumes you have installed the CLI (sf --version works) and can read the sf <topic> <command> --flag structure. You also need a Salesforce org you can log in to, such as a free Developer Edition org.
What authorization does
Authorizing an org means the CLI stores a credential for a user in that org on your computer. After that, commands can act as that user without asking for a password. The CLI never stores your password. It stores tokens that Salesforce issues after you approve access in the browser.
The most common method is the web login flow, which uses OAuth 2.0 (a standard way to grant an application access without sharing a password).
Log in with the browser
Run the login command with an alias and the default flag:
sf org login web --alias myorg --set-default
sf org listThe first command opens your browser. Sign in, then click Allow on the access request. The terminal then prints the authorized username.
--alias myorggives the org a short name you can use in later commands instead of the full username.--set-defaultmakes this org the default target for commands that need one. The next chapter covers defaults in more detail.
sf org list shows the orgs the CLI knows about, with their aliases, usernames, and org IDs. A mark in the first column shows which org is your default.
Log in to a sandbox or custom domain
By default, sf org login web sends you to login.salesforce.com. That works for production and Developer Edition orgs. A sandbox signs in through a different URL, so pass it explicitly:
sf org login web --alias mysandbox --instance-url https://test.salesforce.comIf your company uses a My Domain, give that URL instead, for example https://mycompany.my.salesforce.com. Using the wrong URL is the most common cause of the "invalid username or password" error, even when your credentials are correct.
Other login methods
Not every environment has a browser. These commands cover other cases:
| Command | When to use it |
|---|---|
| `sf org login web` | Normal development on a machine with a browser |
| `sf org login device` | A machine with no usable browser. You approve on another device using a code |
| `sf org login jwt` | Automated jobs such as continuous integration. Uses a certificate and a connected app |
| `sf org login sfdx-url` | Reusing an authorization stored as an auth URL file |
Use sf org login web --help and the help for the other commands to see their required flags.
Check and remove logins
To confirm that a login still works, display the org:
sf org display --target-org myorgTo remove the stored credential for an org, log out:
sf org logout --target-org myorgLogging out only removes the local authorization. It does not delete the org or its data. Expired scratch orgs and orgs where your access was revoked can remain in sf org list until you remove them.
Common errors
- The browser does not open. Copy the URL the CLI prints into a browser yourself, or use
sf org login device. - The login succeeds but the wrong org is the default. Run
sf org listand check the default mark, then log in again with--set-defaultfor the right org. - A login worked yesterday but fails today. The token may have expired or an administrator may have revoked access. Run
sf org login webagain with the same alias.
Exercise
You need to authorize a sandbox and refer to it later as qa. Which command does this, and why will the default command not work for a sandbox?
Show answer
Run sf org login web --alias qa --instance-url https://test.salesforce.com. Without --instance-url, the CLI sends you to login.salesforce.com, which is the login page for production and Developer Edition orgs. A sandbox user cannot sign in there.