Write a custom function

Give your agent a function with your business rules. This example calculates a quote: products plus shipping, with free shipping from 1,000 MXN.

You can also use functions to check availability across systems or create a request. They are written in TypeScript and return a JSON object.

Before you start

You need your Platica API key in PLATIC_API_KEY. For the SDK, use Node.js 20.3 or newer and platic-sdk; the REST examples use jq to read the file.

To connect it to an agent, keep its id in AGENT_ID (create one). In the platform, use Custom functions.

  1. Create the function

    Save this code as quote.ts. It takes quantity and unit price. Shipping costs 100 MXN and is free when the subtotal reaches 1,000 MXN.

    import { z } from "zod";
    import { defineFunction } from "@platica/functions";
    
    export default defineFunction({
      input: z.object({
        quantity: z.number().int().min(1).describe("Number of products"),
        unitPrice: z.number().nonnegative().describe("Unit price in MXN"),
      }),
      async run(input) {
        const subtotal = input.quantity * input.unitPrice;
        const shipping = subtotal >= 1000 ? 0 : 100;
        return { currency: "MXN", subtotal, shipping, total: subtotal + shipping };
      },
    });

    input defines the data the agent sends. Descriptions explain each field. run calculates and returns the result.

    Create the draft with the file:

    import { readFile } from "node:fs/promises";
    import { Platic } from "platic-sdk";
    
    const platic = new Platic({ apiKey: process.env.PLATIC_API_KEY! });
    
    const fn = await platic.tools.functions.create({
      name: "quote_total",
      description: "Calculates the product and shipping total in MXN from quantity and unit price.",
      source: await readFile("quote.ts", "utf8"),
    });
    console.log(fn.id);

    Save the function's id in FUNCTION_ID. You can't change its name later. The function starts in draft and is not yet available to agents. When connected, the agent will see it as fn_quote_total.

  2. Test the calculation

    Test the draft with two products at 250 MXN each:

    const test = await platic.tools.functions.test(fn.id, {
      input: { quantity: 2, unitPrice: 250 },
    });
    console.log(test.ok, test.output);

    The test verifies the code, deploys the draft separately and runs it once. It leaves the published version unchanged. Check ok: true and this output:

    { "currency": "MXN", "subtotal": 500, "shipping": 100, "total": 600 }

    If you receive ok: false, check error and logs, fix the code and retry. A code failure can still return HTTP 200. Testing and publishing count toward the workspace's limit of 50 daily deployments.

  3. Publish the function

    Publish the draft when its result is correct:

    const published = await platic.tools.functions.publish(fn.id, {
      notes: "Quote with shipping",
    });
    console.log(published.toolId);

    The function becomes enabled and the response includes toolId. Save it in TOOL_ID. Each publication creates a fixed version; later edits change the draft until you publish it again.

  4. Connect it to the agent

    Connect the tool so the agent can use it. The connection applies immediately and doesn't require publishing the agent.

    await platic.agents.tools.connect(process.env.AGENT_ID!, published.toolId!);

    To use the function from your backend, you can also run the published version through the API.

Check it worked

Ask the agent for a quote for two products at 250 MXN each. Check that it uses the function and gives a total of 600 MXN. Read executions to review the input and output.

If you need to call other systems

Declare allowed domains and bind their credentials as secrets. Use http.fetch for HTTP calls. Tests call the real system and may change it if your code does.

The authenticated service example includes verification, domains, secrets, logs and errors. SOAP and XML are possible cases within this kind of integration.

See the environment and packages and limits and usage. For a single HTTP call, you can use a custom API.

Next steps