> ## Documentation Index
> Fetch the complete documentation index at: https://bunnynet-cb9733c2-add-new-partner-apis.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Standalone scripts

> Standalone scripts take the place of an origin server and handle HTTP requests directly on the bunny.net CDN network.

A standalone script handles the request itself. It serves dynamic content, runs your logic, and shapes the response at the edge, which in many cases means you don't need an origin server at all. Running that close to the user keeps latency down, and a script handles many requests at once.

## Use cases

* Serve REST APIs and microservices straight from the edge
* Generate dynamic UIs and landing pages based on user interactions or geolocation
* Build AI-powered endpoints that process images, run chat completions, or generate embeddings
* Back a static frontend with contact forms, webhooks, and data submissions
* Call external APIs to send emails, transform data, or integrate with third-party services

## The `serve` function

The `serve` function starts an HTTP server that listens for incoming requests and handles them using the provided handler function.

```ts theme={null}
import * as BunnySDK from "@bunny.net/edgescript-sdk";

BunnySDK.net.http.serve(async (request: Request) => {
  return new Response("Hello from the edge!");
});
```

### Function signature

```ts theme={null}
serve(handler: (request: Request) => Response | Promise<Response>)
```

### Handler

The handler function receives a standard [Request](https://developer.mozilla.org/en-US/docs/Web/API/Request) object and must return a [Response](https://developer.mozilla.org/en-US/docs/Web/API/Response) or `Promise<Response>`.

## Before cache execution

<Badge color="orange" icon="sparkles">Preview</Badge>

Standalone scripts act as the origin for their Pull Zone, so by default they
only execute when the cache is missed. If your Pull Zone is configured to
execute scripts before cache, your script runs on **every request**, before
the cache lookup.

<Info>
  You can enable it in your Pull Zone under **General** > **Origin** >
  **Run script before cache**, or directly in the script settings. Learn more
  about [before cache execution](/scripting/before-cache).
</Info>

## Example

This example runs an A/B test based on a custom header:

```ts theme={null}
import * as BunnySDK from "@bunny.net/edgescript-sdk";

BunnySDK.net.http.serve(async (req) => {
  console.log(`[INFO]: ${req.method} - ${req.url}`);

  const origin = "bunny.net";
  const newOrigin = "dash.bunny.net/auth/register";

  // Retrieve scenario header value
  const scenario = req.headers.get("X-AB");

  const url = new URL(req.url);
  url.port = "443";
  url.protocol = "https";

  if (scenario === "new") {
    url.host = newOrigin;
  } else {
    url.host = origin;
  }

  return fetch(url);
});
```

### Local development

You can run standalone scripts locally using Deno:

```bash theme={null}
deno run -A script.ts
```

Test with curl:

```bash theme={null}
# Case A - routes to new origin
curl http://127.0.0.1:8080/ --header 'X-AB: new'

# Case B - routes to default origin
curl http://127.0.0.1:8080/
```
