# Salesforce API keys: find, store, rotate, and use them safely with AI agents

Source: https://botpassword.com/keys/salesforce
Last checked: 2026-11-08

Salesforce API access uses OAuth rather than a static API key: you create an external client app in Setup, copy its consumer key and consumer secret, and get access and refresh tokens through an OAuth flow. Connected app creation is restricted as of Spring '26, so Salesforce recommends external client apps for new integrations, and the Salesforce CLI signs in with `sf org login web`.

## Key types

| Type | Prefix | Secret | Scope | Expiry |
|---|---|---|---|---|
| Consumer key | — | No | Identifies the external client app or connected app (the OAuth client ID) | — |
| Consumer secret | — | Yes | Authenticates the app to the token endpoint (the OAuth client secret) | — |
| OAuth access token | — | Yes | The app's OAuth scopes combined with the authorizing user's permissions | — |
| OAuth refresh token | — | Yes | Obtains new access tokens; requires the `refresh_token` scope | Valid indefinitely by default; set **Refresh Token Validity** in the app's OAuth policies |
| Security token | — | Yes | Appended to a user's password for API and desktop-client logins from outside trusted IP ranges | — |

## How to create a Salesforce key

1. From Setup, enter **external client** in the Quick Find box and select **External Client App Manager**.
2. Click **New External Client App** and fill out **Basic Information**.
3. Expand **API (Enable OAuth Settings)**, tick **Enable OAuth**, enter the callback URL and add only the OAuth scopes the integration needs.
4. Click **Create**. A new external client app can take up to 30 minutes to become available.
5. Click **Settings**, then **Consumer Key and Secret** under **OAuth Settings**, and copy the values into a secrets vault.

## Limiting what the key can do

Choose only the OAuth scopes the integration needs on the external client app, for example `refresh_token` for offline access and `mcp_api` only for MCP clients. In the app's **OAuth Policies** you can require a permission set for pre-authorization, restrict connections to IP ranges, and shorten refresh token validity. Salesforce suggests 30 days or less and turning on **Refresh Token Rotation**.

## How to rotate a Salesforce key

1. From Setup, enter **Apps** in Quick Find, select **App Manager**, find the connected app and select **View**.
2. Next to **Consumer Key and Secret**, click **Manage Consumer Details** and verify your identity.
3. Under **Staged Consumer Details**, click **Generate** and share the staged values with the app's integrations.
4. When the integrations are ready, click **Apply**, then **Apply** again to accept the warning.

## How to revoke a Salesforce key

1. POST `token=` with the access or refresh token to `https://MyDomainName.my.salesforce.com/services/oauth2/revoke`. Revoking a refresh token also revokes its access tokens.
2. For a Salesforce CLI session, run `sf org logout --target-org <alias>` (or `--all`) to remove the stored authorization.
3. For a leaked security token, reset it under personal settings with **Reset My Security Token**; the old token stops working.

## If the key leaked

1. Revoke the exposed refresh or access tokens at the `/services/oauth2/revoke` endpoint.
2. If the consumer secret leaked, generate and apply new consumer details; applying them revokes every existing token for the app.
3. Review the org's login history for sessions you do not recognise.
4. Restrict the app with IP ranges and a required permission set before re-issuing credentials.
5. GitHub secret scanning detects Salesforce access tokens, refresh tokens and consumer keys and secrets in pushes, and reports Marketing Cloud API OAuth2 tokens to Salesforce as a scanning partner.

## Gotchas

- New connected apps can no longer be created by default as of Spring '26; use an external client app.
- Applying rotated consumer details revokes every access and refresh token for the app, so users must re-authorize.
- Do not use a sandbox as the source org for a production app; deleting or refreshing the sandbox can make the consumer details unusable.
- The client credentials flow does not accept `login.salesforce.com` or `test.salesforce.com`; use your My Domain URL.

## Using this key with AI agents (BotPassword)

- `cli_login`: Lets git, Docker, the AWS CLI and kubectl log in through bpw's credential helper, so the token is fetched when the tool needs it and not written into its config. The helper hands the value to the tool, so the grant is always disclosing. Other tools use run_with; OAuth device logins are planned.
- `run_with`: Runs a command with the key injected as an environment variable or a 0600 temporary file, never on the command line, and redacts the key from the output.
- `rotate`: Partly built: creates a new key, stores it as pending, verifies it, promotes it, then revokes the old key after a grace period. Today it works for Cloudflare tokens and AWS IAM keys you have set up, and an agent can start it only under a grant with autopilot; other providers cannot be rotated through their API yet.

## FAQ

### Does Salesforce have a simple API key?

No. Salesforce API access uses OAuth tokens issued to an external client app or connected app, identified by a consumer key and consumer secret.

### Where do I find my Salesforce consumer key and secret?

In Setup, open **External Client App Manager**, select the app, click **Settings**, then **Consumer Key and Secret** under **OAuth Settings**.

### How do I rotate a Salesforce consumer secret?

In App Manager, view the connected app, click **Manage Consumer Details**, **Generate** staged values, then **Apply** them. Applying revokes all existing tokens for the app.

### How do I reset my Salesforce security token?

In personal settings, search Quick Find for **Reset**, select **Reset My Security Token** and click **Reset Security Token**. The new token is emailed to you.

### How does the Salesforce CLI sign in?

Run `sf org login web --alias my-org` and sign in in the browser window that opens. Use `--instance-url` for a My Domain or sandbox login URL.

### Can an AI agent use my Salesforce key without seeing it?

Yes, if the key never enters the agent's context. Instead of pasting the key into a chat or leaving it in a `.env` file the agent can read, keep it in a credential broker that injects it into the command or request on the agent's behalf. BotPassword does this with `run_with`, so the agent gets the result and never the value.

## Sources

- https://developer.salesforce.com/docs/platform/hosted-mcp-servers/guide/create-external-client-app.html
- https://help.salesforce.com/s/articleView?id=xcloud.connected_app_rotate_consumer_details.htm&type=5
- https://help.salesforce.com/s/articleView?id=xcloud.remoteaccess_revoke_token.htm&type=5
- https://help.salesforce.com/s/articleView?id=xcloud.remoteaccess_oauth_flow_errors.htm&type=5
- https://help.salesforce.com/s/articleView?id=xcloud.user_security_token.htm&type=5
- https://developer.salesforce.com/docs/platform/sfdx-dev/guide/sfdx-dev-auth-web-flow.html
- https://developer.salesforce.com/docs/platform/sfdx-dev/guide/sfdx-dev-auth-logout.html
- https://developer.salesforce.com/docs/platform/sfdx-dev/guide/sfdx-dev-mcp.html
- https://docs.github.com/en/code-security/secret-scanning/introduction/supported-secret-scanning-patterns
