Custom functions

Write a custom function in TypeScript so your agent can use your business logic and services. For example:

  • Calculate a quote using your prices and rules.
  • Check availability in your systems and combine their responses.
  • Create a request in your system through its API.

You define these actions in the code. Platica reviews and runs it in an isolated environment. The agent calls the function as fn_<name>.

For one HTTP call with JSON, you can configure a custom API without writing code.

Quick example

import { readFile } from "node:fs/promises";

// 1. Create the function as a draft with the code from your repository
const fn = await platic.tools.functions.create({
  name: "tax_id_lookup",
  description: "Looks up a customer's company name by tax ID. Use it when the customer gives their tax ID.",
  source: await readFile("tax_id_lookup.ts", "utf8"),
  allowedDomains: ["api.example.com"],
  secretSlots: [{ slot: "API_KEY", origin: "https://api.example.com" }],
});

// 2. Verify, bind the secret, test, publish and connect to the agent
const check = await platic.tools.functions.verify(fn.id);
if (!check.ok) for (const issue of check.issues) console.log(issue.stage, issue.line, issue.message);
await platic.tools.functions.bindSecret(fn.id, { slot: "API_KEY", secretId });
const test = await platic.tools.functions.test(fn.id, { input: { taxId: "XAXX010101000" } });
if (test.ok) {
  const { toolId } = await platic.tools.functions.publish(fn.id, { notes: "First version" });
  if (toolId) await platic.agents.tools.connect(agentId, toolId);
}

Code format

import { z } from "zod";
import { defineFunction } from "@platica/functions";

export default defineFunction({
  input: z.object({
    taxId: z.string().describe("Customer tax ID, 12 or 13 characters")
  }),
  async run(input, { http, secrets, log }) {
    const res = await http.fetch("https://api.example.com/v1/tax-id", {
      method: "POST",
      headers: { "Content-Type": "application/json", Authorization: `Bearer ${secrets.API_KEY}` },
      body: JSON.stringify({ taxId: input.taxId })
    });
    if (res.status !== 200) return { found: false };
    log.info("response", { status: res.status });
    return { found: true, name: res.json().company_name };
  }
});
  • input is a zod schema. Platica turns it into the schema the agent sees, and the descriptions in .describe(...) tell it what to send.
  • run(input, { http, secrets, log }) returns a JSON object. That is what the agent receives.
  • http.fetch(url, { method, headers, body }) is the only way out to the internet. It answers { status, headers, text, json() }.
  • secrets.API_KEY is not the key: it is a placeholder. Platica swaps it for the value only when the call goes to the origin declared for that slot.
  • log.info, log.warn and log.error end up in the executions.

Allowed packages: zod, @platica/functions, date-fns, decimal.js, papaparse, lodash-es and fast-xml-parser. Get the environment gives their versions, the template and the exact types.

Not allowed: fs, net, http, https, child_process, process, require, import(), eval, Function, the global fetch, WebAssembly, globalThis, XMLHttpRequest and WebSocket. Verify rejects code that uses them.

Security

  • Each function has an isolated environment. It cannot access other functions, other workspaces, or Platica's data.
  • Internet access is limited. Only HTTPS calls to domains in allowedDomains are allowed: up to 10 exact names, such as api.example.com. Any other call fails with EGRESS_DENIED.
  • Protected secrets. The code receives a placeholder. Platica sends the real value only to the slot's authorized origin. Other destinations receive the placeholder. Logs and executions exclude the value.
  • It is reviewed before running. Verify compiles the code and rejects packages and APIs that are not allowed before anything is deployed.
  • What is published does not change. Each publication is a fixed version. Editing changes the draft; agents keep using the published version until the next publication, and you can go back to an earlier one.

Secrets

