Quickstart: create an agent and talk to it
Create an agent and send it two messages in the same conversation. The example prints the replies and deletes the agent when finished.
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.
Create the agent and send two messages
- Creates an agent called "Quickstart" with instructions about Acme's plans.
- Sends a message and prints the reply. Each turn runs inside the request.
- Sends a second message with the
conversationIdof the first, so the agent remembers the chat. - Deletes the agent in a
finallyblock, even when a step fails.
The code
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: "Quickstart",
instructions: "You answer questions about Acme's plans: Basic ($10 a month, up to 5 people), Team ($50, up to 30) and Enterprise (priced on request). Be brief.",
});
console.log(`created agent ${agent.id}`);
try {
// A new chat: the agent answers within the request.
const turn = await platic.chat.send({ agentId: agent.id, message: "Which plan fits a team of 25?" });
for (const reply of turn.messages) console.log(`agent: ${textOf(reply.content)}`);
// The same chat: send its conversationId with the next message.
const next = await platic.chat.send({ conversationId: turn.conversationId, message: "And how much is it?" });
for (const reply of next.messages) console.log(`agent: ${textOf(reply.content)}`);
} finally {
await platic.agents.delete(agent.id);
console.log(`deleted agent ${agent.id}`);
}
// A reply's text; replies that are not text (an image, a file) carry the platform's object.
function textOf(content: unknown): string {
return typeof content === "string" ? content : JSON.stringify(content);
} Run it
Save the code in quickstart.ts and run it with tsx.
Set your API key and run the script:
export PLATIC_API_KEY=pl_key_...
npx tsx quickstart.ts What to expect
created agent ag_…
agent: The Team plan fits 25 people. …
agent: The Team plan is $50 a month.
deleted agent ag_… Reply wording may vary. If the script stops after the first line, check the error: AuthenticationError indicates an incorrect key. The cleanup block deletes the agent even if a step fails.
API reference used
Next steps
All SDK examples: the next one is Chat with a contact.