Sub topic 5
Log in to an org
The CLI can't deploy, query, or run Apex until it holds credentials for an org. This chapter covers how sf authorizes an org, which login method fits which situation, where the credentials are stored, and how to fix common failures.
This chapter assumes you have installed the CLI (sf --version works) and can read the sf <topic> <command> --flag structure. You also need an org you can sign in to, such as a free Developer Edition org.
What "authorizing an org" means
Authorizing an org means the CLI obtains tokens that let it call Salesforce APIs as one specific user. You do not type your password into the CLI. Salesforce authenticates you, and the CLI receives tokens in exchange.
The CLI stores each authorization locally and identifies it by the username it was issued for. Every later command that needs an org reads from that store. You can have many authorized orgs at once, and the CLI needs a way to know which one a command targets. That is the job of aliases and the default target org, covered in the next chapter. Here, we only create the authorizations.
The login commands
All login commands live under sf org login. Each one suits a different environment.
| Command | Use it for | Needs a browser |
|---|---|---|
| `sf org login web` | Everyday development on your own machine | Yes |
| `sf org login device` | Machines without a usable browser, such as a remote shell | On another device |
| `sf org login jwt` | Unattended automation such as CI jobs | No |
| `sf org login sfdx-url` | Reusing an existing authorization in automation | No |
| `sf org login access-token` | Short-lived access with a token you already have | No |
Use web interactively and jwt or sfdx-url for pipelines. The remaining sections cover each.
Browser login
This is the method you will use most:
sf org login web --alias myorg --set-default
sf org listThe first command opens your default browser at a Salesforce login page. You sign in and approve access for the Salesforce CLI. The browser then redirects to a small server the CLI runs on your own machine, and the CLI finishes the exchange. The terminal prints the authorized username.
The flags do the following:
--alias myorgstores a short name that points to the username. You can passmyorgwherever a command accepts a target org.--set-defaultmakes this org the default target org, so commands without--target-orguse it.
The sequence looks like this:
sf org list then shows the orgs the CLI knows about, with their aliases, usernames, and connection status.
Sandboxes, My Domain, and custom login URLs
By default, the CLI sends you to https://login.salesforce.com. That works for production, Developer Edition, and Trailhead orgs. A sandbox signs in at a different host, so you must say so with --instance-url:
sf org login web --alias mysandbox --instance-url https://test.salesforce.comIf your company enforces My Domain login, use your domain URL instead:
sf org login web --alias mycompany --instance-url https://mycompany.my.salesforce.comThe flag name is misleading. Despite "instance," it is the URL where you log in. Pointing a sandbox at login.salesforce.com is one of the most common causes of a failed login.
Choosing a browser
If the wrong browser opens, or you want a private window, pick one explicitly:
sf org login web --alias myorg --browser chromeThe --browser flag accepts chrome, edge, and firefox. Use a private or incognito window when you are signed in to a different org in your normal browser. Otherwise the login can silently reuse that existing session and authorize the wrong org.
Device login
On a remote server or container with no browser, use the device flow:
sf org login device --alias myorgThe CLI prints a verification URL and a short code. You open that URL on any other device, enter the code, and sign in there. The CLI waits and completes the authorization on its side. Salesforce must have the device flow enabled for the connected app in use. If the command is rejected, ask an admin whether it is available.
JWT login for automation
A CI job cannot click through a browser. The JWT bearer flow lets a script authorize with a private key instead. It needs setup before the first run:
- Create a certificate and private key pair.
- Create a connected app in the org that uses the certificate and allows the OAuth scopes you need.
- Pre-authorize the target user for that connected app, usually through a permission set or profile.
Then log in with the key file, the connected app's consumer key, and the username:
sf org login jwt \
--username [email protected] \
--jwt-key-file server.key \
--client-id "$SF_CONSUMER_KEY" \
--instance-url https://login.salesforce.com \
--alias ci-orgKeep server.key out of source control and inject it from your CI system's secret store. The consumer key is not as sensitive, but there is no reason to hard-code it either. If the user is not pre-authorized for the connected app, the login fails with an error that mentions the user not being approved. Fix that in the org, not in the command.
Reusing an authorization with an SFDX auth URL
If you have already logged in on one machine, you can export that authorization as a text string and import it elsewhere. Show it with the verbose display:
sf org display --target-org myorg --verboseThe output includes a line labeled Sfdx Auth Url. It has this shape:
force://<client-id>:<client-secret>:<refresh-token>@<instance-host>Save that string to a file, for example authfile.txt, and import it:
sf org login sfdx-url --sfdx-url-file authfile.txt --alias ci-orgThe URL contains a refresh token, which is equivalent to a long-lived credential for that user. Treat the file as a secret. Never commit it, and delete it after use. This method is convenient for scratch orgs in a pipeline, because the setup stage can create the org and later stages can import it.
Access-token login
If something else has already given you a valid access token, you can pass it through an environment variable:
export SF_ACCESS_TOKEN="<token>"
sf org login access-token --instance-url https://mycompany.my.salesforce.com --no-promptAn access token expires, typically within hours, and there is no refresh token behind it. Use this only for short tasks. The environment variable keeps the token out of your shell history and process list, which is why the CLI reads it that way.
Where the credentials go
The CLI keeps authorization data in a hidden .sfdx folder in your home directory. Each org gets a JSON file named after its username, and the tokens inside are encrypted. A separate alias.json file maps aliases to usernames.
Three consequences follow:
- Authorizations are per user account on your computer, not per project. Any project folder can use any org you have logged in to.
- Running
sf org login webagain for the same username updates the existing entry. It does not create a duplicate. - If you reuse an alias for a different username, the alias moves to the new org. The old org stays authorized, but you now refer to it by username.
Inspecting and removing authorizations
To see what the CLI knows:
sf org list
sf org display --target-org myorgsf org list lists non-scratch orgs and active scratch orgs. Add --all to include expired scratch orgs. sf org display prints the username, org ID, instance URL, and connection status for one org. It hides the tokens unless you add --verbose.
To remove an authorization, log out:
sf org logout --target-org myorgThe CLI asks for confirmation. Add --no-prompt in scripts. Logging out deletes the local credentials only. It does not delete the org, and it does not necessarily revoke the tokens on the Salesforce side. If a token has leaked, revoke it in the org's connected app usage or OAuth settings.
Common errors and fixes
| Symptom | Likely cause | Fix |
|---|---|---|
| Login page says the user cannot be found | Sandbox user signing in at the production login URL | Add `--instance-url https://test.salesforce.com` |
| Browser opens but the CLI never finishes | Browser blocked the redirect to localhost, or a different profile was used | Retry with `--browser` set, or use `sf org login device` |
| Authorized the wrong org | Existing browser session reused | Use a private window, or log out of Salesforce in the browser first |
| `No authorization information found for myorg` | The alias or username is not in the local store | Run `sf org list` to check spelling, then log in again |
| Expired access or refresh token message | Org policy expired the refresh token, or an admin revoked it | Log in again with the same alias |
| JWT login says the user is not approved | User not pre-authorized for the connected app | Assign the connected app through a permission set or profile |
Org policy also matters here. A connected app's refresh token policy can force periodic re-login, so a long-idle org can need re-authorization without anything being wrong on your machine.
Practical habits
- Give every org an alias at login time. Usernames are long and easy to mistype.
- Use a meaningful alias such as
dev-sandboxrather thanorg1, because it will appear in scripts and commands. - Do not create an alias by hand-editing
alias.json. Use the--aliasflag. - In automation, pass
--no-promptwhere available, so a job never waits for input that no one will give. - Authorize production with
weblogin on your own machine only when needed, and prefer a sandbox or scratch org for experiments.
Exercise
Your teammate runs sf org login web --alias qa to authorize a sandbox. The login page rejects their credentials, although the username and password are correct. What is the most likely cause, and what command fixes it?
Show answer
The command sent them to the default production login URL (login.salesforce.com), but sandbox users sign in at test.salesforce.com. Re-run the login with the sandbox URL: sf org login web --alias qa --instance-url https://test.salesforce.com. If the company uses My Domain login, use the My Domain URL instead.
Exercise
You need a CI job to authorize an org without any human interaction. Name one login command that works for this, and list the setup the org needs before the first run.
Show answer
Use sf org login jwt. Before the first run, create a certificate and private key, create a connected app that uses the certificate and the needed OAuth scopes, and pre-authorize the target user for that connected app (for example through a permission set or profile). The job then supplies --username, --jwt-key-file, and --client-id. The key must come from a secret store, not from source control.