Sub topic 3
Authorizing an org and setting the default target
Every retrieve and deploy command needs to know which org to talk to. This chapter covers how the Salesforce CLI (sf) gets permission to reach an org, how it stores that permission, and how it picks a target when you don't name one.
This chapter assumes you have sf installed, a Salesforce DX project (covered earlier in this tutorial), and a user account in the org you want to connect to.
What authorization does
Authorizing an org means proving to Salesforce that you are allowed to use it, then letting the CLI keep the resulting credentials. After that, commands such as sf project retrieve start can call the org's APIs without asking for a password again.
The CLI stores these credentials on your machine, in your home directory under .sfdx and .sf. The credentials are tied to your computer and your user, not to a project. Any DX project on the same machine can use an org you have authorized.
Each authorized org has two names you can use to refer to it:
- The username, such as
[email protected]. It is unique and always works. - The alias, a short label you choose, such as
my-sandbox. It exists only on your machine.
Use aliases in commands and scripts. They are shorter and don't change when a sandbox refresh alters the username suffix.
Logging in with a browser
The most common method is the web login flow. It opens a browser window, you sign in, and you approve access for the CLI.
sf org login web --alias my-sandbox
sf config set target-org my-sandboxThe first command authorizes the org and saves it under the alias my-sandbox. The second makes that org the default target for later commands.
By default, sf org login web sends you to the production login page (login.salesforce.com). That page does not work for sandboxes or custom My Domain URLs. For those, pass the login URL with --instance-url:
sf org login web --alias my-sandbox --instance-url https://test.salesforce.com
sf org login web --alias my-sandbox --instance-url https://mycompany--qa.sandbox.my.salesforce.comThe first form works for any sandbox. The second uses the My Domain URL of one specific sandbox. If you log in to a sandbox through the production page, the login fails with an error that no matching user was found.
You can combine both steps into one command with --set-default:
sf org login web --alias my-sandbox --instance-url https://test.salesforce.com --set-defaultOther login methods
A browser is not always available. The CLI offers other flows for those cases.
| Command | When to use it |
|---|---|
| `sf org login web` | Normal interactive work on your own machine. |
| `sf org login device` | You are on a remote machine or container with no browser. The CLI prints a code and a URL, and you complete the sign-in on another device. |
| `sf org login jwt` | Unattended automation, such as CI. Uses a connected app and a private key. |
| `sf org login sfdx-url` | Automation that has a stored authorization URL for an org. |
For a JWT login, you first create a connected app in the org and upload a certificate. Then the pipeline runs a command like this:
sf org login jwt \
--client-id "$SF_CLIENT_ID" \
--jwt-key-file server.key \
--username [email protected] \
--instance-url https://login.salesforce.com \
--alias ci-prodThe $SF_CLIENT_ID value is the connected app's consumer key. Keep it and the key file in your CI system's secret store, never in the repository.
The sfdx-url flow takes a file containing an authorization URL. You can produce one from an org you already authorized:
sf org display --target-org my-sandbox --verbose --jsonThe JSON output includes an sfdxAuthUrl field. Treat that value like a password, because anyone who has it can act as you in that org.
Checking what you have authorized
List every org the CLI knows about:
sf org listThe table separates Dev Hubs, sandboxes, scratch orgs, and other orgs. The first column marks the default target. Orgs whose tokens have expired appear in the list with a status such as Expired or Unable to refresh session.
To see the details of one org, including its username, instance URL, and access token status, run:
sf org display --target-org my-sandboxSetting the default target org
Typing --target-org on every command gets tedious. The target-org configuration variable supplies a default for any command that accepts that flag.
sf config set target-org my-sandbox
sf config get target-orgWith that set, these two commands do the same thing:
sf project retrieve start --manifest manifest/package.xml
sf project retrieve start --manifest manifest/package.xml --target-org my-sandboxLocal and global scope
sf config set writes to one of two files:
- Local (the default when you run it inside a project):
.sf/config.jsonin the project directory. It applies only to that project. - Global (
--globalor-g):~/.sf/config.jsonin your home directory. It applies everywhere.
sf config set target-org my-sandbox --globalA local value overrides a global one. This lets you keep one default org per project. Add .sf/ to .gitignore, which new DX projects do already, so your personal default does not reach teammates.
If you run sf config set outside a project directory without --global, the CLI reports an error because there is no project to hold a local setting.
How the CLI picks the org
When a command runs, the CLI looks for the target org in a fixed order. The first match wins.
The environment variable is useful in CI, where you want to set the target once for a whole job without writing a config file:
export SF_TARGET_ORG=ci-prod
sf project deploy start --manifest manifest/package.xmlDev Hub and other defaults
A Dev Hub is the org that creates and manages scratch orgs. It has its own default, separate from the target org:
sf config set target-dev-hub my-devhubKeeping the two settings separate means you can point target-org at a scratch org while commands that need a Dev Hub still find it.
Removing an org
When you no longer need access, log out. This deletes the stored credentials from your machine. It does not delete the org itself.
sf org logout --target-org my-sandboxAdd --no-prompt to skip the confirmation in scripts. If the logged-out org was your default, the config value stays set but points to an org the CLI no longer knows. The next command fails until you set a new default.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `No authorization information found for ...` | The alias or username was never authorized on this machine, or it is misspelled. | Run `sf org list` to check names, then log in again. |
| Login page says no matching user | You logged in to a sandbox through the production URL. | Repeat the login with `--instance-url https://test.salesforce.com`. |
| `expired access/refresh token` | The org was refreshed, the password changed, or the session was revoked. | Log in again with the same alias. |
| `Unable to resolve target-org` or similar after a logout | The config still names an org you removed. | Run `sf config set target-org <new-alias>`. |
| Commands hit an unexpected org | A local config or `SF_TARGET_ORG` overrides your global setting. | Check `sf config list` and `env`, or pass `--target-org`. |
Run sf config list to see every config value and whether it comes from the local or global file.
Exercise
You work in a DX project where sf config get target-org shows my-sandbox. Your CI job sets SF_TARGET_ORG=ci-prod and runs sf project deploy start --manifest manifest/package.xml --target-org staging. Which org receives the deploy, and why?
Show answer
The staging org. The --target-org flag has the highest priority, so it wins over both the SF_TARGET_ORG environment variable and the local or global target-org config value.
Exercise
You authorized a sandbox with sf org login web --alias qa and the login page reported that no matching user was found. What was probably wrong, and what command fixes it?
Show answer
The login used the default production URL, which does not accept sandbox users. Run sf org login web --alias qa --instance-url https://test.salesforce.com, or use the sandbox's My Domain URL.