Sub topic 6
Authorizing the Dev Hub from the CLI
This chapter assumes you have enabled Dev Hub in Setup and installed the Salesforce CLI (sf). You should also be comfortable running commands in a terminal. Authorizing the Dev Hub is the step that lets the CLI create scratch orgs on your behalf.
What authorization does
Enabling Dev Hub in Setup only changes the org. The CLI still has no credentials for it. Authorization connects the two. The CLI opens a browser, you log in, and the CLI stores an OAuth refresh token for that org on your machine. Later commands use that token, so you do not enter a password again.
The CLI tracks each authorized org by its username. You can attach an alias to make commands shorter. You can also mark one org as the default Dev Hub, so that sf org create scratch knows which Dev Hub to use without a flag.
Confirm the CLI first
Check that the CLI runs before you try to log in.
sf --versionIf the command is not found, install the CLI and reopen your terminal. A version string means you can continue.
Log in and set the default
Run the web login command with two options.
sf org login web --set-default-dev-hub --alias my-hub--set-default-dev-hubmarks this org as your default Dev Hub.--alias my-hubgives the org a short name you can use in later commands.
A browser window opens. Log in as a user who has the permissions needed to use Dev Hub, then approve the access request. When the browser reports success, the terminal prints the username that was authorized.
The flow looks like this:
Log in to a different login URL
The command above uses the standard production login page. If your Dev Hub is a sandbox or uses a My Domain, pass --instance-url with that URL. Do this only when the default page does not reach your org.
Verify the result
List the orgs the CLI knows about.
sf org listYour Dev Hub appears with the alias my-hub. The output marks the default Dev Hub, so confirm the marker is next to this org. If you also want to check that the Dev Hub is usable, run sf limits api display --target-org my-hub and look for the scratch org limits. A later chapter covers those.
Common problems
| Symptom | Likely cause | What to do |
|---|---|---|
| Login succeeds but scratch org creation says no Dev Hub is set | You omitted `--set-default-dev-hub` | Log in again with the flag, or pass `--target-dev-hub my-hub` on each command |
| Browser shows the wrong org | Your browser holds a session for another org | Log out in the browser or use a private window, then rerun the command |
| Later commands report an authorization error | The token was revoked or the user's access changed | Run the login command again |
Logging in again for the same org is safe. The CLI updates the stored entry instead of creating a duplicate.
Exercise
You authorized your Dev Hub with sf org login web --alias my-hub and left out --set-default-dev-hub. Creating a scratch org now fails because no Dev Hub is found. Name two ways to fix it.
Show answer
Run the login command again with --set-default-dev-hub, which marks the org as the default Dev Hub. Or keep the current login and add --target-dev-hub my-hub to the scratch org creation command so it names the Dev Hub explicitly.