# Webhooks

## Create Webhook

`client.printMail.webhooks.create(WebhookCreateParamsbody, RequestOptionsoptions?): Webhook`

**post** `/print-mail/v1/webhooks`

Create a Webhook.

### Parameters

- `body: WebhookCreateParams`

  - `enabledEvents: Array<"letter.created" | "letter.updated" | "postcard.created" | 18 more>`

    The list of event types this webhook listens for.

    - `"letter.created"`

    - `"letter.updated"`

    - `"postcard.created"`

    - `"postcard.updated"`

    - `"self_mailer.created"`

    - `"self_mailer.updated"`

    - `"cheque.created"`

    - `"cheque.updated"`

    - `"box.created"`

    - `"box.updated"`

    - `"snap_pack.created"`

    - `"snap_pack.updated"`

    - `"return_envelope_order.created"`

    - `"return_envelope_order.updated"`

    - `"tracker.visited"`

    - `"campaign.created"`

    - `"campaign.updated"`

    - `"virtual_mailbox_item.created"`

    - `"postal_statement.created"`

    - `"document.created"`

    - `"document.updated"`

  - `url: string`

    An HTTPS URL that PostGrid can invoke for webhook deliveries.

  - `description?: string`

    An optional string describing this resource. Will be visible in the API and the dashboard.

  - `metadata?: Record<string, unknown>`

    See the section on Metadata.

  - `payloadFormat?: "jwt" | "json"`

    The format in which a Webhook's event payload is delivered.

    - `"jwt"`

    - `"json"`

  - `secret?: string`

    A webhook signing secret with at least 20 non-whitespace characters.

### Returns

- `Webhook`

  - `id: string`

    A unique ID prefixed with webhook_

  - `createdAt: string`

    The UTC time at which this resource was created.

  - `enabled: boolean`

    Whether this webhook is enabled. Disabled webhooks are not triggered.

  - `enabledEvents: Array<"letter.created" | "letter.updated" | "postcard.created" | 18 more>`

    The list of event types this webhook listens for.

    - `"letter.created"`

    - `"letter.updated"`

    - `"postcard.created"`

    - `"postcard.updated"`

    - `"self_mailer.created"`

    - `"self_mailer.updated"`

    - `"cheque.created"`

    - `"cheque.updated"`

    - `"box.created"`

    - `"box.updated"`

    - `"snap_pack.created"`

    - `"snap_pack.updated"`

    - `"return_envelope_order.created"`

    - `"return_envelope_order.updated"`

    - `"tracker.visited"`

    - `"campaign.created"`

    - `"campaign.updated"`

    - `"virtual_mailbox_item.created"`

    - `"postal_statement.created"`

    - `"document.created"`

    - `"document.updated"`

  - `live: boolean`

    `true` if this is a live mode resource else `false`.

  - `object: "webhook"`

    Always `webhook`.

    - `"webhook"`

  - `updatedAt: string`

    The UTC time at which this resource was last updated.

  - `url: string`

    An HTTPS URL that PostGrid can invoke for webhook deliveries.

  - `description?: string`

    An optional string describing this resource. Will be visible in the API and the dashboard.

  - `metadata?: Record<string, unknown>`

    See the section on Metadata.

  - `payloadFormat?: "jwt" | "json"`

    The format in which a Webhook's event payload is delivered.

    - `"jwt"`

    - `"json"`

  - `secret?: string`

    A webhook signing secret with at least 20 non-whitespace characters.

### Example

```typescript
import PostGrid from 'postgrid-node';

const client = new PostGrid({
  printMailAPIKey: process.env['POSTGRID_PRINT_MAIL_API_KEY'], // This is the default and can be omitted
});

const webhook = await client.printMail.webhooks.create({
  enabledEvents: ['letter.created'],
  url: 'https://example.com/postgrid-webhook',
  description: 'Letter Created',
});

console.log(webhook.id);
```

#### Response

