UiPath Documentation
functions
latest
false
Functions user guide
  • Overview
    • About Functions
    • Licensing
  • JavaScript functions
    • Getting started
    • Building JavaScript functions
    • HTTP triggers and routing
    • Function context
    • Accessing platform services
    • Testing and debugging
  • Python functions
  • Deploy and run

Building JavaScript functions

Anatomy of a JavaScript function, covering HTTP and job functions, typed contracts, response helpers, error handling, and logging.

Every function is a file directly in the functions/ folder that default-exports a defineFunction(...) object. The function's name is the file name. Files whose names start with an underscore are helpers, not functions. The CLI keeps the functions list in uipath.json in sync with the folder.

import { defineFunction, defineSchema } from "@uipath/coded-functions-js-sdk";

export default defineFunction({
  name: "process-order",
  input: defineSchema<Input>(),
  output: defineSchema<Output>(),
  handler: async (input, ctx) => { /* ... */ },
});
import { defineFunction, defineSchema } from "@uipath/coded-functions-js-sdk";

export default defineFunction({
  name: "process-order",
  input: defineSchema<Input>(),
  output: defineSchema<Output>(),
  handler: async (input, ctx) => { /* ... */ },
});

HTTP and job functions​

The project's targetRuntime in uipath.json decides how every function in it is called.

targetRuntimeFunctionsDeclarationInvoked by
javascript-serverHTTP functionsmethod and path set on every functionA coded app or any HTTP client, through the function's HTTP trigger
javascriptJob functionsmethod and path omittedA Maestro Service Task, a Run Job activity, the Orchestrator API, or a job trigger

uip function new --kind http or --kind job sets the targetRuntime. In a project of HTTP functions, a function without a method and a path fails serve, run and pack. HTTP and job functions are packaged and deployed the same way.

An HTTP function backs a coded app. A job function is a step in a larger automation. See HTTP triggers and routing for the first and Invoking functions for the second.

Typed contracts​

defineSchema<T>() turns a TypeScript interface into the JSON Schema that drives variable binding on every invocation surface. The interface is the single source of truth — it types the handler and declares the contract:

interface CreateOrderInput {
  /** Customer reference. */
  customerId: string;
  /** @default 1 */
  quantity?: number;
}
interface CreateOrderInput {
  /** Customer reference. */
  customerId: string;
  /** @default 1 */
  quantity?: number;
}

Optional properties become optional in the schema, and a JSDoc @default tag carries the default value through. In JavaScript projects, pass a JSON Schema object literal in place of defineSchema<T>().

Write schemas as literals in the function file. A bound or a schema object referenced through a variable can be left out of the function's contract.

Declaring output is optional. When present, the handler's return value is validated against it before it leaves the function.

Returning results​

Return a plain object to send 200 with that object as the body:

handler: async (input) => ({ orderId: input.customerId, processed: true }),
handler: async (input) => ({ orderId: input.customerId, processed: true }),

For anything else, return a response object or use a helper:

import { ok, created, notFound } from "@uipath/coded-functions-js-sdk";

return created({ id: "new-id" });          // 201
return notFound("No such order");          // 404
return { status: 202, body: { queued: true } };
import { ok, created, notFound } from "@uipath/coded-functions-js-sdk";

return created({ id: "new-id" });          // 201
return notFound("No such order");          // 404
return { status: 202, body: { queued: true } };
Warning:

A returned object with a numeric status field is read as a response: status becomes the HTTP status, and only body is sent. Name an output field of your own something else, such as httpStatus.

Errors​

Throw FunctionError to fail with a specific status and message:

import { FunctionError } from "@uipath/coded-functions-js-sdk";

if (!input.customerId) {
  throw new FunctionError("customerId is required", 400);
}
import { FunctionError } from "@uipath/coded-functions-js-sdk";

if (!input.customerId) {
  throw new FunctionError("customerId is required", 400);
}

An uncaught error becomes a 500. When the function runs as a job, a thrown error faults the job and the message reaches the job result.

Logging​

Log with the SDK logger. Its entries reach the job logs in Orchestrator at their level:

import { logger } from "@uipath/coded-functions-js-sdk";

logger.info(`Processed order ${input.orderId}`);
import { logger } from "@uipath/coded-functions-js-sdk";

logger.info(`Processed order ${input.orderId}`);

Never log secret values.

Next steps​

Was this page helpful?

Connect

Need help? Support(opens in new window)

Want to learn? UiPath Academy(opens in new window)

Have questions? UiPath Forum(opens in new window)

Stay updated