---
title: "Set up a Portal"
source: "https://browserpod.io/docs/guides/setup-portal"
---

# Set up a Portal

Preview and share a server running in a Pod with a Portal URL

This guide shows how to start a server inside a Pod and get a Portal URL to preview and share it.

> Portals need an API key. Boot the Pod with one before you start.

_This guide assumes you have a [terminal](https://browserpod.io/docs/guides/run-programs-in-a-pod.md)._

## 1. Add a preview to your page

```html
<div id="url">Waiting for Portal...</div>
<iframe id="preview"></iframe>
```

## 2. Register a Portal handler

Register a callback with [`onPortal`](https://browserpod.io/docs/reference/BrowserPod/onPortal.md) to show the Portal URL and load it in the iframe:

```js
const iframe = document.querySelector("#preview");
const urlDiv = document.querySelector("#url");


pod.onPortal(({ url, port }) => {
  urlDiv.innerHTML = `Portal for port ${port}: <a href="${url}">${url}</a>`;
  iframe.src = url;
});
```

## 3. Write the server code

Write a minimal server into the Pod (see [Write files to the Pod](https://browserpod.io/docs/guides/write-files-to-pod.md)):

```js
await pod.createDirectory("/project");
const file = await pod.createFile("/project/server.js", "utf-8");
await file.write(`require("http")
  .createServer((req, res) => res.end("Hello from the Pod"))
  .listen(3000);`);
await file.close();
```

## 4. Start the server

When your server listens on a port, BrowserPod creates a Portal:

```js
pod.run("node", ["server.js"], {
  echo: true,
  terminal,
  cwd: "/project",
});
```

## Notes

- Open the Portal URL to preview the server, or share it with others.
- If multiple servers listen on different ports, you will receive a callback for each Portal.