```json
{
  "id": "webhook_skN2ZTvFwS62oc5tJY1gzw",
  "object": "webhook",
  "live": false,
  "description": "Letter Created",
  "url": "https://example.com/postgrid-webhook",
  "enabledEvents": [
    "letter.created"
  ],
  "payloadFormat": "jwt",
  "enabled": true,
  "secret": "webhook_secret_xkNXtQ4jHevFdkTG3mSf1Q",
  "createdAt": "2022-02-16T18:37:02.048Z",
  "updatedAt": "2022-02-16T18:37:02.048Z"
}
```

## List Webhooks

`client.printMail.webhooks.list(WebhookListParamsquery?, RequestOptionsoptions?): SkipLimit<Webhook>`

**get** `/print-mail/v1/webhooks`

Retrieve a paginated list of Webhooks.

### Parameters

- `query: WebhookListParams`

  - `limit?: number`

  - `search?: string`

    You can supply any string to help narrow down the list of resources. For example, if you pass `"New York"` (quoted), it will return resources that have that string present somewhere in their response. Alternatively, you can supply a structured search query. See the documentation on `StructuredSearchQuery` for more details.

  - `skip?: number`

### Returns

- `Webhook`

  - `id: string`

    A unique ID prefixed with webhook_

  - `createdAt: string`

    The UTC time at which this resource was created.

  - `enabled: boolean`

    Whether this webhook is enabled. Disabled webhooks are not triggered.

  - `enabledEvents: Array<"letter.created" | "letter.updated" | "postcard.created" | 18 more>`

    The list of event types this webhook listens for.

    - `"letter.created"`

    - `"letter.updated"`

    - `"postcard.created"`

    - `"postcard.updated"`

    - `"self_mailer.created"`

    - `"self_mailer.updated"`

    - `"cheque.created"`

    - `"cheque.updated"`

    - `"box.created"`

    - `"box.updated"`

    - `"snap_pack.created"`

    - `"snap_pack.updated"`

    - `"return_envelope_order.created"`

    - `"return_envelope_order.updated"`

    - `"tracker.visited"`

    - `"campaign.created"`

    - `"campaign.updated"`

    - `"virtual_mailbox_item.created"`

    - `"postal_statement.created"`

    - `"document.created"`

    - `"document.updated"`

  - `live: boolean`

    `true` if this is a live mode resource else `false`.

  - `object: "webhook"`

    Always `webhook`.

    - `"webhook"`

  - `updatedAt: string`

    The UTC time at which this resource was last updated.

  - `url: string`

    An HTTPS URL that PostGrid can invoke for webhook deliveries.

  - `description?: string`

    An optional string describing this resource. Will be visible in the API and the dashboard.

  - `metadata?: Record<string, unknown>`

    See the section on Metadata.

  - `payloadFormat?: "jwt" | "json"`

    The format in which a Webhook's event payload is delivered.

    - `"jwt"`

    - `"json"`

  - `secret?: string`

    A webhook signing secret with at least 20 non-whitespace characters.

### Example

```typescript
import PostGrid from 'postgrid-node';

const client = new PostGrid({
  printMailAPIKey: process.env['POSTGRID_PRINT_MAIL_API_KEY'], // This is the default and can be omitted
});

// Automatically fetches more pages as needed.
for await (const webhook of client.printMail.webhooks.list()) {
  console.log(webhook.id);
}
```

#### Response

```json
{
  "object": "list",
  "limit": 10,
  "skip": 0,
  "totalCount": 1,
  "data": [
    {
      "id": "webhook_skN2ZTvFwS62oc5tJY1gzw",
      "object": "webhook",
      "live": false,
      "description": "Letter Created",
      "url": "https://example.com/postgrid-webhook",
      "enabledEvents": [
        "letter.created"
      ],
      "payloadFormat": "jwt",
      "enabled": true,
      "secret": "webhook_secret_xkNXtQ4jHevFdkTG3mSf1Q",
      "createdAt": "2022-02-16T18:37:02.048Z",
      "updatedAt": "2022-02-16T18:37:02.048Z"
    }
  ]
}
```

