Skip to main content
Standalone scripts run independently to perform specific tasks without the need for external intervention. They are useful when you need complete control over execution and state. By using standalone functions, you can serve dynamic content, implement custom logic, and manipulate requests and responses directly at the edge. This approach eliminates the need for an origin server in many scenarios, allowing you to build powerful, low-latency applications.

Use cases

  • REST APIs and microservices — Handle client requests and return responses directly from the edge
  • Dynamic UIs and landing pages — Generate and serve content based on user interactions or geolocation
  • AI-powered endpoints — Process images, run chat completions, or generate embeddings
  • Backend functions for static frontends — Power contact forms, webhooks, and data submissions
  • External API calls — Send emails, transform data, or integrate with third-party services

Benefits

By executing directly at the network’s edge, standalone scripts reduce latency significantly, enabling faster data processing and response times. They are inherently capable of handling multiple requests simultaneously without straining resources, facilitating smooth scalability as user demands increase.

The serve function

The serve function starts an HTTP server that listens for incoming requests and handles them using the provided handler function.
import * as BunnySDK from "@bunny.net/edgescript-sdk";

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

Function signature

serve(handler: (request: Request) => Response | Promise<Response>)

Handler

The handler function receives a standard Request object and must return a Response or Promise<Response>.

Example

This example demonstrates A/B testing based on a custom header:
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:
deno run -A script.ts
Test with curl:
# 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/