---
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) 1 minute to read

The Mobile SDK enables you to easily implement Virtual and Live Agent chat services within your Android application.
You use the [NowChatSDK](https://servicenow-prod.fluidtopics.net/KH06EgrO6~MWXrIPFGMHbw#NowChatSDKAndroidAPI "The NowChatService class provides the function necessary to create a NowChatService that interacts with NowChat. NowChat provides the ability to embed Live Agent and Virtual Agent within your application.") API to create the chat service. Once the service is created, you must then start the chat user interface.

The following is a snippet that shows how to create the chat service and start the interface.

    /**
     * Helper class used to handle different Now service instances.
     */
    @Singleton
    class SdkManager @Inject constructor() {

        private var chatService: NowChatService? = null

        /**
         * Create the NowChatService once in the lifetime of the application, inside the Application class or another manager class
         * that will be injected into other classes via dagger/hilt.
         * NowChatService should be created after initializing the NowSDK.
         */
        suspend fun getNowChatService(): NowChatService? {
            if (chatService != null) return chatService

            return NowChatSDK.makeChatService(URL("https://instance-name.service-now.com"),
                object : NowChatSdkCallbacks {})
                .getOrThrow()
                .also { this.chatService = it }
        }
    }

    //Activity that will start the NowChat
    class MainActivity : AppCompatActivity() {

        @Inject
        lateinit var sdkManager: SdkManager

        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)

            //Start NowChat using the activity
            lifecycleScope.launch {
                sdkManager.getNowChatService()?.start(this@MainActivity)
            }
        }
    }

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-and-imp-chat__section_tcq_3tj_k1c}

You can pass chat context variables when starting a chat session by passing the contextData parameter in the [NowChatService-start()](https://servicenow-prod.fluidtopics.net/Q3l5E6Rkr2057VkSlu6ktQ#NChatServA-start_O_O "Launches the specified NowChat activity.") function. 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).  

    class MainActivity : AppCompatActivity() {
      @Inject

      lateinit var sdkManager: SdkManager

      override fun onCreate(savedInstanceState: Bundle?) { 
        super.onCreate(savedInstanceState)

        val contextData = mapOf("sys_id" to "123456789", "table" to "wm_task")
        //Start NowChat using the activity 
        lifecycleScope.launch {
          sdkManager.getNowChatService()?.start(this@MainActivity, contextData = contextData)
        }
      }
    }

## 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 start() call. For a list of elements that you can customize, see [NowChatService - launchIntent(context:Context, nowChatTheme:NowChatTheme):Intent](https://servicenow-prod.fluidtopics.net/Q3l5E6Rkr2057VkSlu6ktQ#NChatServA-launchIntent_O_O "Launches the intent used to open the NowChat activity. Typically used for creating a [android.app.PendingIntent]."). By default, the chat UI uses the nowUIColor theme for all NowSDK UI elements. Refer to the sample application for an example on how to apply a theme to your chat UI.