## Update Webhook

`client.printMail.webhooks.update(stringid, WebhookUpdateParamsbody, RequestOptionsoptions?): Webhook`

**post** `/print-mail/v1/webhooks/{id}`

Update a Webhook by ID.

### Parameters

- `id: string`

- `body: WebhookUpdateParams`

  - `description?: string`

    An optional string describing this resource. Will be visible in the API and the dashboard.

  - `enabled?: boolean`

    Whether this webhook is enabled. Disabled webhooks are not triggered.

  - `enabledEvents?: Array<"letter.created" | "letter.updated" | "postcard.created" | 18 more>`

    The list of event types this webhook listens for.

    - `"letter.created"`

    - `"letter.updated"`

    - `"postcard.created"`

    - `"postcard.updated"`

    - `"self_mailer.created"`

    - `"self_mailer.updated"`

    - `"cheque.created"`

    - `"cheque.updated"`

    - `"box.created"`

    - `"box.updated"`

    - `"snap_pack.created"`

    - `"snap_pack.updated"`

    - `"return_envelope_order.created"`

    - `"return_envelope_order.updated"`

    - `"tracker.visited"`

    - `"campaign.created"`

    - `"campaign.updated"`

    - `"virtual_mailbox_item.created"`

    - `"postal_statement.created"`

    - `"document.created"`

    - `"document.updated"`

  - `metadata?: Record<string, unknown>`

    See the section on Metadata.

  - `payloadFormat?: "jwt" | "json"`

    The format in which a Webhook's event payload is delivered.

    - `"jwt"`

    - `"json"`

  - `secret?: string`

    A webhook signing secret with at least 20 non-whitespace characters.

  - `url?: string`

    An HTTPS URL that PostGrid can invoke for webhook deliveries.

### Returns

- `Webhook`

  - `id: string`

    A unique ID prefixed with webhook_

  - `createdAt: string`

    The UTC time at which this resource was created.

  - `enabled: boolean`

    Whether this webhook is enabled. Disabled webhooks are not triggered.

  - `enabledEvents: Array<"letter.created" | "letter.updated" | "postcard.created" | 18 more>`

    The list of event types this webhook listens for.

    - `"letter.created"`

    - `"letter.updated"`

    - `"postcard.created"`

    - `"postcard.updated"`

    - `"self_mailer.created"`

    - `"self_mailer.updated"`

    - `"cheque.created"`

    - `"cheque.updated"`

    - `"box.created"`

    - `"box.updated"`

    - `"snap_pack.created"`

    - `"snap_pack.updated"`

    - `"return_envelope_order.created"`

    - `"return_envelope_order.updated"`

    - `"tracker.visited"`

    - `"campaign.created"`

    - `"campaign.updated"`

    - `"virtual_mailbox_item.created"`

    - `"postal_statement.created"`

    - `"document.created"`

    - `"document.updated"`

  - `live: boolean`

    `true` if this is a live mode resource else `false`.

  - `object: "webhook"`

    Always `webhook`.

    - `"webhook"`

  - `updatedAt: string`

    The UTC time at which this resource was last updated.

  - `url: string`

    An HTTPS URL that PostGrid can invoke for webhook deliveries.

  - `description?: string`

    An optional string describing this resource. Will be visible in the API and the dashboard.

  - `metadata?: Record<string, unknown>`

    See the section on Metadata.

  - `payloadFormat?: "jwt" | "json"`

    The format in which a Webhook's event payload is delivered.

    - `"jwt"`

    - `"json"`

  - `secret?: string`

    A webhook signing secret with at least 20 non-whitespace characters.

### Example

```typescript
import PostGrid from 'postgrid-node';

const client = new PostGrid({
  printMailAPIKey: process.env['POSTGRID_PRINT_MAIL_API_KEY'], // This is the default and can be omitted
});

const webhook = await client.printMail.webhooks.update('id', {
  description: 'Letter creates and updates',
  enabled: true,
  enabledEvents: ['letter.created', 'letter.updated'],
  url: 'https://example.com/postgrid-webhook',
});

console.log(webhook.id);
```

