Sub topic 3
Authorizing an Org and Setting the Default Target
This chapter assumes you have the Salesforce CLI (sf) installed, know the basics of the command line, and have a Salesforce DX project open in a terminal. Every retrieve and deploy command needs to know which org it talks to. Here you will authorize an org and choose a default so you don't repeat the org on every command.
What authorization does
Authorizing an org stores a token on your machine. The CLI then uses that token to act on the org without asking for your password each time. Each authorized org gets a username, and you can give it a short alias to make commands easier to type.
The tokens are stored in your user home directory, not in the project. This means authorizations are shared by all projects on your machine, and they never end up in Git.
Log in with a browser
The most common method is the web login flow. The command opens a browser window where you sign in and approve access.
sf org login web --alias my-sandboxBy default this logs in to a production or developer org at login.salesforce.com. For a sandbox, add the sandbox login URL:
sf org login web --alias my-sandbox --instance-url https://test.salesforce.comAfter you approve, the terminal prints the username that was authorized. The alias my-sandbox now stands for that username.
Set the default target org
Many commands accept --target-org. If you leave it out, the CLI uses the default target org. Set it with the config command:
sf config set target-org my-sandboxThis writes to the project's local configuration, so each project can have its own default. To make a default for every project on your machine, add --global:
sf config set target-org my-sandbox --globalA flag on a command always wins over the default. If you run a command with --target-org production, it uses production even though the default is my-sandbox.
Check what you have
List every authorized org. The default is marked in the output.
sf org listTo see the current default and where it was set:
sf config get target-orgTo inspect one org, including its instance URL and username, run:
sf org display --target-org my-sandboxCommon errors
| Symptom | Likely cause | Fix |
|---|---|---|
| No default target org found | `target-org` was never set, or it was set in another project | Run `sf config set target-org <alias>` inside this project |
| Browser login hangs or fails for a sandbox | The login went to the production URL | Repeat with `--instance-url https://test.salesforce.com` |
| Command hits the wrong org | An old default or a global default is in effect | Run `sf config get target-org` and reset it |
| Expired or invalid token | The authorization was revoked or has lapsed | Log in again with `sf org login web` |
Log out
To remove an authorization from your machine, use:
sf org logout --target-org my-sandboxThis removes the stored token only. It does not delete the org.
Exercise
You run sf config set target-org my-sandbox in a project, and later run sf project deploy start --source-dir force-app --target-org production. Which org receives the deployment, and why?
Show answer
The production org. A --target-org flag on a command overrides the default target org, so my-sandbox is ignored for that command.
Exercise
Your web login for a sandbox keeps signing you in to production. What flag do you add to fix it?
Show answer
Add --instance-url https://test.salesforce.com to sf org login web, so the login page for sandboxes is used.