To bind a credential:

  1. Declare each credential in secretSlots with an uppercase name (API_KEY) and the only origin it can be sent to (https://api.example.com). The origin's host has to be in allowedDomains.
  2. Store the value as a workspace secret or reuse one from List secrets.
  3. Bind the secret to the slot. It is authorized only for that origin.

Test and run use the bound secrets, which are resolved on Platica's side. If the code reads a secret that is not bound, the execution answers SECRET_MISSING. Sending secrets in the body of a test or a run answers 400.

States

statusWhat it means
draftDraft. It has never been published and no agent sees it
enabledPublished and on. The tool is in the catalog and connected agents use it
pausedPaused. No agent sees it; the code, the versions and the connections to agents are kept

An enabled function still has a draft: editing does not change what agents use until you publish again.

Limits

WhatLimit
Functions per workspace20
Time per execution1 to 60 s; 20 s by default
Memory256 MB
HTTP calls per execution20, each up to 15 s and 2 MB
Input and output256 KB each. The agent reads up to 16,000 characters of the output
Code100 KB (1 MB bundled)
Allowed domains10 per function
Secret slots10 per function
Calls60 per minute per function; 5,000 per day and 50,000 per month per workspace
Deployments (publish and test)50 per day per workspace

Get usage says how much you have used. When a limit is exceeded, the response is LIMIT_REACHED with details.limit and, on calls, details.resetAt.

Errors

Errors follow the format in Errors: { code, error, details: { code, message, ... } }.

Statusdetails.codeWhen
400INVALID_REQUESTThe body or parameters fail validation. details.issues has the list of problems
401UNAUTHENTICATEDThe credential is missing, or revoked or expired
403FORBIDDENThe person behind the token does not have access to Integrations
403INSUFFICIENT_SCOPEThe OAuth token does not have write permission
404NOT_FOUNDThe function or the version does not exist in this workspace
409NAME_TAKENA function with that name already exists
409NOT_PUBLISHEDThe function does not have a published version yet
409PAUSEDThe function is paused
409LIMIT_REACHEDThe workspace already has 20 functions
422VALIDATION_FAILEDThe code does not pass verification when publishing or testing. details.issues has each problem
429LIMIT_REACHEDA call or deployment limit was exceeded. details.limit says which and details.resetAt when it frees up
501FUNCTION_SECRETS_UNAVAILABLEBinding secrets is not available at the moment
502SERVICE_MISCONFIGURED or CUSTOM_FUNCTIONS_SERVICE_ERRORInternal Platica failure. Try later
503CUSTOM_FUNCTIONS_SERVICE_UNAVAILABLE or PROVIDER_UNAVAILABLEFunctions are not available at this moment

Errors inside an execution

Check ok in the response: a failed execution returns HTTP 200, with ok: false and the reason in error.code.

error.codeWhat happened
FUNCTION_ERRORThe code threw an error. Check logs
TIMEOUTIt took longer than timeoutSeconds and was stopped
EGRESS_DENIEDIt tried to call a domain that is not in allowedDomains
INPUT_INVALIDThe input does not meet the input schema
OUTPUT_TOO_LARGEIt returned more than 256 KB
SECRET_MISSINGIt reads a secret that is not bound. Bind it with Bind a secret

Endpoints

MethodEndpointWhat it does
GET / POST/v1/tools/functionsList functions or create one as draft
GET / PATCH / DELETE/v1/tools/functions/{functionId}Get, edit the draft or delete
POST/v1/tools/functions/{functionId}/verifyReview the code without running it
POST/v1/tools/functions/{functionId}/secretsBind a secret to a slot
POST/v1/tools/functions/{functionId}/testTest the draft with an input
POST/v1/tools/functions/{functionId}/publishPublish a new version and turn it on
POST/v1/tools/functions/{functionId}/rollbackPut an earlier version live
POST/v1/tools/functions/{functionId}/enableTurn on a paused function
POST/v1/tools/functions/{functionId}/pausePause without losing anything
POST/v1/tools/functions/{functionId}/runRun the published version by hand
GET/v1/tools/functions/{functionId}/versionsPublished versions, and one with its code
GET/v1/tools/functions/runsExecutions of all functions or of one
GET/v1/tools/functions/usageThe workspace's usage against its limits
GET/v1/tools/functions/runtimePackages, limits, template and types

Permissions: reading requires read access to Integrations; creating, changing, testing, publishing and running, write access (a workspace API key, or an OAuth token with the integrations permission and platica:write); deleting, delete access. If the credential covers several workspaces, send ?workspace=<id>.

Once published, the function appears in the workspace catalog with kind: "function" and is connected to an agent with Connect a tool. AI tools are in the Custom functions MCP tools and the dashboard guide is in Custom functions.