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.
Salesforce key types
| Type | Prefix | Secret? | Scope | Expiry |
|---|---|---|---|---|
| Consumer key | — | No | Identifies the external client app or connected app (the OAuth client ID) | — |
| Consumer secret Salesforce hides the consumer key and secret from users whose profile lacks the needed permissions. | — | Yes | Authenticates the app to the token endpoint (the OAuth client secret) | — |
| OAuth access token Can be opaque or a JSON Web Token, depending on the app's security settings. | — | 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 Not shown in settings; the only way to get one is to reset it. | — | Yes | Appended to a user's password for API and desktop-client logins from outside trusted IP ranges | — |
How to create a Salesforce key
- From Setup, enter external client in the Quick Find box and select External Client App Manager.
- Click New External Client App and fill out Basic Information.
- Expand API (Enable OAuth Settings), tick Enable OAuth, enter the callback URL and add only the OAuth scopes the integration needs.
- Click Create. A new external client app can take up to 30 minutes to become available.
- 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
- From Setup, enter Apps in Quick Find, select App Manager, find the connected app and select View.
- Next to Consumer Key and Secret, click Manage Consumer Details and verify your identity.
- Under Staged Consumer Details, click Generate and share the staged values with the app's integrations.
- When the integrations are ready, click Apply, then Apply again to accept the warning.
Applying new consumer details cannot be reverted and revokes all existing access and refresh tokens for the app. For up to 10 minutes afterwards, the old and new values both work intermittently; after that only the new values work. Rotation needs the Allow consumer key and secret rotation permission.
How to revoke a Salesforce key
- POST
token=with the access or refresh token tohttps://MyDomainName.my.salesforce.com/services/oauth2/revoke. Revoking a refresh token also revokes its access tokens. - For a Salesforce CLI session, run
sf org logout --target-org <alias>(or--all) to remove the stored authorization. - For a leaked security token, reset it under personal settings with Reset My Security Token; the old token stops working.
If your Salesforce key leaked
- Revoke the exposed refresh or access tokens at the
/services/oauth2/revokeendpoint. - If the consumer secret leaked, generate and apply new consumer details; applying them revokes every existing token for the app.
- Review the org's login history for sessions you do not recognise.
- Restrict the app with IP ranges and a required permission set before re-issuing credentials.
- 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.
Environment variables and CLI
Salesforce CLI (sf): sf org login web --alias my-org opens a browser to sign in; sf org logout removes a stored authorization
Things that catch people out
- 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.comortest.salesforce.com; use your My Domain URL.
Common Salesforce authentication errors
invalid_grant
The authorization code, user credentials, assertion or audience is invalid, the user has not approved the app, the login is IP restricted, or the refresh token has expired.
invalid_client
The consumer secret is invalid.
invalid_client_id
The consumer key (client identifier) is invalid.
Recent incidents that put Salesforce keys at risk
If you were affected by one of these, rotate your Salesforce keys using the rotation steps above.
- Klue integration breach: a leaked GitHub PAT led to theft of customers' Salesforce OAuth tokens 2026-06-18
- Old Gainsight OAuth tokens were reused to call Salesforce APIs; Salesforce revoked the connection 2025-11-20
- Salesloft Drift OAuth tokens were stolen and used to export Salesforce data and hunt for secrets 2025-08-21
Using your Salesforce key with AI agents
A coding agent that can read your .env file or your shell history can read the key, and anything in its context can end up in logs, transcripts or a prompt-injected request. The safer pattern is to keep the key in a credential broker that performs the authenticated step for the agent and hands back only the result.
BotPassword works this way. For a Salesforce key, these intents fit:
| Intent | What BotPassword does |
|---|---|
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. |
Salesforce also publishes its own MCP server. Whatever server the agent uses, keep the credential it authenticates with out of the agent's context.
Set it up in your agent: Aider · Amp · Claude Code · Claude Desktop · Cline · OpenAI Codex CLI · all agents
Frequently asked questions
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.