Integrate a chat into your product

Show your agent's chat inside a screen in your product, such as an order page.

Before you start

You need a published agent with its id in AGENT_ID, and access to your site's HTML.

To use the platform, see Web chat and Web page.

  1. Create the web chat

    Set the name, agent and your site's domain. The web chat is created published and needs no additional authorization. (Create a web chat)

    import { Platic } from "platic-sdk";
    
    const platic = new Platic({ apiKey: process.env.PLATIC_API_KEY! });
    const agentId = process.env.AGENT_ID!;
    
    const webchat = await platic.webchats.create({
      name: "Acme",
      agentId,
      allowedDomains: ["acme.com"],
    });
    console.log(webchat.embed.inline);

    The response has the web chat's id: keep it in WEBCHAT_ID. When you create it, the name is also the one the visitor sees in the chat; later you change it in config.content.assistantName.

  2. Mount the chat inside your screen

    Use embed.inline to show the open chat inside a container. The step 1 response includes that code and embed.publicKey, the public key you can put in your HTML. (Get a web chat)

    const { embed } = await platic.webchats.get(webchat.id);
    console.log(embed.inline);

    Place the container in the screen where you need the chat. Use your embed.publicKey in data-webchat and the container's selector in data-target.

    <section class="order-page">
      <h1>Order A-1024</h1>
      <p>Ask our assistant about your order.</p>
      <div id="order-chat"></div>
    </section>
    
    <script
      src="https://chat.platica.mx/widget.js"
      data-webchat="wc_pub_7Hq2LmN4pR8sT1vX"
      data-target="#order-chat"
      defer>
    </script>

    The chat loads inside #order-chat when this screen opens. Set its height with config.inline.height and config.inline.minHeight.

    The container defines where the chat appears. To send identity and context from your backend with your own interface, use the Chat API.

    To show the chat button, paste embed.script before </body>:

    <script
      src="https://chat.platica.mx/widget.js"
      data-webchat="wc_pub_7Hq2LmN4pR8sT1vX"
      defer>
    </script>

    The button opens the chat when the visitor clicks it.

    Open the order screen and send a message from the embedded chat.

    To test from your computer, add the local domain to allowedDomains, such as localhost:5173. To mount it when a React or Svelte component opens and remove it when you leave, see Embed it in your site.

  3. Change how it looks and publish

    Change the main color and the greeting. Configuration changes are saved without publishing: your site stays the same until you publish. (Update a web chat, Publish a web chat)

    await platic.webchats.update(webchat.id, {
      config: {
        appearance: { primary: "#E4572E" },
        content: { welcomeTitle: "Hi! Looking for something in particular?" },
      },
    });
    
    await platic.webchats.publish(webchat.id);

    You only send what changes: the rest of the configuration stays the same. Before publishing, hasUnpublishedChanges is true; after, false. The code you pasted doesn't change. What you can change is in the configuration.

Check it worked

Use Get the web chat to check that hasUnpublishedChanges is false. Open your product screen and check that the chat appears inside the container with the published color and greeting. Send a message and find the conversation in Platica's inbox, where your team can read or take it over.

The agent, the name and the domains change with the same PATCH and apply right away, without publishing. To turn the chat off, disconnect the channel; to turn it on, publish it again. See Web chat.

Next steps