#### Response

```json
{
  "id": "webhook_skN2ZTvFwS62oc5tJY1gzw",
  "object": "webhook",
  "live": false,
  "description": "Letter creates and updates",
  "url": "https://example.com/postgrid-webhook",
  "enabledEvents": [
    "letter.created",
    "letter.updated"
  ],
  "payloadFormat": "jwt",
  "enabled": true,
  "secret": "webhook_secret_xkNXtQ4jHevFdkTG3mSf1Q",
  "createdAt": "2022-02-16T18:37:02.048Z",
  "updatedAt": "2022-02-16T18:37:14.021Z"
}
```

## Get Webhook

`client.printMail.webhooks.retrieve(stringid, RequestOptionsoptions?): Webhook`

**get** `/print-mail/v1/webhooks/{id}`

Retrieve a Webhook by ID.

### Parameters

- `id: string`

### Returns

- `Webhook`

  - `id: string`

    A unique ID prefixed with webhook_

  - `createdAt: string`

    The UTC time at which this resource was created.

  - `enabled: boolean`

    Whether this webhook is enabled. Disabled webhooks are not triggered.

  - `enabledEvents: Array<"letter.created" | "letter.updated" | "postcard.created" | 18 more>`

    The list of event types this webhook listens for.

    - `"letter.created"`

    - `"letter.updated"`

    - `"postcard.created"`

    - `"postcard.updated"`

    - `"self_mailer.created"`

    - `"self_mailer.updated"`

    - `"cheque.created"`

    - `"cheque.updated"`

    - `"box.created"`

    - `"box.updated"`

    - `"snap_pack.created"`

    - `"snap_pack.updated"`

    - `"return_envelope_order.created"`

    - `"return_envelope_order.updated"`

    - `"tracker.visited"`

    - `"campaign.created"`

    - `"campaign.updated"`

    - `"virtual_mailbox_item.created"`

    - `"postal_statement.created"`

    - `"document.created"`

    - `"document.updated"`

  - `live: boolean`

    `true` if this is a live mode resource else `false`.

  - `object: "webhook"`

    Always `webhook`.

    - `"webhook"`

  - `updatedAt: string`

    The UTC time at which this resource was last updated.

  - `url: string`

    An HTTPS URL that PostGrid can invoke for webhook deliveries.

  - `description?: string`

    An optional string describing this resource. Will be visible in the API and the dashboard.

  - `metadata?: Record<string, unknown>`

    See the section on Metadata.

  - `payloadFormat?: "jwt" | "json"`

    The format in which a Webhook's event payload is delivered.

    - `"jwt"`

    - `"json"`

  - `secret?: string`

    A webhook signing secret with at least 20 non-whitespace characters.

### Example

```typescript
import PostGrid from 'postgrid-node';

const client = new PostGrid({
  printMailAPIKey: process.env['POSTGRID_PRINT_MAIL_API_KEY'], // This is the default and can be omitted
});

const webhook = await client.printMail.webhooks.retrieve('id');

console.log(webhook.id);
```

#### Response

```json
{
  "id": "webhook_skN2ZTvFwS62oc5tJY1gzw",
  "object": "webhook",
  "live": false,
  "description": "Letter Created",
  "url": "https://example.com/postgrid-webhook",
  "enabledEvents": [
    "letter.created"
  ],
  "payloadFormat": "jwt",
  "enabled": true,
  "secret": "webhook_secret_xkNXtQ4jHevFdkTG3mSf1Q",
  "createdAt": "2022-02-16T18:37:02.048Z",
  "updatedAt": "2022-02-16T18:37:02.048Z"
}
```

## Delete Webhook

`client.printMail.webhooks.delete(stringid, RequestOptionsoptions?): WebhookDeleteResponse`

