Sub topic 3
Authorizing an Org and Checking Permissions
Every sf agent command acts on an org, so the CLI needs an authorized connection to one before anything else works. This chapter covers how to authorize an org, how to confirm the connection, and how to check that your user can build agents there.
This chapter assumes you have installed the Salesforce CLI and its agent plugin (the previous chapter), and that you know what orgs, sandboxes, and scratch orgs are. You should also be comfortable with permission sets and profiles. It also assumes you have a Salesforce DX project.
Why authorization comes first
The CLI doesn't store your Salesforce password. Authorizing an org runs an OAuth flow and saves a token on your machine under an alias or username. Later commands refer to that saved connection through --target-org.
In the agent workflow, this connection is used at every step:
sf agent generate agent-specreads from the org when it builds a spec.sf agent createwrites the agent and its metadata into the org.sf agent previewandsf agent test runsend conversations to the org.
If the connection is missing or expired, every one of these commands fails. The error messages are often generic. Checking the connection first saves time.
Choosing an authorization method
The CLI offers several ways to authorize. Pick the one that fits where the command runs.
| Method | Command | Best for |
|---|---|---|
| Browser login | `sf org login web` | Local development on your own machine |
| JWT bearer flow | `sf org login jwt` | CI systems and other unattended runs |
| Auth URL | `sf org login sfdx-url` | Reusing a stored credential, often in CI |
| Scratch org creation | `sf org create scratch` | Disposable orgs. The CLI authorizes it for you. |
For daily agent work, browser login is the usual choice. CI is covered in a later chapter.
Logging in with the browser
Run this from any directory. It opens your default browser.
sf org login web --alias my-dev-orgLog in with the org's credentials and approve access. The terminal then prints a confirmation that includes the username you authorized.
Use the --alias flag. An alias is a short name for the connection. Without it, you would have to type the full username, such as [email protected], in every command.
Two flags are worth knowing:
--instance-urlsets the login URL. Usehttps://test.salesforce.comfor a sandbox. For a My Domain or Experience Cloud site, use the org's own domain URL.--set-defaultmakes this org the default for the current project. Commands then work without--target-org.
sf org login web \
--alias my-sandbox \
--instance-url https://test.salesforce.com \
--set-defaultA production org or Developer Edition org uses the default login URL, so you do not need --instance-url for it.
Targeting an org with --target-org
Most commands accept --target-org, which takes an alias or a username. This tutorial passes it explicitly in each example, because that makes each command's target visible. A default org is convenient, but it can send a command to the wrong place when you switch between projects.
You can set defaults in two places:
- The project-level default applies only inside the current project folder.
- The global default applies everywhere.
sf config set target-org my-dev-org
sf config get target-orgBe careful with the global default. Passing --target-org always overrides it, and doing so on every command is the safest habit.
Checking the connection
After you log in, confirm that the saved connection works.
sf org list
sf org display --target-org my-dev-orgsf org list shows every org you have authorized, with its alias, username, and connection status. A status of Connected means the token works. Expired or revoked tokens show a different status. Scratch orgs also show an expiration date.
sf org display prints the details of one org: the username, the org ID, the instance URL, and the API version. Check the instance URL. It tells you whether you are talking to a sandbox, a scratch org, or production.
By default, sf org display does not print the access token. Don't paste output from --verbose into tickets or chat, because it can include a live token.
Prerequisites in the org
A working login is not enough. The org must have Agentforce available, and your user must be allowed to use it. Missing features and missing permissions are among the most common reasons sf agent create fails.
Org-level requirements
The org needs these before agent commands can work:
- An edition and license that include Agentforce. Not every edition has it.
- Einstein generative AI turned on in Setup.
- Agentforce turned on in Setup.
- Data Cloud or other features that your chosen agent type or actions depend on.
Exact Setup labels and requirements change between Salesforce releases. Check the current Salesforce documentation for the release your org runs, and don't rely on screen names from memory.
Scratch orgs add one more step. A scratch org is built from a definition file, and that file must list the features and settings the org needs. If it doesn't, the org is created without them. The definition file is where you declare this, so it is source-controlled with the project.
User-level permissions
Your user needs permissions to build and manage agents. These are granted through permission sets, permission set groups, or profiles. Names vary by release. Look for permission sets related to Agentforce, such as an agent-builder set. Your administrator may grant a similar custom set.
Your user also needs the usual developer permissions for deploying metadata, such as API Enabled and access to Apex classes. You need those to deploy the invocable Apex class in a later chapter.
An agent also runs as a user. When you create a customer-facing or employee-facing agent, the agent acts through a specific user or context, and that user needs access to the objects and fields the agent reads. For the case status example in this tutorial, the running user needs read access to Case and its Status field. The agent can see only what that user can see. Your CLI user and the agent's running user are two different identities. Check both.
sf org assign permset \
--name Agentforce_Builder_Example \
--target-org my-dev-orgReplace Agentforce_Builder_Example with the real permission set API name from your org. The name here is a placeholder.
Verifying a permission set assignment
You can check an assignment with a SOQL query on the PermissionSetAssignment object. This query uses the CLI to list the permission sets for your user.
sf data query \
--query "SELECT PermissionSet.Name, PermissionSet.Label FROM PermissionSetAssignment WHERE Assignee.Username = '[email protected]'" \
--target-org my-dev-orgReplace the username with your own, which you can read from sf org display. The output shows every permission set assigned to you. If the agent-related set is missing, ask an administrator to assign it, or assign it yourself if you have the rights.
How the pieces fit together
The sequence below shows what happens when you authorize and then run an agent command.
Common errors
| Symptom | Likely cause | What to do |
|---|---|---|
| `No authorization information found for ...` | The alias or username is misspelled, or the org was never authorized. | Run `sf org list` and compare names. Log in again if needed. |
| The status shows an expired or invalid token | The refresh token was revoked, or a scratch org expired. | Run `sf org login web` again. Recreate an expired scratch org. |
| Login redirects to the wrong place | Sandbox login used the production URL. | Add `--instance-url https://test.salesforce.com`. |
| Insufficient access when creating an agent | Your user lacks the agent-building permission set. | Check assignments with the query above. |
| The agent command is not found | The agent plugin is not installed. | Revisit the setup chapter and run `sf plugins`. |
| Feature or license error | Agentforce or generative AI is not enabled in the org. | Ask an administrator to enable it, or fix the scratch org definition. |
A short checklist
Run these checks before moving on to the agent spec.
sf org listshows your org as connected.sf org display --target-org my-dev-orgshows the instance URL you expect.- Agentforce and generative AI features are enabled in the org.
- Your user has an agent-building permission set.
- The agent's running user can read the objects the agent will use.
Exercise
You log in to a sandbox with sf org login web --alias my-sandbox, and the browser shows a production login page where your sandbox credentials fail. What is the cause, and what command fixes it?
Show answer
The login used the default production login URL. Sandboxes log in through a different URL. Run sf org login web --alias my-sandbox --instance-url https://test.salesforce.com to authorize the sandbox.
Exercise
sf org list shows your org as connected, but sf agent create fails with an insufficient access error. Name two things to check, and say how you would check the first.
Show answer
First, check that your user has a permission set that allows building agents. Query PermissionSetAssignment for your username with sf data query and look for the agent-related set. Second, check that Agentforce and generative AI are enabled in the org. A connected status only proves the token works, not that your user has the required permissions or that the features are on.
Exercise
Why does this tutorial pass --target-org on every command even though a default org can be set?
Show answer
An explicit target makes each command show which org it affects, and it overrides any default. This prevents sending a command, such as sf agent create, to the wrong org after switching projects.