> ## Documentation Index
> Fetch the complete documentation index at: https://docs.styne.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Network and egress

> Choose what a sandbox may reach on the internet, and change the rules while it runs.

[//]: # "smoke: not executed, the examples need a live API key and create sandboxes"

By default a sandbox can reach the whole internet. Set the rules on create with `allow_internet_access` and `network`, and replace
them later on a running sandbox. The rules are enforced outside the sandbox, so nothing inside it can change them.

## Rules on create

| Field | What it does |
| - | - |
| `allow_internet_access` (or `allowInternetAccess`) | Default `true`. `false` closes the internet; only what `network.allowOut` lists stays reachable |
| `network.allowOut` (or `allow_out`) | Destinations the sandbox may reach: IPv4 or IPv6 addresses, CIDR blocks (`203.0.113.0/24`) and host names (`pypi.org`) |
| `network.denyOut` (or `deny_out`) | Destinations the sandbox may not reach, in the same forms |
| `network.allowPublicTraffic` | Opens every port of the sandbox to anyone with its URL; see [Ports](https://docs.styne.ai/ports.md) |
| `network.maskRequestHost` | The `Host` header a request gets inside the sandbox when it arrives through a preview URL. `${PORT}` becomes the port: `localhost:${PORT}` lets a dev server that checks its host name answer |
| `network.dnsServers` | IP addresses of the resolvers the sandbox uses. Default `1.1.1.1` and `8.8.8.8`. An empty list or an entry that is not an IP address answers `400` |

A bare address means that one address. A host name needs at least two labels (`api.example.com`). Any other field, or a
misspelled network field, answers `400` that names it, so a rule is never dropped in silence.

## How the rules combine

* **The more specific rule wins**, and an allow wins a tie. `denyOut: ["10.1.0.0/16"]` with `allowOut: ["10.0.0.0/8"]` keeps
  `10.1.0.0/16` closed and the rest of `10.0.0.0/8` open. The same entry in both lists is allowed.
* **Allowlist.** `denyOut: ["0.0.0.0/0"]` with `allowOut` host names reaches those hosts and nothing else over IPv4, and the
  sandbox can still resolve names. IPv6 stays open when only `0.0.0.0/0` is denied; add `::/0` to close it too.
  `allow_internet_access: false` with `allowOut` gives the same allowlist and closes IPv6 as well.
* **With internet on** (the default), the sandbox reaches everything that `denyOut` does not close.
* **With internet off**, the sandbox reaches only what `allowOut` lists.
* **Host names** stand for the addresses they resolve to. The node resolves each name through its own resolver and renews the
  addresses when the answer's time to live runs out, so a name that moves to a new address follows. Another name that shares one of
  those addresses is reachable too. With internet off, the sandbox reaches a resolver only when `allowOut` lists a host name,
  so a sandbox with no names has no DNS.
* Mail ports (25, 465, 587), common mining ports, the node's own management addresses and the addresses of other sandboxes are
  closed for every sandbox, whatever the rules say.

## Create a sandbox with rules

```bash theme={"theme":"css-variables"}
curl -sS -X POST https://api.styne.ai/sandboxes -H "Authorization: Bearer $STYNE_API_KEY" -H "content-type: application/json" \
  -d '{"templateID": "base", "allow_internet_access": false,
       "network": {"allowOut": ["pypi.org", "files.pythonhosted.org"], "denyOut": ["10.0.0.0/8"]}}'
```

With the E2B SDK ([switch from E2B](https://docs.styne.ai/e2b-drop-in.md)) the same rules are arguments of `create`:

```python theme={"theme":"css-variables"}
from e2b import Sandbox

sbx = Sandbox.create(
    template="base",
    allow_internet_access=False,
    network={"allow_out": ["pypi.org", "files.pythonhosted.org"]},
)
print(sbx.commands.run("curl -sS -o /dev/null -w '%{http_code}' https://pypi.org").stdout)
```

## Change the rules of a running sandbox

```bash theme={"theme":"css-variables"}
curl -sS -X PUT https://api.styne.ai/sandboxes/$SANDBOX_ID/network -H "Authorization: Bearer $STYNE_API_KEY" -H "content-type: application/json" \
  -d '{"allowInternetAccess": false, "allowOut": ["api.github.com"], "denyOut": []}'
```

The answer is `204`. The call replaces `allowOut`, `denyOut` and `allowInternetAccess` together: a field you leave out goes back to
its default (nothing listed, internet on). The lists may sit at the top of the body or inside a `network` object as on create; when
both are given they must be the same list. Other fields, such as `dnsServers`, answer `400`. The new rules apply to the running
sandbox at once. A sandbox that is paused deeply must be resumed first (see [pause tiers](https://docs.styne.ai/pause-tiers.md)).

## Related

* [Ports and preview URLs](https://docs.styne.ai/ports.md) for `allowPublicTraffic`.
* [AI credentials proxy](https://docs.styne.ai/ai-credentials.md) for model keys that never enter the sandbox.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.