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

# PostGrid plugin

> Print and mail API for sending letters, postcards and self mailers, with contact, template and tracker management.

Use **PostGrid** through Corsair: one client, typed API calls.

PostGrid prints and mails physical mail on demand. Create a key under Settings in the dashboard and paste it in; it travels in the `x-api-key` header. A key created in test mode reads and writes test-mode data only, which is the right place to develop.

What to know before you print:

* **An order spends money.** `letters.create`, `postcards.create` and `selfMailers.create` print and post a physical item. There is no sandbox flag on the request: test mode is a property of the key.
* **Idempotency.** Order creation accepts an `Idempotency-Key` header, and PostGrid honours it for 24 hours. The plugin generates one per call; pass `idempotencyKey` in the input to reuse it so a retried request returns the original order instead of printing a second one.
* **Cancelling.** An order can be cancelled only while its status is `ready`. After that it is with the printer. For a send-as-soon-as-possible order that is roughly midnight Eastern on the following day.
* **Tracking.** Every order carries `status`, from `ready` through `printing` and `processed_for_delivery` to `completed` or `cancelled`. Live mail bound for the US also gains `imbStatus`, the USPS stage, plus `imbDate` and `imbZIPCode`.
* **Paging is by offset.** List routes take `skip` and `limit` and return `totalCount`; page while `skip` is below it by adding `limit` each time.
* **Addresses are corrected.** A created contact may come back with a different address than it was sent, and `addressStatus` says whether it was `verified`, `corrected` or `failed`. On an order, a `to` or `from` contact given with an `id` is sent as that id alone, so `skipVerification` and `forceVerifiedStatus` take effect only on an inline contact (address fields, no `id`).
* **Webhooks.** PostGrid can post delivery notifications to a URL you register in its dashboard, but this plugin does not handle inbound deliveries.

**What you get:**

* 21 typed API operations

## Setup

**1. Install**

```bash npm theme={null}
npm install corsair @corsair-dev/postgrid better-sqlite3
npm install --save-dev @types/better-sqlite3
```

```bash yarn theme={null}
yarn add corsair @corsair-dev/postgrid better-sqlite3
yarn add --dev @types/better-sqlite3
```

```bash pnpm theme={null}
pnpm install corsair @corsair-dev/postgrid better-sqlite3
pnpm install --save-dev @types/better-sqlite3
```

```bash bun theme={null}
bun add corsair @corsair-dev/postgrid better-sqlite3
bun add --dev @types/better-sqlite3
```

**2. Add the plugin**

```ts corsair.ts theme={null}
import Database from 'better-sqlite3';
import { createCorsair } from 'corsair';
import { postgrid } from '@corsair-dev/postgrid';

export const corsair = createCorsair({
	plugins: [
		postgrid(),
	],
	database: new Database('corsair.db'),
	kek: process.env.CORSAIR_KEK!,
	multiTenancy: true,
	hub: {
		projectApiKey: process.env.CORSAIR_API_KEY!,
		signingSecret: process.env.CORSAIR_SIGNING_SECRET!,
	},
});
```

With `multiTenancy: true`, scope calls with `corsair.withTenant(id)`. See [Quick start](https://docs.corsair.dev/quick-start.md) for KEK + Hub keys, and [Multi-tenancy](https://docs.corsair.dev/concepts/multi-tenancy.md) for account isolation.

**3. Choose authentication**

> [tab] API Key (Recommended)
>
> Store the PostGrid API key before the first call: `pnpm corsair setup --plugin=postgrid --tenant=acme api_key=<your-key>` (leave out `--tenant` without `multiTenancy`), or pass it as `key` in the plugin options. With Hub, each tenant enters their own key on the connect page. See [API key auth](https://docs.corsair.dev/concepts/api-key).
>
> ```ts theme={null}
> postgrid()
> ```
>
> More: [API Key](https://docs.corsair.dev/concepts/api-key)

**4. Connect a tenant**

Mint a connect link and send the tenant to it. Hub hosts the page and delivers the result to your app — see [Connect / OAuth](https://docs.corsair.dev/management/connect).

```ts theme={null}
const { connectUrl } = await corsair.manage.connect.createLink({
	plugin: 'postgrid',
	tenantId: 'acme',
});
// redirect the user's browser to connectUrl
```

## Example API calls

**List recent letters**

```ts theme={null}
const tenant = corsair.withTenant('acme');
await tenant.postgrid.api.letters.list({ limit: 10 });
```

**Create a template with a merge variable**

```ts theme={null}
const tenant = corsair.withTenant('acme');
await tenant.postgrid.api.templates.create({ description: 'Welcome postcard', html: '<b>Hello</b> {{to.firstName}}!' });
```

See the full list on the [API](https://docs.corsair.dev/plugins/postgrid/api.md) page.

## What's next

> [card] [API reference](https://docs.corsair.dev/plugins/postgrid/api.md)
>
> Every `postgrid.api.*` operation with input and output types.

> [card] [Connect / OAuth](https://docs.corsair.dev/management/connect)
>
> createLink, Hub delivery, and tenant connect flows.

> [card] [Use with agents](https://docs.corsair.dev/mcp-adapters/mcp-adapters)
>
> Expose this plugin's operations as MCP tools.


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