# Integrate Agentforce SDK

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

```sfdocs-code {"lang":"kotlin", "title": "Agentforce SDK Import"}
import com.salesforce.android.agentforceservice.*
import com.salesforce.android.agentforcesdk.components.theme.AgentforceThemeMode
import com.salesforce.android.agentforcesdk.components.theme.AgentforceTheming
import com.salesforce.android.agentforcesdk.components.theme.createCustomAgentforceThemeManager
```

## 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.

```sfdocs-code {"lang":"kotlin", "title": "Sample OAuth Implementation"}
class MyAppCredentialProvider : AgentforceAuthCredentialProvider {
   override fun getAuthCredentials(): AgentforceAuthCredentials {
       return AgentforceAuthCredentials.OAuth(
           authToken = "YOUR_AUTH_TOKEN",
           orgId = "YOUR_ORG_ID",
           userId = "YOUR_USER_ID"
       )
   }
}
```

```sfdocs-code {"lang":"kotlin", "title": "Sample OrgJWT Implementation"}
class MyAppCredentialProvider : AgentforceAuthCredentialProvider {
   override fun getAuthCredentials(): AgentforceAuthCredentials {
       return AgentforceAuthCredentials.OrgJWT(orgJWT = "YOUR_ORG_JWT")
   }
}
```

```sfdocs-code {"lang":"kotlin", "title": "Sample Guest User Implementation"}
class MyAppCredentialProvider : AgentforceAuthCredentialProvider {
   override fun getAuthCredentials(): AgentforceAuthCredentials {
       return AgentforceAuthCredentials.Guest(url = "YOUR_GUEST_URL")
   }
}
```

Related links:

- *Salesforce Mobile SDK Developer Guide*: [OAuth 2.0 Authentication Flow](https://developer.salesforce.com/docs/platform/mobile-sdk/guide/oauth-intro-flow.md)
- *Salesforce Help*: [JWT-Based Access Tokens](https://help.salesforce.com/s/articleView?id=xcloud.jwt_access_tokens.htm\&type=5)

## 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](https://help.salesforce.com/s/articleView?id=ai.agent_setup_type.htm) in Salesforce Help.

:::tip
For guidance on gathering information for the agent configuration object, see [Agent Type Setup](/docs/ai/agentforce-mobile-sdk/guide/agentforce-mobile-sdk-before-you-begin.md#agent-type) in Before You Begin.
:::

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

```sfdocs-code {"lang":"kotlin", "title": "Employee Agent Configuration"}
// Configure an Employee agent
val configuration = AgentforceConfiguration.builder(
    authCredentialProvider = myAuthCredentialProvider
)
    .setApplication(application)
    .setSalesforceDomain("https://your-domain.my.salesforce.com")
    .setAgentId("YOUR_AGENT_ID")
    .setTheming(
        AgentforceTheming.CustomManager(
            createCustomAgentforceThemeManager(themeMode = AgentforceThemeMode.SYSTEM)
        )
    )
    .setNetwork(myNetworkProvider)
    .setLogger(myLogger)
    .setNavigation(myNavigation)
    .build()

// Set mode for Employee agent
val agentforceMode = AgentforceMode.FullConfig(configuration)

// Create client
agentforceClient?.init(
    authCredentialProvider = configuration.authCredentialProvider,
    agentforceMode = agentforceMode,
    application = application
)
```

```sfdocs-code {"lang":"kotlin", "title": "Service Agent Configuration"}
// Configure a Service agent
val agentforceMode = AgentforceMode.ServiceAgent(
    serviceAgentConfiguration = ServiceAgentConfiguration.builder(
        serviceApiURL = "https://your-service-api-url.com",
        organizationId = "YOUR_ORG_ID",
        esDeveloperName = "YOUR_CHAT_DEVELOPER_NAME"
    ).build()
)

// Create client
agentforceClient?.init(
    authCredentialProvider = configuration.authCredentialProvider,
    agentforceMode = agentforceMode,
    application = this.application
)
```

```sfdocs-code {"lang":"kotlin", "title": "Full Configuration"}
val config = AgentforceConfiguration.builder(
   authCredentialProvider =
       object : AgentforceAuthCredentialProvider {
           override fun getAuthCredentials(): AgentforceAuthCredentials {
               return AgentforceAuthCredentials.OAuth(
                   authToken = "YOUR_AUTH_TOKEN",
                   orgId = "YOUR_ORG_ID",
                   userId = "YOUR_USER_ID"
               )
           }
       }
).setApplication(application)
   .setSalesforceDomain("https://your-domain.my.salesforce.com")
   .setNetwork(myNetworkProvider)
   .setLogger(myLogger)
   .setNavigation(myNavigation)
   .setDataProvider(myDataProvider)
   .setFeatureFlagSettings(
       AgentforceFeatureFlagSettings.builder().build()
   )
   .setCameraUriProvider(myCameraUriProvider)
   .setVoiceManaging(myAgentforceVoiceManaging)
   .setInstrumentationHandler(myAgentforceInstrumentationHandler)
   .setUser(myUser)
   .setPermission(myPermission)
   .setTheming(AgentforceTheming.CustomManager(myAgentforceThemeManager))
   .setViewProvider(myAgentforceViewProvider)
   .build()

val agentforceMode = AgentforceMode.FullConfig(config)
agentforceClient?.init(authCredentialProvider = config.authCredentialProvider, agentforceMode = agentforceMode, application = application)
```

:::note
As of SDK version 262.3, `AgentforceVoiceManaging` (`myAgentforceVoiceManaging` above) no longer has a default implementation — the SDK doesn't provide SDK-managed speech-to-text out of the box. If you don't need custom speech-to-text, omit `setVoiceManaging` and users can dictate into the chat input using the platform's native keyboard mic instead. See [Set Up Agentforce Voice for Android](/docs/ai/agentforce-mobile-sdk/guide/agentforce-mobile-sdk-android-voice.md#speech-to-text-for-chat-input-agentforcevoicemanaging) for details.
:::

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.

```sfdocs-code {"lang":"kotlin", "title": "Start Conversation"}
// Start the conversation
agentforceClient.startAgentforceConversation()

// Get the conversation session
val session = agentforceClient.fetchAgentforceSession(YOUR_AGENT_ID)
```

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

## Next Steps

- **Prebuilt UI**: See [Use Prebuilt UI](/docs/ai/agentforce-mobile-sdk/guide/agentforce-mobile-sdk-android-prebuilt-ui.md) for ready-to-use chat interface
- **Headless**: See [Build a Headless Experience](/docs/ai/agentforce-mobile-sdk/guide/agentforce-mobile-sdk-android-headless.md) for custom UI implementation
- **Customization**: See [Branding and Theming](/docs/ai/agentforce-mobile-sdk/guide/agentforce-mobile-sdk-android-branding-theming.md) for visual customization
- **Custom Lightning Types**: See [Custom Lightning Types on Mobile with the Agentforce Mobile SDK](/docs/ai/agentforce-mobile-sdk/guide/agent-sdk-mobile-custom-lightning-types.md) for native CLT renderers and editors
