---
docId: doc_cf3b31f9c720414c9218d72db0e571ce
title: "Chat with a contact"
description: "Talk to an agent as one of your contacts, give it instructions for one turn and read the chat back."
sidebarTitle: "Chat with a contact"
---

# Chat with a contact

Send messages on behalf of a contact with `platic.chat.send({ client })`, then read the full chat. The API calls contacts `clients`.

## Before you start

- A Platica API key in `PLATIC_API_KEY`.
- Chat turns use your workspace's credits.
- Node.js 20.3 or newer and `npm install platic-sdk`.

<span id="what-it-does"></span>

## Identify the contact and keep the chat

- Creates an agent that helps customers with their orders.
- Sends a message as a contact with your own id. With `client.id`, Platica uses exactly that contact and matches nothing else.
- Sends a second message with `additionalInstructions`. Those instructions apply only to that turn; they do not change later turns or the agent's configuration.
- Reads the chat with `chat.get` and prints the `user` and `assistant` messages.
- Deletes the contact and the agent at the end.

> **Important:** Without `client.id`, Platica continues as the contact that has the `phoneNumber` or `email` you send. Send those without an id only when your application has verified them.

## The code

```ts
import { Platic } from "platic-sdk";

const apiKey = process.env.PLATIC_API_KEY;
if (!apiKey) {
  console.error("Set PLATIC_API_KEY to a Platica API key (pl_key_…).");
  process.exit(1);
}

const platic = new Platic({ apiKey });

const agent = await platic.agents.create({
  name: "Order status",
  instructions: "You help customers of Acme with their orders. Greet them by their first name. Be brief.",
});
let contactId: string | undefined;

try {
  const client = { id: `example-${Date.now()}`, name: "Ana López", firstname: "Ana" };
  const turn = await platic.chat.send({ agentId: agent.id, client, message: "Hi! Where is my order?" });
  contactId = turn.clientId;
  for (const reply of turn.messages) console.log(`agent: ${textOf(reply.content)}`);

  // Instructions for this turn only: the agent's configuration and later turns do not keep them.
  const next = await platic.chat.send({
    conversationId: turn.conversationId,
    message: "It is order 1001.",
    additionalInstructions: "Order 1001 left the warehouse today and arrives in two days.",
  });
  for (const reply of next.messages) console.log(`agent: ${textOf(reply.content)}`);

  // The whole chat, as Platica stored it. Show only `user` and `assistant` messages to people.
  const chat = await platic.chat.get(turn.id);
  for (const message of chat.messages) {
    if (message.role === "user" || message.role === "assistant") console.log(`${message.role}: ${textOf(message.content)}`);
  }
} finally {
  if (contactId) await platic.clients.delete(contactId);
  await platic.agents.delete(agent.id);
  console.log("removed the example's agent and contact");
}

function textOf(content: unknown): string {
  return typeof content === "string" ? content : JSON.stringify(content);
}
```

## Run it

Save the code in `chat-with-a-contact.ts` and run it with [tsx](https://tsx.is).

Set your API key and run the script:

```bash
export PLATIC_API_KEY=pl_key_...
npx tsx chat-with-a-contact.ts
```

## What to expect

```text
agent: Hi Ana! … I can look into order …
agent: Order 1001 left the warehouse today and arrives in two days.
user: Hi! Where is my order?
assistant: …
user: It is order 1001.
assistant: …
removed the example's agent and contact
```

Check that the second reply uses that turn's instructions. The script prints `user` and `assistant` messages from the stored chat; other roles may be present.

## API reference used

- [Send a chat message](/api-reference/chat/send)
- [Get a chat](/api-reference/chat/get)
- [Delete a contact](/api-reference/contacts/delete)
- [Create an agent](/api-reference/agents/create)

## Next steps

- [All SDK examples](/sdk/examples/quickstart): the next one is [Work a conversation](/sdk/examples/conversations).

- [Errors](/sdk/errors)
