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

# Factory functions

> Global factory functions for loading Scramjet APIs

Scramjet provides factory functions that load the appropriate APIs for different contexts. These functions are available globally after loading the Scramjet bundle.

## \$scramjetLoadController()

Factory function that creates the `ScramjetController` class for the window context.

```typescript theme={null}
function $scramjetLoadController(): { ScramjetController: typeof ScramjetController }
```

### Returns

An object containing the `ScramjetController` class.

### Example

```typescript theme={null}
const { ScramjetController } = $scramjetLoadController();

const scramjet = new ScramjetController({
  prefix: "/scramjet/"
});

await scramjet.init();

const frame = scramjet.createFrame();
document.body.appendChild(frame.frame);
frame.go("https://example.com");
```

## \$scramjetLoadClient()

Factory function that creates the `ScramjetClient` class for controlling sandboxing.

```typescript theme={null}
function $scramjetLoadClient(): { ScramjetClient: typeof ScramjetClient }
```

### Returns

An object containing the `ScramjetClient` class.

### Example

```typescript theme={null}
const { ScramjetClient } = $scramjetLoadClient();

const scramjetClient = new ScramjetClient(window);
```

<Note>
  Most users won't need to interact with `ScramjetClient` directly. It's automatically created when using `ScramjetController` and `ScramjetFrame`.
</Note>

## \$scramjetLoadWorker()

Factory function that creates the `ScramjetServiceWorker` class for the service worker context.

```typescript theme={null}
function $scramjetLoadWorker(): { ScramjetServiceWorker: typeof ScramjetServiceWorker }
```

### Returns

An object containing the `ScramjetServiceWorker` class.

### Example - Plain service worker

```typescript theme={null}
// In your service worker
const { ScramjetServiceWorker } = $scramjetLoadWorker();

const scramjet = new ScramjetServiceWorker();

self.addEventListener("fetch", async (ev) => {
  await scramjet.loadConfig();

  if (scramjet.route(ev)) {
    ev.respondWith(scramjet.fetch(ev));
  }
});
```

### Example - Workbox routing

```typescript theme={null}
// In your service worker (ensure you are using a bundler for Workbox)
// This is more useful for a webOS or if you have offline PWA support on your proxy site
import { registerRoute } from 'workbox-routing';

const { ScramjetServiceWorker } = $scramjetLoadWorker();

const scramjet = new ScramjetServiceWorker();

registerRoute(
  ({ request }) => {
    return scramjet.route({ request });
  },
  async ({ event }) => {
    await scramjet.loadConfig();

    return scramjet.fetch(event);
  }
);
```

## \$scramjetVersion

Version information for the current Scramjet build.

```typescript theme={null}
const $scramjetVersion: ScramjetVersionInfo
```

### Type

```typescript theme={null}
interface ScramjetVersionInfo {
  /** The git commit hash that this build was created from */
  build: string;
  /** The semantic version */
  version: string;
}
```

### Example

```typescript theme={null}
console.log(`Scramjet v${$scramjetVersion.version}`);
console.log(`Build: ${$scramjetVersion.build}`);
```
