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

# Functions

> Serverless Deno functions: invoke from the app, deploy from the terminal.

Invoke from the app:

```ts theme={"dark"}
const { data, error } = await backend.functions.invoke("my-function", {
  method: "POST",
  body: { orderId },
});
```

## Writing and deploying

Code is a Deno module - standard `fetch`/`Response`, ESM imports, no bundler:

```ts theme={"dark"}
// fn.ts
export default async function (req: Request) {
  const { orderId } = await req.json();
  return new Response(JSON.stringify({ ok: true, orderId }), {
    headers: { "Content-Type": "application/json" },
  });
}
```

```bash theme={"dark"}
npx @resultdev/cli functions deploy my-function --file fn.ts
```

The same command updates an existing function. It runs at
`$NEXT_PUBLIC_BACKEND_URL/functions/my-function`.

## Secrets

Store API keys once; read them inside functions as environment variables:

```bash theme={"dark"}
npx @resultdev/cli secrets set OPENWEATHER_KEY=...
```

```ts theme={"dark"}
// inside the function
const key = Deno.env.get("OPENWEATHER_KEY");
```

## When to use a function

Put logic in a function (not client code) when it involves secrets, spend
(AI calls, email sends), or trust (payments, fulfillment) - anything a
stranger holding your publishable key must not be able to drive.
