# Network policy
A sandbox's `network` settings control outbound traffic. Internet access is enabled by default, so commands like `pip install` and `npm install` work without additional configuration.
You can disable internet access or restrict it to specific hosts before running untrusted code.
Inbound traffic is configured separately. See [Preview ports](ports.md).
The examples use `$SANDBOX` for the sandbox ID, as in the [quickstart](quickstart.md).
## The policy
| Field | Default | Meaning |
|---|---|---|
| `internet` | `true` | Enables outbound internet access, including DNS. Set to `false` to block connections except those in `allow`. |
| `allow` | none | Domains, IPv4 addresses, or IPv4 CIDRs that remain accessible when `internet` is `false`. |
| `deny` | none | Domains, IPv4 addresses, or IPv4 CIDRs that are always blocked. `deny` takes precedence over `allow`. |
`allow` only applies when `internet` is `false`.
Outbound traffic on port 25 is always blocked, even when internet access is enabled.
## Set it at create
To disable internet access:
```bash
curl https://sandbox.keln.ai/v1/sandboxes \
-H "Authorization: Bearer $KELN_API_KEY" \
-H "Content-Type: application/json" \
-d '{"network": {"internet": false}}'
```
To allow access only to specific hosts:
```json
{ "network": { "internet": false, "allow": ["pypi.org", "files.pythonhosted.org"] } }
```
To allow internet access except for specific hosts:
```json
{ "network": { "internet": true, "deny": ["example.com", "203.0.113.0/24"] } }
```
## Change it while the sandbox runs
`PATCH /v1/sandboxes/{sandbox_id}` replaces the network policy.
```bash
curl -X PATCH https://sandbox.keln.ai/v1/sandboxes/$SANDBOX \
-H "Authorization: Bearer $KELN_API_KEY" \
-H "Content-Type: application/json" \
-d '{"network": {"internet": false}}'
```
The API returns once the new policy is active for new connections, usually within about a second.
For example, an agent can install dependencies, disable internet access, wait for the response, and then run untrusted code.
Each request that includes `network` replaces the entire policy. Fields you omit revert to their defaults.
For example, sending only `{"network": {"allow": ["pypi.org"]}}` enables internet access because `internet` defaults to `true`. The allow list then has no effect.
Always include `internet` when updating the policy.
The response returns the sandbox with its updated `network` settings. If the sandbox is still starting, Keln waits until it's ready before applying the change.
## Pause and resume
The network policy stays the same when a sandbox is paused or resumed.
Existing connections are closed when the sandbox is [paused](pause-resume.md).
Network policy
A sandbox's network settings control outbound traffic. Internet access is enabled by default, so commands like pip install and npm install work without additional configuration.
You can disable internet access or restrict it to specific hosts before running untrusted code.
Inbound traffic is configured separately. See Preview ports.
The examples use $SANDBOX for the sandbox ID, as in the quickstart.
The policy
Field
Default
Meaning
internet
true
Enables outbound internet access, including DNS. Set to false to block connections except those in allow.
allow
none
Domains, IPv4 addresses, or IPv4 CIDRs that remain accessible when internet is false.
deny
none
Domains, IPv4 addresses, or IPv4 CIDRs that are always blocked. deny takes precedence over allow.
allow only applies when internet is false.
Outbound traffic on port 25 is always blocked, even when internet access is enabled.
The API returns once the new policy is active for new connections, usually within about a second.
For example, an agent can install dependencies, disable internet access, wait for the response, and then run untrusted code.
Each request that includes network replaces the entire policy. Fields you omit revert to their defaults.
For example, sending only {"network": {"allow": ["pypi.org"]}} enables internet access because internet defaults to true. The allow list then has no effect.
Always include internet when updating the policy.
The response returns the sandbox with its updated network settings. If the sandbox is still starting, Keln waits until it's ready before applying the change.
Pause and resume
The network policy stays the same when a sandbox is paused or resumed.
Existing connections are closed when the sandbox is paused.