# 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.

```typescript

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.

| `targetRuntime` | Functions | Declaration | Invoked by |
|---|---|---|---|
| `javascript-server` | HTTP functions | `method` and `path` set on every function | A coded app or any HTTP client, through the function's HTTP trigger |
| `javascript` | Job functions | `method` and `path` omitted | A 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](javascript-http-triggers.md) for the first and [Invoking functions](invoking-functions.md) 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:

```typescript
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:

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

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

```typescript

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:

```typescript

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:

```typescript

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

Never log secret values.

## Next steps

- [HTTP triggers and routing](javascript-http-triggers.md)
- [Function context](javascript-function-context.md) — identity, platform coordinates, and request data.
- [`defineFunction` reference](https://uipath.github.io/uipath-typescript/js-functions/api/define-function/) — every field and its defaults.
