# Bindings & Mounts

Call Databases, Buckets, Queues, Topics, and Email from Workers and Sandboxes over localhost without storing API keys in code.

**Bindings** let code running inside a Worker instance or a Sandbox call your organization's resources over plain HTTP on localhost (`SKOPIK_BINDINGS_URL`, also exposed as `MH_BINDINGS_URL`, at `http://127.0.0.1:8090`).

A lightweight forwarder inside the guest tunnels connections over the sandbox's private vsock to the host bindings gateway. The host identifies the caller strictly by its sandbox socket — **no API keys ever enter the workload**.

## Required Header

Every request to `SKOPIK_BINDINGS_URL` **must** include the header:

```text
mh-bindings: 1
```

Requests without it are rejected with `400 invalid_request` to prevent SSRF attacks against localhost.

## Binding Endpoints

| Kind | Allowed requests under `SKOPIK_BINDINGS_URL` | With `"access": "read"` |
| --- | --- | --- |
| **Database (`sql`)** | `POST /databases/{binding}/execute`, `/batch`, `/query` | `query` only |
| **Database (`documents`)** | `PUT`, `GET`, `DELETE /databases/{binding}/documents/{id}`, `POST .../documents`, `.../documents/bulk`, `.../documents/query` | `GET`, `query` only |
| **Bucket** | `PUT`, `GET`, `HEAD`, `DELETE /buckets/{binding}/objects/{key}`, `GET /buckets/{binding}/objects` | `GET`, `HEAD`, list only |
| **Queue** | `POST /queues/{binding}/messages`, `.../messages/batch` | Not allowed |
| **Topic** | `POST /topics/{binding}/publish/{topic}`, `POST /topics/{binding}/credentials` | Not allowed |
| **Email** | `POST /email/{binding}/send` | Not allowed |

## Example: Calling Bindings in Code

:::tabs
::tab Node.js
```js
const base = process.env.SKOPIK_BINDINGS_URL
const headers = { 'mh-bindings': '1', 'content-type': 'application/json' }

// Query a SQL database
const dbRes = await fetch(`${base}/databases/DB/query`, {
  method: 'POST',
  headers,
  body: JSON.stringify({ sql: 'SELECT * FROM notes LIMIT 10' }),
})

// Send an email from the declared sender
await fetch(`${base}/email/EMAIL/send`, {
  method: 'POST',
  headers,
  body: JSON.stringify({
    to: 'user@example.com',
    subject: 'Welcome',
    text: 'Your workspace is ready.',
  }),
})
```
::tab Python
```python
import os, json, urllib.request

base = os.environ["SKOPIK_BINDINGS_URL"]
req = urllib.request.Request(
    f"{base}/databases/DB/query",
    data=json.dumps({"sql": "SELECT * FROM notes LIMIT 10"}).encode(),
    headers={"mh-bindings": "1", "content-type": "application/json"},
)
with urllib.request.urlopen(req) as resp:
    data = json.loads(resp.read())
```
::tab curl (in Sandbox)
```bash
curl -s -H "mh-bindings: 1" -H "content-type: application/json" \
  -d '{"sql": "SELECT * FROM notes LIMIT 10"}' \
  "$SKOPIK_BINDINGS_URL/databases/DB/query"
```
:::

## Bindings & Mounts in Sandboxes

Pass `bindings` and `mounts` when creating a Sandbox (they are immutable for the Sandbox's lifetime):

```bash
curl -X POST https://api.platform.skopik.com/v1/sandboxes \
  -H "Authorization: Bearer $SKOPIK_API_KEY" \
  -H "content-type: application/json" \
  -d '{
    "vcpus": 1,
    "memory_mib": 512,
    "bindings": { "FILES": { "bucket": "datasets", "access": "read" } },
    "mounts": { "/mnt/datasets": { "binding": "FILES" } }
  }'
```

> [!NOTE]
> Bindings only expose data operations. Management operations (creating/deleting resources, rewinding databases, reading dead letters) always require the control API with an authenticated key or person token.