**delete** `/print-mail/v1/webhooks/{id}`

Delete a Webhook by ID. Note that this operation cannot be undone.

### Parameters

- `id: string`

### Returns

- `WebhookDeleteResponse`

  - `id: string`

    A unique ID prefixed with webhook_

  - `deleted: true`

    - `true`

  - `object: "webhook"`

    Always `webhook`.

    - `"webhook"`

### Example

```typescript
import PostGrid from 'postgrid-node';

const client = new PostGrid({
  printMailAPIKey: process.env['POSTGRID_PRINT_MAIL_API_KEY'], // This is the default and can be omitted
});

const webhook = await client.printMail.webhooks.delete('id');

console.log(webhook.id);
```

#### Response

```json
{
  "id": "webhook_skN2ZTvFwS62oc5tJY1gzw",
  "object": "webhook",
  "deleted": true
}
```

## List Webhook Invocations

`client.printMail.webhooks.listInvocations(stringid, WebhookListInvocationsParamsquery?, RequestOptionsoptions?): SkipLimit<WebhookInvocation>`

**get** `/print-mail/v1/webhooks/{id}/invocations`

Retrieve a paginated list of invocations for a Webhook.

### Parameters

- `id: string`

- `query: WebhookListInvocationsParams`

  - `limit?: number`

  - `search?: string`

    You can supply any string to help narrow down the list of resources. For example, if you pass `"New York"` (quoted), it will return resources that have that string present somewhere in their response. Alternatively, you can supply a structured search query. See the documentation on `StructuredSearchQuery` for more details.

  - `skip?: number`

### Returns

- `WebhookInvocation`

  - `id: string`

    A unique ID prefixed with `webhook_invocation_`.

  - `createdAt: string`

    The UTC time at which this invocation was created.

  - `event: string`

    The ID of the event that was delivered in this invocation.

  - `object: "webhook_invocation"`

    Always `webhook_invocation`.

    - `"webhook_invocation"`

  - `statusCode: number`

    The HTTP status code returned by your endpoint for this invocation.

  - `type: "letter.created" | "letter.updated" | "postcard.created" | 18 more`

    The type of event that a Webhook can listen for and that an Event represents.

    - `"letter.created"`

    - `"letter.updated"`

    - `"postcard.created"`

    - `"postcard.updated"`

    - `"self_mailer.created"`

    - `"self_mailer.updated"`

    - `"cheque.created"`

    - `"cheque.updated"`

    - `"box.created"`

    - `"box.updated"`

    - `"snap_pack.created"`

    - `"snap_pack.updated"`

    - `"return_envelope_order.created"`

    - `"return_envelope_order.updated"`

    - `"tracker.visited"`

    - `"campaign.created"`

    - `"campaign.updated"`

    - `"virtual_mailbox_item.created"`

    - `"postal_statement.created"`

    - `"document.created"`

    - `"document.updated"`

  - `updatedAt: string`

    The UTC time at which this invocation was last updated.

  - `webhook: string`

    The ID of the webhook that was invoked.

  - `orderID?: string`

    The ID of the order associated with this invocation, if the event was order-related.

### Example

```typescript
import PostGrid from 'postgrid-node';

const client = new PostGrid({
  printMailAPIKey: process.env['POSTGRID_PRINT_MAIL_API_KEY'], // This is the default and can be omitted
});

// Automatically fetches more pages as needed.
for await (const webhookInvocation of client.printMail.webhooks.listInvocations('id')) {
  console.log(webhookInvocation.id);
}
```

#### Response

```json
{
  "object": "list",
  "limit": 10,
  "skip": 0,
  "totalCount": 1,
  "data": [
    {
      "id": "webhook_invocation_6TAGtJezjUyGFnznTRdX37",
      "object": "webhook_invocation",
      "webhook": "webhook_skN2ZTvFwS62oc5tJY1gzw",
      "event": "event_r1nfP4xAacyqMYtt7PeyFD",
      "orderID": "letter_rqfnwvzq5TUsYR2r6L9V8X",
      "type": "letter.created",
      "statusCode": 200,
      "createdAt": "2021-04-02T02:09:32.066Z",
      "updatedAt": "2021-04-02T02:09:32.066Z"
    }
  ]
}
```

