Connect to Direct Send APIs
To send transactional messages through Direct Send API, authenticate with an OAuth Bearer token that has the sfap_api scope. The same token works for every channel: email, SMS, WhatsApp, RCS, and push. To get the token, create an external client app and run an OAuth flow.
Direct Send APIs don’t accept a token from a normal user login. The token must come from an external client app that grants the sfap_api scope and issues JSON Web Token (JWT)-based access tokens.
- Access to Setup in your org, with permission to create external client apps. If you don’t have this access, ask your Salesforce admin to create the app and share its consumer key and secret with you.
- An active user to set as the app’s Run As user. The user needs API access and permission to create send definitions.
Create an external client app in Setup and configure its OAuth settings. The app generates the OAuth credentials that your application uses to request access tokens. Direct Send APIs require the sfap_api scope and JWT-based tokens.
-
In Setup, in the Quick Find box, search for and select External Client App Manager.
-
Click New External Client App.
-
Enter an External Client App Name and a Contact Email. Leave Distribution State set to Local.
-
Under API (Enable OAuth Settings), select Enable OAuth.
-
For Callback URL, enter an HTTPS URL, such as
https://login.salesforce.com/services/oauth2/callback. The callback URL must use HTTPS. -
Under OAuth Scopes, add these scopes to Selected OAuth Scopes.
- Access the Salesforce API Platform (sfap_api)
- Manage user data via APIs (api)
- Perform requests at any time (refresh_token, offline_access)
The
sfap_apiscope addshttps://api.salesforce.comto the token’s audience, which lets the Salesforce API gateway accept the call. Keep the list of scopes as short as possible. An app that requests too many scopes returnsinvalid_request: too many scopes requestedwhen you request a token. -
Under Flow Enablement, select Enable Client Credentials Flow, and then click OK to confirm.
-
Under Security, select Issue JSON Web Token (JWT)-based access tokens for named users.
The gateway validates scopes from the JWT payload. Without this setting, the gateway can’t verify the
sfap_apiscope. -
Click Create.
- On the detail page for the external client app, click the Settings tab, and then expand OAuth Settings.
- Click Consumer Key and Secret. The values open in a new tab.
- Copy the Consumer Key and Consumer Secret. Store the secret in an environment variable or a secrets manager, never in code that you commit.
Enable the client credentials flow in two places: in the OAuth settings from step 1, and in the app’s policies. The policies also set the Run As user.
-
On the detail page for the external client app, click the Policies tab, and then click Edit.
-
Under OAuth Flows and External Client App Enhancements, select Enable Client Credentials Flow.
-
For Run As (Username), enter the username of an active user who has API access.
The Run As user’s permissions apply to every call that you make with the token. To create send definitions, this user also needs permission to create them.
-
To test from your local computer, under App Authorization, for IP Relaxation, select Relax IP restrictions.
-
Click Save.
Relaxing IP restrictions widens where the app’s credentials can be used. Before you go to production, restrict the app to your server’s IP addresses. Your Salesforce admin decides which IP policy applies to the integration.
For server-to-server integrations, use the client credentials flow. Post the consumer key and secret to your org’s token endpoint on your My Domain host, such as mycompany.my.salesforce.com. Don’t use the lightning.force.com address.
Replace the placeholders with your own values.
YOUR_DOMAIN: Your org’s My Domain name.YOUR_CONSUMER_KEY: The consumer key from step 2.YOUR_CONSUMER_SECRET: The consumer secret from step 2.
Don’t send a scope parameter. With the client credentials flow, scopes come from the app’s configuration. A request that includes scope returns invalid_request: scope parameter not supported.
The response contains an access_token property.
The client credentials flow doesn’t return a refresh token. When a call returns 401, request a new token. Reuse a token until it expires instead of requesting a new one for every send.
For interactive apps where users log in with their own credentials, see Use the Authorization Code Flow for Interactive Apps.
Before you call the API, confirm that the token carries the sfap_api scope. A valid token is a JWT with three dot-separated segments. This command prints 2, the number of dots, for a JWT and 0 for an opaque token.
If the command prints 0, open the app in Setup and turn on Issue JSON Web Token (JWT)-based access tokens for named users. Then request a new token.
To read the token’s claims, decode the middle segment. JWTs use URL-safe Base64 without padding, and the base64 -d command on macOS can silently truncate the result, so use Python to decode it.
Confirm that the decoded payload contains these claims.
scpincludessfap_api.audincludeshttps://api.salesforce.com.mtyhas the valueoauth.
If scp doesn’t include sfap_api, add the scope to the app in step 1, and request a new token.
Pass the token in the Authorization header. Create send definitions on your My Domain host, and send messages to api.salesforce.com. This example sends an SMS. Replace YOUR_SEND_DEFINITION_ID with the ID that you get when you create a send definition.
For a test org, use https://test.api.salesforce.com/automation/actions360 as the base URL.
For interactive apps where users log in with their own Salesforce credentials, use the authorization code flow instead of the client credentials flow in step 4. The flow requires a browser, and API calls run with the logged-in user’s permissions.
-
Redirect the user to the authorization endpoint.
-
Exchange the returned authorization code for tokens.
The response contains an
access_tokenand arefresh_token. -
When the access token expires, use the refresh token to get a new one.
YOUR_CALLBACK_URL is the callback URL from step 1. It must match the app’s configuration exactly. Verify the access token the same way as in step 5.
| Error Message or Condition | Cause | Resolution |
|---|---|---|
invalid_grant: no client credentials user enabled | The client credentials flow isn’t fully set up. Usually, the Run As user is missing. | Enable the client credentials flow in both places, and set a Run As user. See Step 3. |
invalid_grant: authentication failure | The consumer key or secret is wrong, or the flow isn’t configured correctly. | Confirm the credentials. For the client credentials flow, confirm that the flow is enabled and that a Run As user is assigned. |
invalid_client: invalid client credentials | The consumer secret is wrong. | Copy the consumer secret again. |
invalid_client_id: client identifier invalid | The consumer key is wrong, or the app doesn’t exist in this org. | Copy the consumer key again, and confirm that you’re calling the right org’s My Domain host. |
invalid_request: scope parameter not supported | The client credentials request included a scope parameter. | Remove the scope parameter. Scopes come from the app’s configuration. |
redirect_uri_mismatch | The redirect_uri in an authorization code request doesn’t match the app’s callback URL. | Make the values match exactly, including the protocol, host, port, and path. |
INVALID_SESSION_ID or 401 response | The token is missing, expired, or invalid. | Request a new token. For the authorization code flow, use the refresh token. |
403 response | The token is valid but isn’t authorized for the call, or the channel isn’t available for your org. | Confirm the token’s scope. Then ask your Salesforce admin to check the Run As user’s access. |
| IP restriction error | The app enforces IP restrictions, and your IP address isn’t allowed. | Relax IP restrictions for testing, or ask your admin to add your IP address to the app’s trusted IP range. |
- Send Transactional Messages Through Direct Send API
- Handle Responses, Errors, and Retries in Direct Send API
- API Reference: Email - Preview and Send (Direct Send API)
- API Reference: SMS - Send (Direct Send API)
- API Reference: WhatsApp - Send (Direct Send API)
- API Reference: RCS - Send (Direct Send API)
- API Reference: Push - Send (Direct Send API)