---
sourceDocument: Brazil API Reference
sourceDocumentLink: https://www.servicenow.com/docs/r/api-reference

 Release :

    - brazil

ft:locale :

    - en-US

ft:publication_title :

    - Brazil API Reference

ft:clusterId :

    - crapiref

bundleId :

    - crapiref

workflow :

    - Creator


---

# Passing context variables to Live Agent and Virtual Agent chat

# Passing context variables to Live Agent and Virtual Agent chat {#ariaid-title1}

Release version: Brazil  
Updated September 10, 2026  
![](https://www.servicenow.com/docs/portal-asset/ico-clock) 2 minutes to read
Summarize  
![AI sparkle icon](https://servicenow.com/docs/portal-asset/ai-sparkle-icon) Summarized using AI  
This content was generated using new OpenAI-powered functionality. Results are provided on an as is basis and are not guaranteed to be accurate or complete.  

## Summary of Passing context variables to Live Agent and Virtual Agent chat

This feature enables ServiceNow customers to pass context variables and customize chat behavior when starting Live Agent or Virtual Agent chat sessions.
Context variables are provided via thecontextDataparameter in theNowChatServiceAPI, allowing enhanced interaction by supplying relevant information at chat initiation.
Show full answer Show less  
Additionally, an optional `NowChatConfiguration` parameter can be used to modify chat UI and functional behavior, such as customizing prompts and disabling specific chat features.

## Key Features

* **Passing Context Variables:** Use `startChat(contextData:completion:)` to send a dictionary of context variables that personalize or influence the chat session.
* **Custom Close Prompt:** Configure a confirmation prompt when users attempt to close the chat window, including customizable header, message, accept button, and decline button text.
* **Feature Control:** Disable specific chat features programmatically using the `disabledFeatures` list based on the `NowChatConfiguration.Feature` enum.
* **Conversation Options:** Apply conversation behaviors such as forcing new conversations or ending conversations on exit via the `conversationOptions` list.
* **UI Customization:** Customize UI elements like the close button (text or image) and attachment upload button visibility using the `uiConfiguration` parameter.

## Implementation Guidance

* Initialize the chat service asynchronously with `NowChat.makeChatService(instanceUrl:delegate:completion:)`.
* Create chat UI using `makeChatUI(theme:chatConfiguration:)`, supplying configured `NowChatConfiguration` to control appearance and features.
* Start the chat session with `startChat`, optionally passing context data to tailor the chat experience.
* Handle errors at each step by logging or resetting the chat state as needed.

## Benefits for ServiceNow Customers

* **Enhanced Personalization:** Passing context variables enables dynamically tailored chat interactions based on customer data or session context.
* **Improved User Experience:** Customizable UI elements and prompts create a more intuitive and controlled chat environment.
* **Flexible Chat Behavior:** Ability to enable or disable features and set conversation options provides adaptability to diverse business needs.
* **Simplified Integration:** Clear API methods and configuration classes allow straightforward embedding of chat functionalities into customer applications.  
You can pass chat context variables when starting a chat session by passing the contextData parameter.
You can pass this value using the [NowChatService - startChat(contextData: \[String: Any\]? = nil, _ completion: @escaping (Result\<Void, NowChatServiceError\>) -\> Void)](https://www.servicenow.com/docs/kcV3eVFB9sXog6a46HmHhw#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=brazil&pubname=brazil-conversational-interfaces&ft:locale=en-US).

In addition, you can also pass an optional NowChatConfiguration parameter when starting a chat session to modify some of the behavior of NowChat.  
* closePrompt: Prompt text that appears before exiting a chat window. You define this prompt text through the following parameters:
  * header: Nullable string value that appears on the prompt's header.
  * message: String value that appears on the prompt's main body.
  * acceptButtonTitle: String value that appears on the primary button of the prompt. This button closes the chat window.
  * declineButtonTitle: String value that appears on the secondary button of the prompt. This button dismisses the prompt without closing the chat window.
  {#mobsdk-ios-pass-con-variables__ul_gm3_wyb_fdc}
* disabledFeatures: List of NowChat features to disable. Refer to the NowChatConfiguration.Feature enum class for the list of features that you can disable.
* conversationOptions: List of conversation options to apply to NowChat. Refer to the NowChatConvestation.ConversationOption enum class for the list of options that you can apply.
* uiConfiguration: UIConfiguration value used to configure UI components in NowChat.
{#mobsdk-ios-pass-con-variables__ul_v4q_lyj_kbc}  
The following code example shows how to call this method:

    // Close Button Type
    func closeButtonText(title: String?) -> NowChatConfiguration.CloseButtonType? {
      guard let title else { return nil }
      return .text(title)
    }  
      
    func closeButtonImage(name imageName: String?) -> NowChatConfiguration.CloseButtonType? {
      guard let imageName,
      let buttonImage = UIImage(named: imageName) else { return nil }
      return .image(buttonImage)	
    }


    // Create the chat UI
    func makeChatScreen() -> UIViewController? {
      guard let chatService = chatService else { return nil }
      let closePrompt = NowChatConfiguration.ClosePrompt(header: "Close Window",
        message: "Are you sure you want to close the chat window?",
        acceptButtonTitle: "Yes",
        declineButtonTitle: "No")     

      let attachmentUploadButton = NowChatConfiguration.AttachmentUploadButton(isVisible: false) // default isVisible = true         

      let chatConfiguration = NowChatConfiguration(closePrompt: closePrompt,
        disabledFeatures: [.startNewConversation],
        conversationOptions: [.forceNewConversation, .endConversationOnExit],
        uiConfiguration: NowChatConfiguration.UIConfiguration(closeButton: closeButtonImage(name: "arrow.left"), attachmentUploadButton: attachmentUploadButton))

      let result = chatService.makeChatUI(theme: CarrascoChatTheme(chatColors: ChatColors()), chatConfiguration: chatConfiguration)
             
      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()
      }
    }


