Sub topic 11
Connecting the Salesforce CLI to your org
This chapter assumes you have a Developer Edition org that you can log in to, a browser, and the Salesforce CLI (sf) installed. It also assumes you are comfortable running commands in a terminal. You will authorize the CLI against your org, then confirm that the connection works.
What authorization does
The Salesforce CLI does not store your password. It uses OAuth, a standard way to grant an application access without sharing credentials. You sign in through a browser. Salesforce then gives the CLI a token that it saves on your machine.
Each authorized org gets an optional alias, which is a short name you choose. Later commands use the alias instead of the long username.
Authorize the org
Run this command in a terminal:
sf org login web --alias my-dev-orgA browser window opens on the Salesforce login page. Sign in with your Developer Edition username and password. Complete the multi-factor prompt if you are asked. When the page asks you to allow access for the Salesforce CLI, choose Allow. The terminal then reports that it authorized your username and saved the alias.
A Developer Edition org uses the default login host, so you do not need extra flags. The --instance-url flag is only for orgs on a custom My Domain login URL or on other hosts, such as a sandbox.
List your orgs and open one
Check what the CLI knows about:
sf org list
sf org open --target-org my-dev-orgsf org list prints a table of authorized orgs with their aliases and usernames. sf org open launches your browser and logs you in with the saved token, so you do not type a password.
Confirm the org with a query
Run a SOQL query, which is Salesforce's query language for reading records. This one reads the Organization object, which holds one row describing your org.
sf data query --target-org my-dev-org --query "SELECT Name, OrganizationType FROM Organization"The result should show your org's name and an OrganizationType of Developer Edition. If it does, the CLI is talking to the right org.
You can also check from inside the org. Open the Developer Console from the gear menu, choose Debug, then Open Execute Anonymous Apex Window. Run this Apex code:
System.debug('Org: ' + UserInfo.getOrganizationName());
System.debug('User: ' + UserInfo.getUserName());Tick "Open Log" before you run it. In the log, filter for USER_DEBUG to see your org name and username.
Set a default org
Typing --target-org every time gets tedious. You can set a default for the current project folder:
sf config set target-org my-dev-orgCommands run in that folder then use the alias when you leave out --target-org. Add --global to make it the default everywhere.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| The browser does not open | No default browser, or a remote terminal | Copy the URL the CLI prints into a browser |
| `No authorization information found` | The alias is misspelled, or the org was never authorized | Run `sf org list` and check the alias |
| Login page shows the wrong org | You were already signed in to another org in the browser | Use a private window, or log out first |
| Query fails with an expired session or invalid grant error | The token was revoked, or the org was reset | Run `sf org login web` again |
| `sf` is not found | The CLI is not installed or not on your PATH | Reinstall it and open a new terminal |
Where this fits
Authorizing the CLI is a one-time step per org and machine. With the alias saved, you can later deploy metadata, run tests, and, once Dev Hub is enabled, create scratch orgs from the same terminal.
Exercise
You ran sf org login web --alias my-dev-org and signed in. Which two commands would you use to confirm that the CLI is authorized and that it points at a Developer Edition org?
Show answer
Run sf org list to see that my-dev-org appears among the authorized orgs. Then run sf data query --target-org my-dev-org --query "SELECT Name, OrganizationType FROM Organization" and check that OrganizationType is Developer Edition.
Exercise
Your teammate's sf org open --target-org my-dev-org fails with "No authorization information found". What is the most likely cause, and how do you fix it?
Show answer
The alias is not authorized on that machine, or it is spelled differently. Run sf org list to check the alias. If it is missing, run sf org login web --alias my-dev-org and sign in again.