Tool with a secret

Connect a custom API token through a secret. Bind the tool's authentication field to the secret so its definition and responses don't include the token.

Before you start

  • A Platica API key in PLATIC_API_KEY.
  • For the real test only: an API you own, in PLATIC_EXAMPLE_API_URL, and its token in PLATIC_EXAMPLE_API_TOKEN.
  • Node.js 20.3 or newer and npm install platic-sdk.

Store the token and prepare the call

  • Creates a bearer secret with the token. The API never returns its value.
  • Creates a custom API tool. auth: { type: "bearer" } defines the auth.token field; secrets: { "auth.token": secret.id } binds it to the secret.
  • Tests the tool with dryRun: true: the request is built and nothing outside Platica is called.
  • With the real test enabled: calls your API, enables the tool and connects it to a new agent.
  • Removes everything it created, 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 realTest = process.env.PLATIC_EXAMPLE_REAL_TEST === "1";
const apiUrl = process.env.PLATIC_EXAMPLE_API_URL ?? "https://api.example.com/orders";
const apiToken = process.env.PLATIC_EXAMPLE_API_TOKEN ?? "example-token-not-a-real-credential";
if (realTest && !process.env.PLATIC_EXAMPLE_API_URL) {
  console.error("PLATIC_EXAMPLE_REAL_TEST=1 calls a real API: set PLATIC_EXAMPLE_API_URL (and PLATIC_EXAMPLE_API_TOKEN) to one you own.");
  process.exit(1);
}

const platic = new Platic({ apiKey });
const cleanup: Array<[string, () => Promise<unknown>]> = [];

try {
  // 1. The credential, stored once. Its value is never returned by the API.
  const secret = await platic.secrets.create({ name: `Orders API token ${Date.now()}`, kind: "bearer", value: apiToken });
  cleanup.unshift([`secret ${secret.id}`, () => platic.secrets.delete(secret.id)]);

  // 2. The tool. `auth: { type: "bearer" }` exposes the `auth.token` slot, bound here to the secret by id.
  const api = await platic.tools.apis.create({
    name: `order_status_${Date.now()}`,
    description: "Looks up an order by its number and returns its status and delivery date.",
    request: { method: "GET", url: apiUrl, params: [{ key: "number", value: "{{order}}" }], auth: { type: "bearer" } },
    variables: [{ name: "order", type: "string", description: "The order number the customer gives", required: true, mode: "ai" }],
    secrets: { "auth.token": secret.id },
  });
  cleanup.unshift([`tool ${api.id}`, () => platic.tools.apis.delete(api.id)]);
  console.log(`tool ${api.name}: ${api.nextStep}`);

  // 3. Build the request without sending it.
  const dry = await platic.tools.apis.test(api.id, { dryRun: true, input: { order: "1001" } });
  console.log(`dry run: ${dry.success ? "the request builds" : "the request does not build"}${dry.nextStep ? `; ${dry.nextStep}` : ""}`);

  if (realTest) {
    // 4. A real test that passes marks the definition verified, which enabling needs.
    const test = await platic.tools.apis.test(api.id, { input: { order: "1001" } });
    console.log(`real test: ${test.success ? "passed" : "failed"} (HTTP ${test.httpStatus ?? "none"})`);
    if (!test.success) throw new Error(test.nextStep ?? "the real test did not pass");

    // 5. Publish it to the workspace's catalog and give it to an agent.
    const { toolId } = await platic.tools.apis.enable(api.id);
    const agent = await platic.agents.create({ name: "Orders", instructions: "Look up orders with your tool before answering." });
    cleanup.unshift([`agent ${agent.id}`, () => platic.agents.delete(agent.id)]);
    await platic.agents.tools.connect(agent.id, toolId);
    cleanup.unshift([`tool ${toolId} on agent ${agent.id}`, () => platic.agents.tools.disconnect(agent.id, toolId)]);
    console.log(`agent ${agent.id} can now call ${api.name}`);
  }
} finally {
  await cleanUp(cleanup);
}

// Runs every step, even when one fails, and says what could not be removed.
async function cleanUp(steps: Array<[string, () => Promise<unknown>]>): Promise<void> {
  for (const [label, step] of steps) {
    try {
      await step();
      console.log(`removed ${label}`);
    } catch (error) {
      console.error(`could not remove ${label}: ${error instanceof Error ? error.message : String(error)}`);
      process.exitCode = 1;
    }
  }
}

Run it

Save the code in tool-with-a-secret.ts and run it with tsx.

Set your API key and run the script:

export PLATIC_API_KEY=pl_key_...
npx tsx tool-with-a-secret.ts

To run the real test, enable the tool and connect it to an agent, use:

PLATIC_EXAMPLE_REAL_TEST=1 \
PLATIC_EXAMPLE_API_URL=https://api.your-company.com/orders \
PLATIC_EXAMPLE_API_TOKEN=... \
npx tsx tool-with-a-secret.ts

What to expect

tool order_status_…: <next step from the API>
dry run: the request builds
removed tool …
removed secret …

With the real test enabled you also see the real test result and the new agent. The script removes the tool, the secret and the agent in reverse order. If a removal fails, it says which one and exits with code 1.

API reference used

Next steps