---
sourceDocument: Xanadu API Reference
sourceDocumentLink: https://servicenow-prod.fluidtopics.net/r/xanadu/api-reference

 Release :

    - xanadu

ft:locale :

    - en-US

ft:publication_title :

    - Xanadu API Reference

ft:clusterId :

    - crapiref

bundleId :

    - crapiref

workflow :

    - Creator


---

# Implement Virtual and Live Agent chat

# Implement Virtual and Live Agent chat {#ariaid-title1}

* Release version: Xanadu
* 
* Updated August 1, 2024
* 
* ![](https://www.servicenow.com/docs/portal-asset/ico-clock) 2 minutes to read

The Mobile SDK enables you to easily implement Virtual and Live Agent chat services within your iOS application.
You use the [NowChatService](https://servicenow-prod.fluidtopics.net/V5g0WnyO4pFrtA3ShZvYew#NowChatServiceiOSAPI "The NowChatService class provides Virtual and Live Agent chat capabilities.") API to create the chat user interface. Once the UI is created, you
must then start the chat session, and then start the chat service. Similar to other Mobile SDK feature services, the NowChatService API provides
two implementations for some of its methods, including the startChat()
method. One implementation returns a [Combine](https://developer.apple.com/documentation/combine) publisher, and the other calls a completion handler with the return
results.

The following is a snippet from the sample application that shows how to initialize and
start a chat UI and session.  

    // Create the chat UI
    func makeChatScreen() -> UIViewController? {
      guard let chatService = chatService else { return nil }
      let result = chatService.makeChatUI(theme: CarrascoChatTheme(baseTheme: CarrascoTheme()))
      switch result {
      case .success(let chatViewController):
        return chatViewController
      case .failure(let error):
        debugPrint("Chat screen creation failed with error: \(error)")
        return nil
      }
    }
        
    // Start the chat session
    func startChat() {
      guard let chatService = chatService else {
        debugPrint("Chat service is invalid")
        viewState = makeViewState()
        return
      }
      chatService.startChat { [weak self] result in
        if case .failure(let error) = result {
          debugPrint("Chat session initialization failed with error: \(error)")
          self?.resetChat()
        }
      }
    }
        
    // Initialize the chat service
    private func initializeChatService() {
      NowChat.makeChatService(instanceUrl: instanceUrl, delegate: self) { [weak self] result in
        guard let self = self else { return }
                
        switch result {
        case .success(let service):
          self.chatService = service
        case .failure(let error):
          debugPrint("Creating the chat service failed with error: \(error)")
        }
        self.viewState = self.makeViewState()
      }
    }

Before you can leverage the chat functionality within your application, you must configure Virtual Agent within your ServiceNow instance. For details, see [Virtual Agent](https://www.servicenow.com/docs/access?context=virtual-agent-landing-page&version=xanadu&pubname=xanadu-conversational-interfaces&ft:locale=en-US).

## Passing context variables to Live Agent and Virtual Agent chat {#mobsdk-ios-imp-chat__section_vzg_wqj_k1c}

You can pass chat context variables when starting a chat session by passing the contextData parameter in the [NowChatService - startChat(contextData: \[String: Any\]?)](V5g0WnyO4pFrtA3ShZvYew#NChatServ-startChat "Starts a chat session.") or [NowChatService - startChat(contextData: \[String: Any\]? = nil, _ completion: @escaping (Result\<Void, NowChatServiceError\>))](V5g0WnyO4pFrtA3ShZvYew#NChatServ-startChat_O "Starts a chat session and executes a completion block after chat session has started.") functions. For additional information on chat context variables, see [Live agent chat context variables](https://www.servicenow.com/docs/access?context=live-agent-chat-context-vars&version=xanadu&pubname=xanadu-conversational-interfaces&ft:locale=en-US).  

    func startChat() {
      guard let chatService = chatService else {
        debugPrint("Chat service is invalid")
        viewState = makeViewState()
        return
      }
      let contextData = ["sys_id": "123456789", "table_name": "wm_task", "active": true] as [String: Any]
      chatService.startChat(contextData: contextData) { [weak self] result in
        if case .failure(let error) = result {
          debugPrint("Chat session initialization failed with error: \(error)")
          self?.resetChat()
        }
      }
    }

## Theme the chat user interface

You can customize the colors of the Live Agent and Virtual Agent chat UI by passing a theme object in the makeChatUI() call. For a list of all of the elements that you can customize, see [NowChatColoring protocol - iOS](https://servicenow-prod.fluidtopics.net/p0bcfy_PTXEK3dw47WjfGg#NowChatColoringiOSProtocol "The NowChatColoring protocol defines default colors for the elements in the Live Agent and Virtual Agent chat UI."). By default, the chat UI uses the NowUIColor theme for all NowSDK UI elements. Refer to the [NowChatThemeable protocol - iOS](https://servicenow-prod.fluidtopics.net/4ZwPJRhg90LWoWFnZCscrg "The NowChatThemeable protocol provides properties that enable you to override the colors used within chat pages hosted on your ServiceNow instance.") for sample code snippets on how to apply a theme to your chat UI.

