---
title: "PluginContext"
description: "The invocation shape and the host APIs available to a plugin."
---

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

# PluginContext

A plugin receives a serializable invocation for each hook call. The SDK also
exposes a `PluginContext` to hook handlers; in the sandbox, the same operations
are brokered by the host bridge.

## Invocation shape

```ts
interface HookInvocation {
  v: 1;
  invocationId: string;
  hook: PluginHook;
  storeId: string;
  installId: string;
  pluginId: string;
  pluginVersion: string;
  payload: unknown;        // hook-specific data
  config: PluginSettings;  // merchant-configured settings
  context: {
    requestId: string;
    now: string;
    locale?: string;
    currency?: string;
    actor?: { type: "admin" | "customer" | "system"; id?: string };
  };
  token: string;           // short-lived host bridge token
}
```

The `token` is scoped to one installation and expires. Never log it, store it,
or send it to another service.

## PluginContext

| Member | Capability | Purpose |
|---|---|---|
| `storeId`, `installId`, `pluginId` | — | Identity of the invocation. |
| `config` | `settings` | The installation's settings, with secrets redacted. |
| `storage.get(key)` | `storage` | Read a value from the plugin's declared collection. |
| `storage.put(key, value)` | `storage` | Write a value. |
| `storage.delete(key)` | `storage` | Delete a value. |
| `catalog.getProduct(id)` | `catalog:read` | Read a product view. |
| `cart.get()` | `cart:read` | Read the current cart view. |
| `settings.get()` | `settings` | Read settings, with secret sentinels. |
| `settings.set(settings)` | `settings` | Write settings; secret fields are write-only. |
| `http.fetch(url, init)` | `network:request` | Call an allowed host. |
| `logger` | — | Structured logging to the invocation log. |
| `now()` | — | The host's current time. |

## Host authority

- Plugins never read or write commerce tables directly.
- Cart adjustments are proposals. The host validates targets, currency,
  discountable lines, and amount bounds, then recomputes totals.
- Policy hooks return `{ allow, reason? }`; a `closed` hook can block the
  request.
- The host can reject a malformed response and record an invocation error.

## HTTP calls

`ctx.http.fetch` is limited to `allowedHosts` unless the plugin declares
`network:request:unrestricted`. HTTPS is required, and literal loopback,
private, link-local, metadata, and unique-local addresses are rejected.

For secret settings, declare `egressHosts` on the secret field and map it with
`secret_headers` in the fetch call. The host injects the decrypted value into
the outbound header; the plugin never receives the plaintext.

## Storage

Storage collections must be declared in the manifest. Declare indexes and
unique indexes there so the host can enforce them.

```json
{
  "storage": {
    "loyalty_members": {
      "indexes": [["customer_id"]],
      "uniqueIndexes": [["customer_id", "store_id"]]
    }
  }
}
```

## Related

- [Capabilities and hooks](https://edgecart.dev/plugins/capabilities-hooks)
- [Settings, secrets and storage](https://edgecart.dev/plugins/settings-storage)
- [Plugin manifest](https://edgecart.dev/plugins/manifest)

Source: https://edgecart.dev/plugins/context/index.mdx
