# Run your first Browser Definition

Browser gives your server a real web browser. Your code can open a web page, read it after its scripts run, or take a screenshot. The browser runs on Cloudflare, not on your server.

In this tutorial, you write a browser task that gets the HTML of `https://example.com`. Then you call it from an API route.

> [!NOTE] Before you start
> Use the app from [Installation](https://vitehub.dev/docs/getting-started/installation). You also need a Cloudflare account with Browser Run turned on. The browser runs on Cloudflare even when you test on your computer, so each run can cost money. Run every command from the app root. To use a Chromium browser on your own machine, see [Hosts](https://vitehub.dev/docs/browser/hosts).

::tutorial-step{title="Install and configure"}
## Install and configure

Add the Browser package and Wrangler, the Cloudflare development tool, to your app:

```bash pm [commands/install]
pnpm add @vite-hub/browser
pnpm add -D wrangler
```

Then, in the ViteHub options of your app, set `preset: 'cloudflare'` and turn on Browser. Keep the other options of your app.

### Nuxt

```ts [nuxt.config.ts]
export default defineNuxtConfig({
  modules: ['vite-hub/nuxt'],
  vitehub: {
    preset: 'cloudflare',
    browser: { remote: true },
  },
})
```

### Nitro

```ts [vite.config.ts]
import { nitro } from 'nitro/vite'
import { defineConfig } from 'vite'
import { vitehub } from 'vite-hub'

export default defineConfig({
  plugins: [vitehub({
    preset: 'cloudflare',
    browser: { remote: true },
  }), nitro()],
})
```

### Vite

```ts [vite.config.ts]
import { defineConfig } from 'vite'
import { vitehub } from 'vite-hub'

export default defineConfig({
  plugins: [vitehub({
    preset: 'cloudflare',
    browser: { remote: true },
  })],
})
```

- `preset: 'cloudflare'` builds your app for Cloudflare. Browser works only with this preset.
- `remote: true` lets your app call the browser on Cloudflare while you test on your computer.

::

::tutorial-step{title="Define a browser operation"}
## Define a browser operation

Create `server/browsers/page-html.ts`. The file name is the name of the task: `page-html`. ViteHub finds browser tasks in `server/browsers/` and in files that end in `.browser.ts`.

```ts [server/browsers/page-html.ts]
import { defineBrowser } from 'vite-hub/browser'

export default defineBrowser(async (
  input: { url: string },
  { browser },
) => {
  return await browser.content(input.url)
})
```

- `input` is what the caller sends. Here, it is a URL.
- `browser.content(input.url)` opens the page and returns its HTML.

::

::tutorial-step{title="Run it by name"}
## Run it by name

Create `server/api/page-html.post.ts`. This route sends the request body to the `page-html` task.

```ts [server/api/page-html.post.ts]
import { createError, defineEventHandler, readBody } from 'h3'
import { runBrowser } from 'vite-hub/browser'

export default defineEventHandler(async (event) => {
  const input = await readBody<{ url: string }>(event)
  if (!input?.url) throw createError({ statusCode: 400, message: 'Send a url.' })
  return await runBrowser('page-html', input)
})
```

- `readBody()` returns `undefined` when the request has no body, so the route checks `input` first.

- `runBrowser('page-html', input)` runs the task by its name. ViteHub knows the input type of each task, so TypeScript checks `input`.
- It returns a normal `Response`. If the task fails, the `Response` has an error status and a JSON body.

This route opens any URL that it gets. In this tutorial, send only the URL below. Before you deploy, add sign-in and a list of allowed URLs.

::

::tutorial-step{title="Verify the response"}
## Verify the response

Sign in to the Cloudflare account that has Browser Run. Then build the app, and start it with Wrangler:

```bash [commands/request]
pnpm wrangler login
pnpm vite build
pnpm wrangler dev --config .output/server/wrangler.json
```

Open a second terminal, and send a URL to the route:

```bash [commands/request-browser]
curl -X POST http://localhost:8787/api/page-html \
  -H 'content-type: application/json' \
  -d '{"url":"https://example.com"}'
```

The response is the HTML of `https://example.com`.

::

## Next steps

::card-group
:::card{title="Server API" icon="i-lucide-code" to="/docs/browser/server-api"}
Keep a page open, take screenshots, and run browser actions.
:::

:::card{title="Configure" icon="i-lucide-sliders-horizontal" to="/docs/browser/configure"}
Change the binding, the session engine, and remote mode.
:::

:::card{title="Limits and errors" icon="i-lucide-triangle-alert" to="/docs/browser/limits-and-errors"}
See the limits of Browser Run and check before production.
:::
::

<!-- vitehub-docs:section-links -->
## In this section

- [Overview](https://vitehub.dev/raw/docs/browser.md)
- [Get started](https://vitehub.dev/raw/docs/browser/get-started.md) (this page)
- [Configure](https://vitehub.dev/raw/docs/browser/configure.md)
- [Server API](https://vitehub.dev/raw/docs/browser/server-api.md)
- [Agent capability](https://vitehub.dev/raw/docs/browser/agent-capability.md)
- [Hosts](https://vitehub.dev/raw/docs/browser/hosts.md)
- [Limits and errors](https://vitehub.dev/raw/docs/browser/limits-and-errors.md)

Next: [Configure](https://vitehub.dev/raw/docs/browser/configure.md)

All pages: [llms.txt](https://vitehub.dev/llms.txt)
