Sub topic 7
Authorizing an Org and Setting the Default
This chapter covers how the Salesforce CLI and the VS Code extensions connect your project to an org. You will authorize an org, give it an alias, set it as the default, and learn how to check or change that choice.
Prerequisites
This chapter assumes you have already done the following:
- Installed VS Code, the Salesforce CLI (
sf), a Java JDK, and the Salesforce Extension Pack. - Created a Salesforce DX project, such as
my-apex-project, and opened it in VS Code. - Have access to a Developer Edition org, a sandbox, or another org you may use for development.
What "authorizing an org" means
Your project is only files on disk. To deploy source, run tests, or collect debug logs, the tooling must send requests to a Salesforce org. Before it can do that, the org has to trust your machine.
Authorization is the step that creates this trust. The CLI opens a browser, you log in to the org and approve access, and Salesforce returns an OAuth token. The CLI stores that token locally. Later commands use the stored token, so you do not enter a password each time.
The VS Code extensions do not keep their own credentials. They call the CLI and read the CLI's list of authorized orgs. An org you authorize in the terminal therefore appears in VS Code, and the reverse is also true.
Org types and login URLs
The login URL decides which Salesforce environment the browser visits. Choose the one that matches your org.
| Org type | Login URL | Notes |
|---|---|---|
| Developer Edition, production, Trailhead Playground | `https://login.salesforce.com` | This is the default in `sfdx-project.json`. |
| Sandbox | `https://test.salesforce.com` | Required for sandbox logins. |
| My Domain org | `https://your-domain.my.salesforce.com` | Use your own My Domain URL if your org requires it. |
If you log in to a sandbox with the production URL, the login fails with an error from Salesforce. Match the URL to the org type before you troubleshoot anything else.
Authorizing from the command line
This is the command from the project plan. It opens a browser, saves the org under an alias, and makes it the default.
sf org login web --alias my-org --set-default
sf org listHere is what each part does:
sf org login webstarts the browser-based OAuth flow.--alias my-orggives the org a short name you can use in later commands, so you do not have to type the username.--set-defaultmakes this org the default target org. Commands that need an org use it unless you say otherwise.sf org listprints the orgs the CLI knows about.
To authorize a sandbox, add the instance URL:
sf org login web --alias my-sandbox --instance-url https://test.salesforce.comRun these commands from inside the project folder if you want the default to apply to that project. The next section explains why.
Where the default is stored
A default org can exist at two levels. The level matters because it decides which project is affected when you change it.
| Level | Where it is stored | Scope |
|---|---|---|
| Local | `.sf/config.json` in the project folder | Only this project |
| Global | A config file in your home directory | Every project without a local value |
When a command runs inside a DX project, the CLI reads the local value first. If none exists, it falls back to the global value. Using --set-default inside a project normally writes the local value.
You can check what is set with:
sf config get target-orgYou can also set it directly, without logging in again:
sf config set target-org my-orgThe .sf folder holds local settings and is normally excluded from version control by the project's .gitignore. Do not commit it, because it describes your personal setup.
Authorizing from VS Code
You can do the same thing without opening a terminal. Open the Command Palette and run one of these commands:
- Press
Ctrl+Shift+Pon Windows and Linux, orCmd+Shift+Pon macOS. - Type
SFDX: Authorize an Organd press Enter. This makes the extension run the web login flow. - Choose the login URL type, such as Project Default, Production, Sandbox, or Custom.
- Enter an alias when prompted.
- Log in through the browser and click Allow.
Two related commands are worth knowing:
SFDX: Authorize an Org using Session IDis for orgs where the browser flow is not available.SFDX: Authorize a Dev Hubis for orgs you will use to create scratch orgs. You can leave it for later if you only use a sandbox or Developer Edition org.
After a successful login, the extension usually sets the new org as the default for the project.
Choosing and checking the default org in VS Code
VS Code shows the current default in the status bar at the bottom of the window. It shows the alias if one exists, or the username if not. If you see "No Default Org Set", no org is selected.
To change it:
- Open the Command Palette.
- Run
SFDX: Set a Default Org. - Pick an org from the list.
You can also click the org name in the status bar to open the same picker. The choice is written to .sf/config.json, so the CLI and the extensions stay in sync.
{
"target-org": "my-org"
}Your file may contain other keys. The target-org value is the one that matters here.
The `sourceApiVersion` and `sfdcLoginUrl` link
The sfdx-project.json file from the earlier chapter contains two values that interact with authorization:
{
"packageDirectories": [
{ "path": "force-app", "default": true }
],
"name": "my-apex-project",
"namespace": "",
"sfdcLoginUrl": "https://login.salesforce.com",
"sourceApiVersion": "62.0"
}sfdcLoginUrlis the login URL the extension offers when you choose "Project Default". Change it tohttps://test.salesforce.comif your project always targets a sandbox.sourceApiVersionis the API version used when deploying and retrieving. It is not part of authorization, but a mismatch with the org causes deploy errors that can look like login problems. Keep it at or below the highest version your org supports.
Working with several orgs
Most developers use more than one org, for example a sandbox for development and a Developer Edition org for experiments. Aliases keep this manageable.
sf org login web --alias dev-sandbox --instance-url https://test.salesforce.com
sf org login web --alias playground
sf org listAfter this, you can target any org for a single command with the --target-org flag, without changing the default:
sf apex run test --target-org playground --test-level RunLocalTests --wait 10This is safer than switching the default back and forth. It also makes scripts explicit about which org they touch.
A few rules help avoid mistakes:
- Use clear aliases that include the org type, such as
dev-sandboxorprod. - Check the status bar before every deploy. A deploy goes to whatever the default org is.
- Never set a production org as the default in a project you use for experiments.
Inspecting and removing orgs
To see details about one org, including its username, instance URL, and connection status, run:
sf org display --target-org my-orgThe output includes the access token and the org ID. Treat it as sensitive and do not paste it into tickets, chats, or screenshots.
To remove the stored credentials from your machine, log out:
sf org logout --target-org my-orgLogging out only removes the local token. It does not delete the org or its data. If the org was the default, the default becomes unset.
Common problems
| Symptom | Likely cause | Fix |
|---|---|---|
| Browser shows an invalid login or wrong-org error | Production URL used for a sandbox, or the reverse | Log in again with the correct `--instance-url`. |
| Status bar shows "No Default Org Set" | No org authorized, or the default was removed | Run `SFDX: Set a Default Org` or `sf config set target-org <alias>`. |
| Deploy says no default org or no authorization | The `.sf/config.json` file is missing or points to a removed alias | Check with `sf config get target-org`, then set it again. |
| An org that worked yesterday now fails with an expired or invalid token | The session or refresh token was revoked, or the password changed | Run `sf org login web` again for that org. |
| An org appears in the terminal but not in VS Code | VS Code was open before the login | Reload the window with `Developer: Reload Window`. |
| Browser flow does not return to the CLI | A blocked browser callback port or a restrictive network | Allow the local callback, or try a different browser. |
Verifying your setup
Run this short check before moving on:
sf org list
sf config get target-org
sf org display --target-org my-orgYou should see your org in the list, the alias returned as the default, and a connected status in the display output. Then confirm that the same alias appears in the VS Code status bar. If all four agree, the next chapter's code editing and deployment work will go to the org you expect.
Exercise
You authorized a sandbox with sf org login web --alias dev-sandbox and the browser reported a login error. You then see that the command had no --instance-url flag. What is the most likely cause, and which command fixes it?
Show answer
The CLI used the default production login URL (https://login.salesforce.com), but sandboxes sign in at https://test.salesforce.com. Run sf org login web --alias dev-sandbox --instance-url https://test.salesforce.com.
Exercise
You have two authorized orgs, dev-sandbox (the default) and playground. You want to run the local tests in playground once without changing the default. What command do you use?
Show answer
sf apex run test --target-org playground --test-level RunLocalTests --wait 10. The --target-org flag applies to this one command, and the default stays dev-sandbox.