How to Connect Salesforce CLI to a Sandbox or Scratch Org in 2025
The Salesforce Command Line Interface (CLI) is a powerhouse for managing Salesforce orgs, and connecting it to a sandbox or scratch org unlocks its full potential for development, testing, and automation. Whether you’re iterating in a sandbox or experimenting in a scratch org, the CLI streamlines your workflow. As of April 4, 2025, this guide provides clear steps to connect the Salesforce CLI to these environments, ensuring you’re ready to deploy, query, and manage with ease.
Why Connect Salesforce CLI to a Sandbox or Scratch Org?
- Sandbox: A copy of your production org for testing changes, configurations, or integrations without risking live data.
- Scratch Org: A temporary, source-driven org for Salesforce DX development, perfect for building and validating features.
The CLI connects to both, letting you push code, run scripts, and retrieve metadata from your terminal—faster than browser-based tools.
Prerequisites
Before connecting, ensure you have:
- Salesforce CLI Installed: Verify with sf –version (see for installation).
- Terminal Access: Use Command Prompt (Windows), Terminal (macOS), or a Linux shell.
- Org Credentials: For a sandbox, you need login details. For a scratch org, you need a Dev Hub org set up.
- Permissions: Access to the target sandbox or Dev Hub (typically requires admin rights).
Connecting Salesforce CLI to a Sandbox
Step 1: Log In to the Sandbox
- Open Terminal: Launch your preferred command-line tool.
- Run the Login Command: Use this to authenticate: bashСвернутьПереносКопировать
sf org login web --alias MySandbox --instance-url https://test.salesforce.com
- –alias: A nickname for your sandbox (e.g., MySandbox).
- –instance-url: Use https://test.salesforce.com for sandboxes (not login.salesforce.com).
- Authorize: A browser window opens. Log in with your sandbox credentials, then allow access.
Step 2: Verify the Connection
- Check Org List: Run: bashСвернутьПереносКопировать
sf org list
Look for MySandbox under “Non-Scratch Orgs” with a green checkmark if connected. - Test a Command: Confirm it works: bashСвернутьПереносКопировать
sf org display --target-org MySandbox
This shows org details like ID, username, and instance URL.
Step 3: Set as Default (Optional)
To avoid specifying –target-org in every command:
bashСвернутьПереносКопировать
sf config set target-org=MySandbox
Connecting Salesforce CLI to a Scratch Org
Step 1: Set Up a Dev Hub
- Authorize Dev Hub: Connect to a production or trial org enabled as a Dev Hub: bashСвернутьПереносКопировать
sf org login web --alias MyDevHub --set-default-dev-hub
- –set-default-dev-hub: Marks this as your primary Dev Hub.
- Verify: Check with sf org list –all. Your Dev Hub should appear under “Dev Hub Orgs.”
Step 2: Create a Scratch Org
- Define Config: Ensure you have a project-scratch-def.json file in your SFDX project (create one with sf project generate if needed). Example: jsonСвернутьПереносКопировать
{ "orgName": "MyScratchOrg", "edition": "Developer", "features": ["EnableSetPasswordInApi"], "settings": { "orgPreferenceSettings": { "s1DesktopEnabled": true } } }
- Create the Org: Run: bashСвернутьПереносКопировать
sf org create scratch --definition-file config/project-scratch-def.json --alias MyScratch --set-default
- –alias: Names your scratch org.
- –set-default: Makes it the default org (optional).
- Wait: Creation takes a few minutes. You’ll see a success message with the org ID.
Step 3: Open and Verify
- Open the Org: Launch it in a browser: bashСвернутьПереносКопировать
sf org open --target-org MyScratch
- Check Details: Confirm the connection: bashСвернутьПереносКопировать
sf org display --target-org MyScratch
Look for the username and org ID in the output.
Managing Connections
- List All Orgs: See connected orgs with sf org list.
- Switch Default: Change the default org with sf config set target-org=AliasName.
- Log Out: Disconnect an org with: bashСвернутьПереносКопировать
sf org logout --target-org MySandbox
- Delete Scratch Org: Remove a scratch org when done: bashСвернутьПереносКопировать
sf org delete scratch --target-org MyScratch
Tips for Success
- Sandbox URLs: Use https://test.salesforce.com for sandboxes, not custom domains unless specified.
- Scratch Org Limits: Check your Dev Hub’s active scratch org limit (e.g., 100 for Enterprise) with sf org limits.
- Alias Names: Keep them unique and descriptive (e.g., TestSandbox1, FeatureScratch).
- Troubleshooting: If login fails, verify credentials or Dev Hub status. Clear stale auth with sf org logout –all.
- Secure Access: Avoid sharing auth tokens or storing them in public repos.
Final Thoughts
Connecting the Salesforce CLI to a sandbox or scratch org is a straightforward process that opens the door to powerful development and testing workflows. Sandboxes offer a safe space for real-world simulations, while scratch orgs provide a clean slate for Salesforce DX projects. As of 2025, mastering these connections ensures you’re equipped to handle any Salesforce task from the command line. Get started today—log in, create an org, and take control!