Talk to your agent

Add an agent to a screen in your product. Your interface sends the message to your backend, which uses the Chat API to get the reply.

Use conversationId to continue the thread and read its history. To send and receive WhatsApp messages or email from your system, follow Use channels through the API.

Before you start

You need a published agent with its id in AGENT_ID and your Platica API key in the backend. Keep the key on the server.

In the platform: Start a chat.

  1. Send the first message

    Specify the agent, the message and the person sending it. The same call returns the agent's reply. (Send a message to the agent)

    import { Platic } from "platic-sdk";
    
    const platic = new Platic({ apiKey: process.env.PLATIC_API_KEY! });
    
    const turn = await platic.chat.send({
      agentId: process.env.AGENT_ID!,
      message: "Which plan fits a team of 25 people?",
      client: { id: "customer_123", name: "Ana" },
    });
    
    for (const reply of turn.messages) console.log(reply.content);

    The agent's reply is in messages. Save conversationId in CONVERSATION_ID to continue the thread and id in CHAT_ID to read its history.

    Identify the person in client using the id from your system. Keep that id in future calls. If you omit client, the chat belongs to the key's owner.

  2. Follow the conversation

    Include conversationId to continue the thread. If you omit it, another thread is created.

    To retain information about the person across different threads, enable memory and use the same client.id.

    const next = await platic.chat.send({
      conversationId: turn.conversationId,
      message: "We also need WhatsApp.",
    });

    The call waits for the agent to finish, including its tools. If you lose the response, read the conversation before resending the message: repeating it can generate another reply.

  3. Read the history

    It returns the full conversation, with the person's messages and the agent's in order. (Get a chat)

    const chat = await platic.chat.get(turn.id);
    
    for (const message of chat.messages) console.log(message.role, message.content);

    In each message, role says who spoke: user is the person and assistant is the agent.

Check it worked

Read the reply in messages and check that the history keeps the person's and agent's messages in order. Your team can also read or handle the conversation from the Platica inbox.

Next steps