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

# Functions

> A Browserbase Function runs your TypeScript code next to a Browserbase browser and exposes it through an API.

A Browserbase Function is TypeScript code that runs next to a Browserbase browser. You write the browser logic, publish it, and invoke it through an API.

Functions run your code next to the browser and deploy instantly, with under 5ms latency to the browser. Browserbase creates a browser session for each invocation, gives your code the session's connection URL, and closes the session when your code finishes. Your Function returns a JSON value that you retrieve through the Functions API.

<Warning>
  Functions are currently only available in the **us-west-2** region.
</Warning>

## How a Function works

You define a Function with `defineFn`, then publish its entrypoint:

1. The publish command builds your code and creates a Function version.
2. An invoke request starts an asynchronous invocation.
3. Browserbase creates a [headless browser](/platform/browser/getting-started/what-is-headless-browser).
4. Your handler connects to that browser through `context.session.connectUrl`.
5. Browserbase stores the handler's result, logs, and session ID.

Your code doesn't send every browser action back to a separate service.

| Concept    | What it means                                           |
| ---------- | ------------------------------------------------------- |
| Function   | A named handler and its browser session defaults.       |
| Build      | One publish operation for an entrypoint.                |
| Version    | The Function definition produced by a successful build. |
| Invocation | One asynchronous run of a Function version.             |
| Session    | The browser that Browserbase creates for an invocation. |

Invocations and builds are asynchronous. Subscribe to [Function webhook events](/platform/webhooks/overview) instead of polling: Browserbase POSTs `functions.invocations.*` and `functions.builds.*` events to your endpoint when status changes. See the [webhook example](/platform/webhooks/example) for a complete receiver.

## Why use a Function?

A browser script that runs on your machine already contains the hard part: the logic that navigates pages and completes work. Production adds a process to run the script, an API to trigger it, and browser lifecycle management for every run.

Functions provide that execution layer. Publish your existing Stagehand, Playwright, or Puppeteer code, then invoke it from a webhook, backend, queue worker, or scheduled job. Browserbase manages the browser session and records it for debugging.

Use a Function when:

* You need deterministic browser logic that you own.
* You want to invoke that logic through an HTTP API.
* Your code needs low-latency access to a Browserbase browser.
* You want Browserbase to manage browser creation and cleanup for each run.

| Product                                                                      | Who owns the browser loop? | Use it when                                                                             |
| ---------------------------------------------------------------------------- | -------------------------- | --------------------------------------------------------------------------------------- |
| Functions                                                                    | You                        | You want to deploy custom browser code and invoke it through an API.                    |
| [Agents](/platform/agents/overview)                                          | Browserbase                | You want to send a natural language task without writing or deploying the browser loop. |
| [Browser sessions](/platform/browser/getting-started/create-browser-session) | You                        | You want your own process to connect to and control a browser directly.                 |

## What runs inside a Function?

Functions support TypeScript code that uses the browser library you already know:

* [Stagehand](/welcome/quickstarts/stagehand) adds natural language actions and structured extraction to browser code.
* [Playwright](/welcome/quickstarts/playwright) gives you deterministic browser control.
* [Puppeteer](/welcome/quickstarts/puppeteer) connects through the same browser session URL.

The handler receives the browser connection details and any parameters from the invoke request:

```typescript Node.js theme={null}
import { defineFn } from "@browserbasehq/sdk-functions";
import { chromium } from "playwright-core";

defineFn("page-title", async (context, params) => {
  const browser = await chromium.connectOverCDP(
    context.session.connectUrl,
  );
  const page = browser.contexts()[0]!.pages()[0]!;

  await page.goto(params.url);

  return { title: await page.title() };
});
```

You can run the Function with the local development server before you publish it. Production invocations use the same handler, but run asynchronously on Browserbase.

<CardGroup cols={2}>
  <Card title="Functions quickstart" icon="rocket" href="/platform/functions/quickstart">
    Build, test, publish, and invoke your first Function.
  </Card>

  <Card title="Write a Function" icon="code" href="/platform/functions/write">
    Define parameters, browser settings, and multiple Functions.
  </Card>

  <Card title="Invoke a Function" icon="terminal" href="/platform/functions/invoke">
    Pass parameters, override session settings, and get results.
  </Card>

  <Card title="Deploy Functions" icon="cloud-arrow-up" href="/platform/functions/deploy">
    Publish from the CLI or the Playground.
  </Card>

  <Card title="Functions limits" icon="gauge" href="/platform/functions/limits">
    Check bundle, timeout, storage, and region constraints.
  </Card>

  <Card title="Functions API reference" icon="book" href="/reference/api/invoke-a-function">
    Look up the invoke request, response, and session override schema.
  </Card>

  <Card title="Function webhooks" icon="webhook" href="/platform/webhooks/overview">
    Get invocation and build events delivered to your endpoint.
  </Card>

  <Card title="Webhook example" icon="bolt" href="/platform/webhooks/example">
    Register an endpoint and handle Function invocation events.
  </Card>
</CardGroup>
