# Integrate Agentforce SDK

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

```sfdocs-code {"lang":"swift", "title": "Agentforce SDK Import"}
import AgentforceSDK
```

## Implement Credential Provider

The Agentforce Mobile SDK has to make API calls back to your org to leverage the Agentforce Platform. In order to do so, it requires authenticated credentials, and delegates this responsibility to the app. A specific app might be using the Salesforce Mobile SDK to authenticate, or may have implemented their own OAuth client. The consuming app must implement the `AgentforceAuthCredentialProviding` protocol and provide these credentials to the SDK.

**AgentforceAuthCredentialProviding**: Supplies the SDK with the authentication token required to communicate securely with Salesforce APIs.

```sfdocs-code {"lang":"swift", "title": "Sample OAuth Implementation"}
class MyAppCredentialProvider: AgentforceAuthCredentialProviding {
    public func getAuthCredentials() -> AgentforceAuthCredentials {
        return .OAuth(
            authToken: "YOUR_AUTH_TOKEN",
            orgId: "YOUR_ORG_ID",
            userId: "YOUR_USER_ID"
        )
    }
}
```

```sfdocs-code {"lang":"swift", "title": "Sample JWT Implementation"}
class MyAppCredentialProvider: AgentforceAuthCredentialProviding {
    public func getAuthCredentials() -> AgentforceAuthCredentials {
        return .OrgJWT(orgJWT: "YOUR_ORG_JWT")
    }
}
```

```sfdocs-code {"lang":"swift", "title": "Sample Guest User Implementation"}
class MyAppCredentialProvider: AgentforceAuthCredentialProviding {
    public func getAuthCredentials() -> AgentforceAuthCredentials {
        return .Guest(url: "https://your-domain.my.salesforce.com")
    }
}
```

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":"swift", "title": "Employee Agent Configuration"}
let user = User(userId: "YOUR_USER_ID", org: Org(id: "YOUR_ORG_ID"), displayName: "User's Display Name")

// Configure an Employee agent
let config = EmployeeAgentConfiguration(
    user: user,
    forceConfigEndpoint: "https://your-domain.my.salesforce.com"
)

// Create client
let client = AgentforceClient(
   credentialProvider: credentialProvider,
   mode: .employeeAgent(config)
)
```

```sfdocs-code {"lang":"swift", "title": "Service Agent Configuration"}
// Set up Service UI settings
let serviceUISettings = ServiceUISettings(
    downloadTranscript: false, // Whether to show the download transcript option
    endConversation: true      // Whether to show the end conversation option
)

// Configure a Service agent
let config = ServiceAgentConfiguration(
    esDeveloperName: "YOUR_CHAT_DEVELOPER_NAME",
    organizationId: "YOUR_ORG_ID",
    serviceApiURL: "https://your-service-api-url.com",
    serviceUISettings: serviceUISettings
)

// Create client
let serviceClient = AgentforceClient(
    credentialProvider: credentialProvider,
    mode: .serviceAgent(config)
)
```

`AgentforceClient` must be retained for the duration of the conversation. If it's deallocated, the session will be lost. Instantiate it in an object with an appropriate lifecycle for your needs.

## Start a Conversation

Once you have initialized `AgentforceClient`, you can start a conversation with an agent using the `startAgentforceConversation` method, this method is slightly different depending on whether you're targeting a Service Agent or an Employee Agent. For guidance on getting values for the `createAgentforceChatView` method, see [Agent Type Setup](/docs/ai/agentforce-mobile-sdk/guide/agentforce-mobile-sdk-before-you-begin.md#agent-type).

```sfdocs-code {"lang":"swift", "title": "Employee Agent Start Conversation"}
let employeeConversation = agentforceClient.startAgentforceConversation(
    forAgentId: "YOUR_AGENT_ID"
)
```

```sfdocs-code {"lang":"swift", "title": "Service Agent Start Conversation"}
let serviceConversation = agentforceClient.startAgentforceConversation(
    forESDeveloperName: "YOUR_CHAT_DEVELOPER_NAME"
)
```

This method returns an `AgentConversation` object, which represents a single conversation with an agent. The `AgentConversation` object can be used for headless or bring your own UI experiences, and is also the central input when using one of the built-in UIs.

## Next Steps

- **Prebuilt UI**: See [Use Prebuilt UI](/docs/ai/agentforce-mobile-sdk/guide/agentforce-mobile-sdk-ios-prebuilt-ui.md) for ready-to-use chat interface
- **Headless**: See [Build a Headless Experience](/docs/ai/agentforce-mobile-sdk/guide/agentforce-mobile-sdk-ios-headless.md) for custom UI implementation
- **Customization**: See [Theming](/docs/ai/agentforce-mobile-sdk/guide/agentforce-mobile-sdk-ios-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
