Integrate Agentforce SDK

To use the SDK, be sure to import the Agentforce SDK packages.

Agentforce SDK Import
1import com.salesforce.android.agentforceservice.*

Implement Core Service Interfaces 

The Agentforce Mobile SDK is designed for flexibility and delegates several core responsibilities to the host application. You must create concrete implementations for the following interfaces:

  • AgentforceAuthCredentialProvider: Supplies the SDK with the authentication token (e.g., OAuth 2.0 access token) required to communicate securely with Salesforce APIs.
  • AgentforceInstrumentation: A handler for the SDK to emit instrumentation and telemetry data into your app’s analytics system.

For authentication, create a credential provider using one of the supported authentication methods.

Sample OAuth Implementation
1class MyAppCredentialProvider : AgentforceAuthCredentialProvider {
2   override fun getAuthCredentials(): AgentforceAuthCredentials {
3       return AgentforceAuthCredentials.OAuth(
4           authToken = "YOUR_AUTH_TOKEN",
5           orgId = "YOUR_ORG_ID",
6           userId = "YOUR_USER_ID"
7       )
8   }
9}
Sample OrgJWT Implementation
1class MyAppCredentialProvider : AgentforceAuthCredentialProvider {
2   override fun getAuthCredentials(): AgentforceAuthCredentials {
3       return AgentforceAuthCredentials.OrgJWT(orgJWT = "YOUR_ORG_JWT")
4   }
5}
Sample Guest User Implementation
1class MyAppCredentialProvider : AgentforceAuthCredentialProvider {
2   override fun getAuthCredentials(): AgentforceAuthCredentials {
3       return AgentforceAuthCredentials.Guest(url = "YOUR_GUEST_URL")
4   }
5}

Related links:

Configure Agent 

In order to use the Agentforce SDK you must first identify the agent type you are targeting: Employee Agent or Service Agent. A single app may target each, but requires configuration and instantiation of separate Agentforce clients. To learn more, see Create an Agent from a Template in Salesforce Help.

For guidance on gathering information for the agent configuration object, see Agent Type Setup in Before You Begin.

Tip

The following snippets illustrate how to configure your agent for each agent type.

Employee Agent Configuration
1// Configure an Employee agent
2val configuration = AgentforceConfiguration.builder(
3    authCredentialProvider = myAuthCredentialProvider
4)
5    .setApplication(application)
6    .setSalesforceDomain("https://your-domain.my.salesforce.com")
7    .setAgentId("YOUR_AGENT_ID")
8    .setThemeManager(createCustomAgentforceThemeManager(themeMode = AgentforceThemeMode.SYSTEM))
9    .setNetwork(myNetworkProvider)
10    .setLogger(myLogger)
11    .setNavigation(myNavigation)
12    .build()
13
14// Set mode for Employee agent
15val agentforceMode = AgentforceMode.FullConfig(configuration)
16
17// Create client
18agentforceClient?.init(
19    authCredentialProvider = configuration.authCredentialProvider,
20    agentforceMode = agentforceMode,
21    application = application
22)
Service Agent Configuration
1// Configure a Service agent
2val agentforceMode = AgentforceMode.ServiceAgent(
3    serviceAgentConfiguration = ServiceAgentConfiguration.builder(
4        serviceApiURL = "https://your-service-api-url.com",
5        organizationId = "YOUR_ORG_ID",
6        esDeveloperName = "YOUR_CHAT_DEVELOPER_NAME"
7    ).build()
8)
9
10// Create client
11agentforceClient?.init(
12    authCredentialProvider = configuration.authCredentialProvider,
13    agentforceMode = agentforceMode,
14    application = this.application
15)
Full Configuration
1val config = AgentforceConfiguration.builder(
2   authCredentialProvider =
3       object : AgentforceAuthCredentialProvider {
4           override fun getAuthCredentials(): AgentforceAuthCredentials {
5               return AgentforceAuthCredentials.OAuth(
6                   authToken = "YOUR_AUTH_TOKEN",
7                   orgId = "YOUR_ORG_ID",
8                   userId = "YOUR_USER_ID"
9               )
10           }
11       }
12).setApplication(application)
13   .setSalesforceDomain("https://your-domain.my.salesforce.com")
14   .setNetwork(myNetworkProvider)
15   .setLogger(myLogger)
16   .setNavigation(myNavigation)
17   .setDataProvider(myDataProvider)
18   .setFeatureFlagSettings(
19       AgentforceFeatureFlagSettings.builder().build()
20   )
21   .setCameraUriProvider(myCameraUriProvider)
22   .setVoiceManaging(myAgentforceVoiceManaging)
23   .setInstrumentationHandler(myAgentforceInstrumentationHandler)
24   .setUser(myUser)
25   .setPermission(myPermission)
26   .setThemeManager(myAgentforceThemeManager)
27   .setViewProvider(myAgentforceViewProvider)
28   .build()
29
30val agentforceMode = AgentforceMode.FullConfig(config)
31agentforceClient?.init(authCredentialProvider = config.authCredentialProvider, agentforceMode = agentforceMode, application = application)

Create and retain an instance of AgentforceClient. It’s best to hold this in a lifecycle-aware component, like a ViewModel, to ensure the conversation state persists across configuration changes.

Start a Conversation 

Once you’ve initialized the AgentforceClient, you can start a conversation with an agent using the startAgentforceConversation method.

Start Conversation
1// Start the conversation
2agentforceClient.startAgentforceConversation()
3
4// Get the conversation session
5val session = agentforceClient.fetchAgentforceSession(YOUR_AGENT_ID)

This method returns an AgentforceConversation object, which represents a single conversation with an agent.

Next Steps