> ## Documentation Index
> Fetch the complete documentation index at: https://docs.base44.com/llms.txt
> Use this file to discover all available pages before exploring further.

# functions

***

## Overview

Functions module for invoking custom backend functions.

This module allows you to invoke the custom backend functions defined in the app.

### Authentication Modes

This module is available to use with a client in all authentication modes:

* **Anonymous or User authentication** (`base44.functions`): Functions are invoked with the current user's permissions. Anonymous users invoke functions without authentication, while authenticated users invoke functions with their authentication context.
* **Service role authentication** (`base44.asServiceRole.functions`): Functions are invoked with elevated admin-level permissions. The function code receives a request with admin authentication context.

### Generated Types

If you're working in a TypeScript project, you can generate types from your backend functions to get autocomplete on function names when calling `invoke()`. See the [Dynamic Types](/developers/references/sdk/getting-started/dynamic-types) guide to get started.

## Methods

### invoke()

> **invoke**(`functionName`, `data?`): `Promise`\<`any`>

Invokes a custom backend function by name.

Sends a POST request to a custom backend function deployed to the app.
The function receives the provided data as named parameters and returns
the result. If any parameter is a `File` object, the request will automatically be
sent as `multipart/form-data`. Otherwise, it will be sent as JSON.

For streaming responses, non-POST methods, or raw response access, use [`fetch()`](#fetch) instead.

#### Parameters

<Accordion title="Properties">
  <ParamField body="functionName" type="string" required>
    The name of the function to invoke.
  </ParamField>

  <ParamField body="data" type="Record<string, any>">
    An object containing named parameters for the function.
  </ParamField>
</Accordion>

#### Returns

`Promise<any>`

Promise resolving to the function's response. The `data` property contains the data returned by the function, if there is any.

#### Examples

<CodeGroup>
  ```typescript Basic function call theme={null}
  const result = await base44.functions.invoke('calculateTotal', {
    items: ['item1', 'item2'],
  });
  console.log(result.data.total);
  ```

  ```typescript Function with file upload in React theme={null}
  const handleFileUpload = async (event: React.ChangeEvent<HTMLInputElement>) => {
    const file = event.target.files?.[0];
    if (file) {
      const processedImage = await base44.functions.invoke('processImage', {
        image: file,
        filter: 'grayscale',
        quality: 80
      });
    }
  };
  ```
</CodeGroup>

***

### fetch()

> **fetch**(`path`, `init?`): `Promise`\<`Response`>

Performs a direct HTTP request to a backend function path and returns the native `Response`.

Use `fetch()` when you need low-level control that [`invoke()`](#invoke) doesn't provide, such as:

* Streaming responses, like SSE, chunked text, or NDJSON
* Custom HTTP methods, like PUT, PATCH, or DELETE
* Raw response access, including status codes, headers, and binary bodies

#### Parameters

<Accordion title="Properties">
  <ParamField body="path" type="string" required>
    Function path. Leading slash is optional, so `/chat` and `chat` are equivalent. For example, `'/streaming_demo'` or `'reports/export'`.
  </ParamField>

  <ParamField body="init" type="RequestInit">
    Optional [`RequestInit`](https://developer.mozilla.org/en-US/docs/Web/API/RequestInit) options such as `method`, `headers`, `body`, and `signal`. Auth headers are added automatically.
  </ParamField>
</Accordion>

#### Returns

`Promise<Response>`

Promise resolving to a native [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response).

#### Examples

<CodeGroup>
  ```typescript Stream an SSE response theme={null}
  const response = await base44.functions.fetch('/chat', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ prompt: 'Hello!' }),
  });

  const reader = response.body!.getReader();
  const decoder = new TextDecoder();

  while (true) {
    const { done, value } = await reader.read();
    if (done) break;
    console.log(decoder.decode(value, { stream: true }));
  }
  ```

  ```typescript PUT request theme={null}
  const response = await base44.functions.fetch('/users/profile', {
    method: 'PUT',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ name: 'Jane', role: 'admin' }),
  });

  if (!response.ok) {
    throw new Error(`Request failed: ${response.status}`);
  }

  const updated = await response.json();
  ```

  ```typescript Download a binary file theme={null}
  const response = await base44.functions.fetch('/export/report');
  const blob = await response.blob();

  const url = URL.createObjectURL(blob);
  const a = document.createElement('a');
  a.href = url;
  a.download = 'report.pdf';
  a.click();
  ```
</CodeGroup>

## Type Definitions

### FunctionName

***

> **FunctionName** = keyof `FunctionNameRegistry` *extends* `never` ? `string` : keyof `FunctionNameRegistry`

Union of all function names from the [`FunctionNameRegistry`](#functionnameregistry). Defaults to `string` when no types have been generated.

#### Example

<CodeGroup>
  ```typescript Using generated function name types theme={null}
  // With generated types, you get autocomplete on function names
  await base44.functions.invoke('calculateTotal', { items: ['item1', 'item2'] });
  ```
</CodeGroup>

### FunctionNameRegistry

***

Registry of function names. The [`types generate`](/developers/references/cli/commands/types-generate) command fills this registry, then [`FunctionName`](#functionname) resolves to a union of the keys.
