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

# Script functions

> The script backend: how to write, deploy, and call functions.

Each script can have its own **backend**: JavaScript functions and data collections, hosted by DoglyTdc. You do not need a server.

## How it works

**1. Write the function**

Create `supabase/functions/<nome>.js` with `function handler(request) { ... }`. You can start from a [template](https://docs.doglytdc.dev/en/scripts/functions/templates.md).

**2. Save to the main branch**

A commit to the main branch deploys the backend automatically. You can also redeploy with `POST /scripts/{script_id}/deploy` or `deploy_functions`.

**3. Call the function**

From the script, through the API (`POST /scripts/{script_id}/functions/{name}`), or through AI (`test_function`).

During deployment, DoglyTdc checks each function's syntax. A function with an error is marked as **error** and is not activated (if a working version existed before, it stays active). Functions deleted from the repository are removed.

## The function

```js theme={null}
function handler(request) {
  return doglytdc.respond(200, { ok: true, method: request.method });
}
```

> [!WARNING]
>
> `handler` must be **synchronous**. Do not use `async`/`await`: `doglytdc.*` calls already wait for results themselves. An `async` function fails with "handler não pode ser async" (handler cannot be async).

## The `request` object

| Field | What it is |
| - | - |
| `request.method` | Request HTTP method |
| `request.body` | JSON body sent |
| `request.query` | URL parameters |
| `request.user` | The caller's `{ username }`, or `null` if not logged in |

## The `doglytdc` object

| Item | What it does |
| - | - |
| `doglytdc.respond(status, body)` | Builds the response (status from 200 to 599). If you return another value, it becomes the body with status 200. |
| `doglytdc.user` | The caller (`{ username }`) or `null`. |
| `doglytdc.env` | Script secrets (name → value), read-only. |
| `doglytdc.db.collection(nome)` | Script database. See [Collections](https://docs.doglytdc.dev/en/scripts/functions/collections.md). |
| `doglytdc.fetch(url, init)` | Calls an allowed site. See [fetch and AI](https://docs.doglytdc.dev/en/scripts/functions/fetch-and-ai.md). |
| `doglytdc.ai.chat(messages, options)` | DoglyTdc AI using the owner's credits. See [fetch and AI](https://docs.doglytdc.dev/en/scripts/functions/fetch-and-ai.md). |
| `doglytdc.log(...)` / `console.log(...)` | Writes to the execution log. |

## Limits per execution

| Limit | Value | If exceeded |
| - | - | - |
| Time | 5 seconds | `504` "Tempo esgotado (5 s)." (Timed out after 5 s.) |
| Memory | 32 MB for the program (64 MB ceiling) | `500` "Memória esgotada (64 MB)." (Out of memory, 64 MB.) |
| Response | 256 KB | `500` "Resposta grande demais (máx. 256 KB)." (Response too large, max. 256 KB.) |
| Log lines | 50 (up to 2,000 characters each) | Additional lines are ignored |
| `fetch` | 10 per execution, 4 s each, response up to 1 MB | Call error |
| AI | 3 calls per execution | Call error |
| Functions per script | 30 | Additional functions are ignored during deployment |
| API calls | 120 per minute per person and script | `429 RATE_LIMITED` |

The function runs in an isolated sandbox: it has no access to networks, files, or databases beyond what the `doglytdc` object provides.

## View the result

Each call is logged with its status, duration, and error (without the body). Logs retain the last 7 days. The function list (`GET /scripts/{script_id}/functions`) shows `status`, `error`, `live_sha`, `last_run_at`, and each function's public address.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.