## Domain Types

### Webhook

- `Webhook`

  - `id: string`

    A unique ID prefixed with webhook_

  - `createdAt: string`

    The UTC time at which this resource was created.

  - `enabled: boolean`

    Whether this webhook is enabled. Disabled webhooks are not triggered.

  - `enabledEvents: Array<"letter.created" | "letter.updated" | "postcard.created" | 18 more>`

    The list of event types this webhook listens for.

    - `"letter.created"`

    - `"letter.updated"`

    - `"postcard.created"`

    - `"postcard.updated"`

    - `"self_mailer.created"`

    - `"self_mailer.updated"`

    - `"cheque.created"`

    - `"cheque.updated"`

    - `"box.created"`

    - `"box.updated"`

    - `"snap_pack.created"`

    - `"snap_pack.updated"`

    - `"return_envelope_order.created"`

    - `"return_envelope_order.updated"`

    - `"tracker.visited"`

    - `"campaign.created"`

    - `"campaign.updated"`

    - `"virtual_mailbox_item.created"`

    - `"postal_statement.created"`

    - `"document.created"`

    - `"document.updated"`

  - `live: boolean`

    `true` if this is a live mode resource else `false`.

  - `object: "webhook"`

    Always `webhook`.

    - `"webhook"`

  - `updatedAt: string`

    The UTC time at which this resource was last updated.

  - `url: string`

    An HTTPS URL that PostGrid can invoke for webhook deliveries.

  - `description?: string`

    An optional string describing this resource. Will be visible in the API and the dashboard.

  - `metadata?: Record<string, unknown>`

    See the section on Metadata.

  - `payloadFormat?: "jwt" | "json"`

    The format in which a Webhook's event payload is delivered.

    - `"jwt"`

    - `"json"`

  - `secret?: string`

    A webhook signing secret with at least 20 non-whitespace characters.

### Webhook Invocation

- `WebhookInvocation`

  - `id: string`

    A unique ID prefixed with `webhook_invocation_`.

  - `createdAt: string`

    The UTC time at which this invocation was created.

  - `event: string`

    The ID of the event that was delivered in this invocation.

  - `object: "webhook_invocation"`

    Always `webhook_invocation`.

    - `"webhook_invocation"`

  - `statusCode: number`

    The HTTP status code returned by your endpoint for this invocation.

  - `type: "letter.created" | "letter.updated" | "postcard.created" | 18 more`

    The type of event that a Webhook can listen for and that an Event represents.

    - `"letter.created"`

    - `"letter.updated"`

    - `"postcard.created"`

    - `"postcard.updated"`

    - `"self_mailer.created"`

    - `"self_mailer.updated"`

    - `"cheque.created"`

    - `"cheque.updated"`

    - `"box.created"`

    - `"box.updated"`

    - `"snap_pack.created"`

    - `"snap_pack.updated"`

    - `"return_envelope_order.created"`

    - `"return_envelope_order.updated"`

    - `"tracker.visited"`

    - `"campaign.created"`

    - `"campaign.updated"`

    - `"virtual_mailbox_item.created"`

    - `"postal_statement.created"`

    - `"document.created"`

    - `"document.updated"`

  - `updatedAt: string`

    The UTC time at which this invocation was last updated.

  - `webhook: string`

    The ID of the webhook that was invoked.

  - `orderID?: string`

    The ID of the order associated with this invocation, if the event was order-related.

### Webhook Delete Response

- `WebhookDeleteResponse`

  - `id: string`

    A unique ID prefixed with webhook_

  - `deleted: true`

    - `true`

  - `object: "webhook"`

    Always `webhook`.

    - `"webhook"`
