Integrate Agentforce SDK

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

Agentforce SDK Import
1import 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.

Sample OAuth Implementation
1class MyAppCredentialProvider: AgentforceAuthCredentialProviding {
2    public func getAuthCredentials() -> AgentforceAuthCredentials {
3        return .OAuth(
4            authToken: "YOUR_AUTH_TOKEN",
5            orgId: "YOUR_ORG_ID",
6            userId: "YOUR_USER_ID"
7        )
8    }
9}
Sample JWT Implementation
1class MyAppCredentialProvider: AgentforceAuthCredentialProviding {
2    public func getAuthCredentials() -> AgentforceAuthCredentials {
3        return .OrgJWT(orgJWT: "YOUR_ORG_JWT")
4    }
5}
Sample Guest User Implementation
1class MyAppCredentialProvider: AgentforceAuthCredentialProviding {
2    public func getAuthCredentials() -> AgentforceAuthCredentials {
3        return .Guest("")
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
1let user = User(userId: "YOUR_USER_ID", org: Org(id: "YOUR_ORG_ID"), displayName: "User's Display Name")
2
3// Configure an Employee agent
4let config = EmployeeAgentConfiguration(
5    user: user,
6    forceConfigEndpoint: "https://your-domain.my.salesforce.com"
7)
8
9// Create client
10let client = AgentforceClient(
11   credentialProvider: credentialProvider,
12   mode: .employeeAgent(config)
13)
Service Agent Configuration
1// Set up Service UI settings
2let serviceUISettings = ServiceUISettings(
3    downloadTranscript: false, // Whether to show the download transcript option
4    endConversation: true      // Whether to show the end conversation option
5)
6
7// Configure a Service agent
8let config = ServiceAgentConfiguration(
9    esDeveloperName: "YOUR_CHAT_DEVELOPER_NAME",
10    organizationId: "YOUR_ORG_ID",
11    serviceApiURL: "https://your-service-api-url.com",
12    serviceUISettings: serviceUISettings
13)
14
15// Create client
16let serviceClient = AgentforceClient(
17    credentialProvider: credentialProvider,
18    mode: .serviceAgent(config)
19)

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.

Employee Agent Start Conversation
1let employeeConversation = agentforceClient.startAgentforceConversation(
2    forAgentId: "YOUR_AGENT_ID"
3)
Service Agent Start Conversation
1let serviceConversation = agentforceClient.startAgentforceConversation(
2    forESDeveloperName: "YOUR_CHAT_DEVELOPER_NAME"
3)

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