Create a Connected App in Your Org
Salesforce CLI requires a connected app in the org that you're authorizing. A connected
app is a framework that enables an external application, in this case Salesforce CLI, to
integrate with Salesforce using APIs and standard protocols, such as OAuth. We provide a default
connected app when you authorize an org with the org login
web command. For extra security, you can create your own connected app in your org
using Setup and configure it with the settings of your choice. You're required to create a
connected app when authorizing the org with the org login
jwt command.
- Log in to your org.
- From Setup, enter App Manager in the Quick Find box, then select App Manager.
- In the top-right corner, click New Connected App.
- Click Create a Connected App, then click Continue.
- Update the basic information as needed, such as the connected app name and your email address.
- Select Enable OAuth Settings.
-
For the callback URL, enter
http://localhost:1717/OauthRedirect.
If port 1717 (the default) is already in use on your local machine, specify an available one instead. Then update your sfdx-project.json file by setting the oauthLocalPort property to the new port. For example, if you set the callback URL to http://localhost:1919/OauthRedirect:
"oauthLocalPort" : "1919"
- (Required for JWT) Select Use digital signatures.
- (Required for JWT) Click Choose File and upload file that contains your digital certificate, such as server.crt.
-
Add these OAuth scopes:
- Manage user data via APIs (api)
- Manage user data via Web browsers (web)
- Perform requests at any time (refresh_token, offline_access)
- Click Save, then Continue.
-
Click Manage Consumer Details.
If prompted, verify your identity by entering the verification code that was automatically sent to your email address.
- Click Copy next to Consumer Key because you need it later when you run an org login command.
- Click Back to Manage Connected Apps.
- Click Manage.
- Click Edit Policies.
-
In the OAuth Policies section, for the Refresh Token Policy field, click
Expire refresh token after: and enter 90 days or less.
Setting a maximum of 90 days for the refresh token expiration is a security best practice. To continue running CLI commands against an org whose refresh tokens have expired, reauthorize it with the org login web or org login jwt command.
-
In the Session Policies section, set Timeout Value to
15 minutes.
Setting a timeout for access tokens is a security best practice. Salesforce CLI automatically handles an expired access token by referring to the refresh token.
- (Required for JWT) In the OAuth Policies section, select Admin approved users are pre-authorized for permitted users, and click OK.
- Click Save.
- (Required for JWT) Click Manage Profiles, select the profiles that are pre-authorized to use this connected app, and click Save. Similarly, click Manage Permission Sets to select the permission sets. Create permission sets if necessary.
To specify the consumer key, use the --client-id
flag of the org login commands. For example, if your
consumer key is 04580y4051234051 and you’re authorizing a Dev Hub org by logging into it from
a browser, run this command in a terminal (macOS and Linux) or command prompt
(Windows):
sf org login web --client-id 04580y4051234051 --set-default-dev-hub --alias my-hub-org
See the reference for org login web and org login jwt for more examples.