Sub topic 3
Authorizing an org and checking permissions
Every sf agent command acts on a specific org, so authorization comes before generating or creating anything. This chapter shows how to authorize an org, confirm which org your commands will hit, and check the permissions an agent builder needs.
This chapter assumes you have installed the Salesforce CLI and its agent plugin (covered in the previous chapter). It also assumes you know the difference between production orgs, sandboxes, and scratch orgs.
How authorization works
When you run a login command, the CLI opens a browser, you sign in, and Salesforce returns a token. The CLI stores that token locally and links it to the org's username and, optionally, an alias. Later commands refer to the org by alias or username through --target-org.
Log in and name the org
Use an alias so you do not have to type a long username.
sf org login web --alias my-dev-org --set-default--aliasgives the org a short name for--target-org.--set-defaultmakes it the default target org for the current project, so you can leave out--target-org.- For a sandbox, add
--instance-url https://test.salesforce.com. Without it, the CLI sends you to the production login page.
For CI or other machines with no browser, use a non-interactive method such as sf org login jwt or sf org login sfdx-url. The CI chapter covers these.
Verify which org you are using
Check the target before you create anything. Agents are metadata, so creating one in the wrong org is an easy mistake.
sf org list
sf org display --target-org my-dev-org
sf config get target-orgsf org list shows every authorized org and marks the default. sf org display prints the username, instance URL, and org ID. sf config get target-org shows which org is used when you omit the flag.
Check the org and user permissions
A successful login does not mean you can build agents. Two things must be true:
- The org has the features turned on. Einstein generative AI and Agentforce must be enabled in Setup. Sandboxes and scratch orgs may need this done explicitly, and a scratch org also depends on its definition file.
- Your user has the access an agent builder needs. Your org's admin assigns this through permission sets. Exact names depend on your release, so check Salesforce's current documentation for the agent builder permissions.
You can list the permission sets assigned to your user with a SOQL query through the CLI.
sf data query \
--query "SELECT PermissionSet.Name, PermissionSet.Label FROM PermissionSetAssignment WHERE Assignee.Username = '[email protected]'" \
--target-org my-dev-orgReplace the username with the one shown by sf org display. This query is read-only, so it is safe to run in any org you can query.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Browser shows a login failure for a sandbox | Missing instance URL | Log in again with `--instance-url https://test.salesforce.com` |
| `No authorization information found` for an alias | Alias misspelled or org never authorized | Run `sf org list` and correct the alias |
| Command runs but hits the wrong org | Another default is set | Pass `--target-org` explicitly |
| Agent commands fail with an access or feature error | Agentforce not enabled, or permissions missing | Enable the features in Setup and assign permission sets |
| Commands that worked now fail with an expired session | Token expired or revoked | Run `sf org login web` again |
Exercise
You run sf agent create and the agent appears in a different org from the one you meant. Name two commands that would have caught this before the create, and say what each shows.
Show answer
sf config get target-org shows which org is used when --target-org is omitted. sf org list marks the default org and lists every authorized org. sf org display --target-org <alias> also shows the username and instance URL of the org a command will reach. Passing --target-org explicitly on every command avoids the problem.