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.
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 }; }, });inputdefines the data the agent sends. Descriptions explain each field.runcalculates and returns the result.Create the draft with the file:
curl https://api.platic.ai/v1/tools/functions \ -H "Authorization: Bearer $PLATIC_API_KEY" \ -H "Content-Type: application/json" \ -d "$(jq -n --rawfile source quote.ts '{ name: "quote_total", description: "Calculates the product and shipping total in MXN from quantity and unit price.", source: $source }')"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);Create quote_total from quote.ts. It calculates the product and shipping total from quantity and unit price.
Tool:
create_function.Save the function's
idinFUNCTION_ID. You can't change its name later. The function starts indraftand is not yet available to agents. When connected, the agent will see it asfn_quote_total.Test the calculation
Test the draft with two products at 250 MXN each:
curl -X POST https://api.platic.ai/v1/tools/functions/$FUNCTION_ID/test \ -H "Authorization: Bearer $PLATIC_API_KEY" \ -H "Content-Type: application/json" \ -d '{ "input": { "quantity": 2, "unitPrice": 250 } }'const test = await platic.tools.functions.test(fn.id, { input: { quantity: 2, unitPrice: 250 }, }); console.log(test.ok, test.output);Test quote_total with quantity 2 and unit price 250. Show the subtotal, shipping and total.
Tool:
test_function.The test verifies the code, deploys the draft separately and runs it once. It leaves the published version unchanged. Check
ok: trueand thisoutput:{ "currency": "MXN", "subtotal": 500, "shipping": 100, "total": 600 }If you receive
ok: false, checkerrorandlogs, fix the code and retry. A code failure can still return HTTP200. Testing and publishing count toward the workspace's limit of 50 daily deployments.Publish the function
Publish the draft when its result is correct:
curl -X POST https://api.platic.ai/v1/tools/functions/$FUNCTION_ID/publish \ -H "Authorization: Bearer $PLATIC_API_KEY" \ -H "Content-Type: application/json" \ -d '{ "notes": "Quote with shipping" }'const published = await platic.tools.functions.publish(fn.id, { notes: "Quote with shipping", }); console.log(published.toolId);Publish quote_total with the note "Quote with shipping".
Tool:
publish_function.The function becomes
enabledand the response includestoolId. Save it inTOOL_ID. Each publication creates a fixed version; later edits change the draft until you publish it again.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.
curl -X PUT https://api.platic.ai/v1/agents/$AGENT_ID/tools/$TOOL_ID \ -H "Authorization: Bearer $PLATIC_API_KEY"await platic.agents.tools.connect(process.env.AGENT_ID!, published.toolId!);Connect quote_total to the Sales agent.
Tool:
connect_agent_tool.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
- Test the agent with evals.
- Custom functions: parameters, versions, pausing and restoring.
- Complete SDK example: creation, testing, publishing and cleanup.