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.
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)
curl https://api.platic.ai/v1/webchats \ -H "Authorization: Bearer $PLATIC_API_KEY" \ -H "Content-Type: application/json" \ -d '{ "name": "Acme", "agentId": "'$AGENT_ID'", "allowedDomains": ["acme.com"] }'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);Create a web chat for acme.com that the Sales agent answers.
Tool:
create_webchatand answers with the code to paste.The response has the web chat's
id: keep it inWEBCHAT_ID. When you create it, the name is also the one the visitor sees in the chat; later you change it inconfig.content.assistantName.Mount the chat inside your screen
Use
embed.inlineto show the open chat inside a container. The step 1 response includes that code andembed.publicKey, the public key you can put in your HTML. (Get a web chat)curl https://api.platic.ai/v1/webchats/$WEBCHAT_ID \ -H "Authorization: Bearer $PLATIC_API_KEY"const { embed } = await platic.webchats.get(webchat.id); console.log(embed.inline);Give me embed.inline for Acme's web chat to show inside an order page.
Tool:
get_webchat.Place the container in the screen where you need the chat. Use your
embed.publicKeyindata-webchatand the container's selector indata-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-chatwhen this screen opens. Set its height withconfig.inline.heightandconfig.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.scriptbefore</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 aslocalhost:5173. To mount it when a React or Svelte component opens and remove it when you leave, see Embed it in your site.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)
curl -X PATCH https://api.platic.ai/v1/webchats/$WEBCHAT_ID \ -H "Authorization: Bearer $PLATIC_API_KEY" \ -H "Content-Type: application/json" \ -d '{ "config": { "appearance": { "primary": "#E4572E" }, "content": { "welcomeTitle": "Hi! Looking for something in particular?" } } }' curl -X POST https://api.platic.ai/v1/webchats/$WEBCHAT_ID/publish \ -H "Authorization: Bearer $PLATIC_API_KEY"await platic.webchats.update(webchat.id, { config: { appearance: { primary: "#E4572E" }, content: { welcomeTitle: "Hi! Looking for something in particular?" }, }, }); await platic.webchats.publish(webchat.id);Make Acme's chat color #E4572E and greet with "Hi! Looking for something in particular?". Then publish it.
Tool:
update_webchatand thenpublish_webchat.You only send what changes: the rest of the configuration stays the same. Before publishing,
hasUnpublishedChangesistrue; 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
- Embed it in your site: inside a container, from your code or in React and Svelte.
- Put it on a channel so it also answers WhatsApp, Messenger or Instagram.
- Conversations: read, search and count what the agent is handling, or pause it to answer yourself.
- Versions and how to go back, in case an agent publication doesn't work.