Sub topic 10
Opening, Inspecting, and Logging in to the Scratch Org
This chapter assumes you have already authorized a Dev Hub with the alias my-hub. It also assumes you created a scratch org with the alias my-scratch from the definition file in the previous chapter. You should be comfortable running sf commands in a terminal.
A new scratch org has no password. The CLI stored an access token when it created the org, and every command you run uses that token. This chapter covers three tasks: opening the org in a browser, reading its details, and setting a password so you can log in without the CLI.
How the CLI logs you in
When the Dev Hub creates a scratch org, it returns an auth token to the CLI. The CLI saves that token on your machine, tied to the org's username and alias. sf org open uses the saved token to build a one-time login link and hands it to your browser.
Two consequences follow. Anyone who holds the login URL can enter the org until the session ends, so treat that URL like a password. And if the local auth store lacks the org, for example on another computer, the CLI cannot open it.
Opening the org
The basic command uses the alias:
sf org open --target-org my-scratchIf you set --set-default when you created the org, you can drop the flag:
sf org openTo see which org is the default, run:
sf config get target-orgUseful flags:
| Flag | Effect |
|---|---|
| `--path` | Opens a specific page instead of the home page |
| `--url-only` | Prints the login URL and does not launch a browser |
| `--browser` | Chooses `chrome`, `edge`, or `firefox` |
| `--private` | Opens a private or incognito window |
| `--source-file` | Opens a metadata file, such as a Flow, in its matching builder |
To jump straight to Setup:
sf org open --target-org my-scratch --path lightning/setup/SetupOneHome/homePrivate windows and session conflicts
Browsers keep one Salesforce session per domain in a normal window. Suppose you already have another org open in the same profile. The new login can replace that session, or it can land you on the wrong org. Use --private to avoid this:
sf org open --target-org my-scratch --privateYou can also keep a separate browser profile for each org you work in.
Headless machines and CI
On a remote server, in a container, or in WSL, there may be no browser to launch. Print the URL and open it elsewhere:
sf org open --target-org my-scratch --url-onlyThe printed URL contains a live token. Do not paste it into chat, tickets, or CI logs.
Inspecting the org
sf org display reads the saved auth details and shows them:
sf org display --target-org my-scratchThe output is a table. Field names vary a little between CLI versions, but you can expect these:
- Username: the generated admin user, which looks like
[email protected]. - Org Id: the 15- or 18-character ID of the scratch org.
- Instance Url: the My Domain URL of the org.
- Status:
Activewhile the org is usable. - Expiration Date: the day the org will be deleted.
- Dev Hub Id: the Dev Hub that created the org.
- Access Token: the session token the CLI uses.
Add --verbose to also see the auth URL, which the CLI can use to reauthorize the org:
sf org display --target-org my-scratch --verboseFor scripts, ask for JSON and read the fields you need:
sf org display --target-org my-scratch --jsonThe JSON output contains the access token as well. Do not store it in build artifacts.
Checking the expiration date
Read the expiration date before you start a long piece of work. If the org was created with --duration-days 7, the date is seven days after creation. Local changes in the org are lost at expiry, so retrieve anything you want to keep before then.
Listing every org the CLI knows about
sf org listThis prints your Dev Hubs and scratch orgs in separate sections. A marker shows which org is the default for the current project. For scratch orgs you can see the alias, username, org ID, and expiration date.
Related flags:
--allincludes expired and deleted scratch orgs that the CLI still has records for.--cleanremoves local records for orgs that are expired or deleted. It asks for confirmation first.--skip-connection-statusskips the live check against each org. Use it when the list is slow.
Run sf org list --clean now and then. Without it, the list fills with entries for dead orgs, and old aliases can confuse you.
Logging in without the CLI
Sometimes you need a username and password. Examples are a tool that does not use the CLI, a mobile app, or a teammate who needs to sign in. The scratch org definition file from this tutorial includes the EnableSetPasswordInApi feature, which allows the CLI to set a password. Generate one:
sf org generate password --target-org my-scratchThe CLI sets a random password on the org's admin user. To read it later, display the user:
sf org display user --target-org my-scratchThe output shows the username, the org ID, the instance URL, and the password if you generated one. To sign in, go to the instance URL in a browser, or to https://test.salesforce.com for a scratch org, and enter the username and password. This sign-in does not use the CLI token.
If the feature is missing from your definition file, password generation fails. Add EnableSetPasswordInApi to features and create a new scratch org, because the definition applies only when the org is created.
A generated password is stored on your machine in the same place as the token. Treat it the same way.
Common problems
| Symptom | Likely cause | Fix |
|---|---|---|
| `No authorization information found for my-scratch` | The alias is mistyped, or the org was removed from the local store | Run `sf org list` and check the alias |
| The browser opens and shows the login page | The token is no longer valid, or the org expired | Check `Status` in `sf org display`. If it is expired, create a new org |
| The browser opens the wrong org | An existing session in the same browser profile took over | Use `--private` or a separate profile |
| Nothing opens on a server | There is no browser on the machine | Use `--url-only` and open the URL on another machine |
| Password generation fails | `EnableSetPasswordInApi` was not set when the org was created | Add the feature and create a new org |
| `sf org open` targets the wrong org | The default org is not the one you expect | Pass `--target-org` explicitly, or run `sf config set target-org my-scratch` |
A short routine
- Run
sf org listto confirm the org isActiveand note its expiration date. - Run
sf org open --target-org my-scratch --privateto start working. - Run
sf org display --target-org my-scratchif you need the username or instance URL. - Generate a password only when something outside the CLI needs to sign in.
Exercise
A teammate asks for access to your scratch org my-scratch. You do not want to send them a URL made by sf org open --url-only. What do you run to give them a username and password, and what must have been in the org's definition file for it to work?
Show answer
Run sf org generate password --target-org my-scratch, then sf org display user --target-org my-scratch to read the username and password. The definition file must include the EnableSetPasswordInApi feature when the org is created. If it does not, add it and create a new scratch org.
Exercise
You run sf org open and it opens a different org than the one you meant. Name two ways to prevent this.
Show answer
Pass --target-org my-scratch explicitly, or set the default with sf config set target-org my-scratch. If the cause is a browser session from another org, add --private or use a separate browser profile.