# Create Filter — Gmail

> Create a Gmail filter that automatically acts on incoming mail matching the given criteria. Provide at least one criterion and at least one action. Criteria are combined with AND: a message must match every criterion you set. Actions apply…

- Key: `gmail-create-filter`
- Type: Action (Write)
- Version: 0.0.1
- App: Gmail (`gmail`) — https://pipedream.com/apps/gmail.md
- This page (HTML): https://pipedream.com/apps/gmail/actions/create-filter
- Hints: open-world
- Source: https://github.com/PipedreamHQ/pipedream/blob/master/components/gmail/actions/create-filter/create-filter.mjs

## Description

Create a Gmail filter that automatically acts on incoming mail matching the given criteria. Provide at least one criterion and at least one action. Criteria are combined with AND: a message must match every criterion you set. Actions apply only to mail that arrives after the filter is created; existing messages are not touched. Example — skip the inbox for cancelled-event notices that were not on the user's calendar: `from` = `calendar-notification@google.com`, `query` = `"This event has been canceled"`, `negatedQuery` = `"removed from your calendar"`, `removeLabelIds` = `["INBOX"]`. Label IDs must be IDs, not names — use **List Labels** to resolve a label name to its ID (system labels like `INBOX`, `STARRED`, `UNREAD`, `TRASH` can be used directly). Use **List Filters** first to avoid creating duplicates. [See the documentation](https://developers.google.com/workspace/gmail/api/reference/rest/v1/users.settings.filters/create).

## Props

| Prop | Type | Required | Description |
|---|---|---|---|
| `from` | `string` | No | Match messages sent from this sender, e.g. calendar-notification@google.com. |
| `to` | `string` | No | Match messages sent to this recipient, e.g. me@example.com. |
| `subject` | `string` | No | Match messages whose subject contains this text (case-insensitive), e.g. invoice. |
| `query` | `string` | No | Match messages that match this Gmail search query, e.g. "This event has been canceled". Supports the same operators as the Gmail search box (OR, -, quotes, has:, etc.). |
| `negatedQuery` | `string` | No | Exclude messages that match this Gmail search query, e.g. "removed from your calendar". |
| `hasAttachment` | `boolean` | No | If true, only match messages that have an attachment. false has no effect (Gmail ignores it) — to exclude messages with attachments, put has:attachment in Doesn't Have. |
| `excludeChats` | `boolean` | No | If true, do not match Google Chat/Hangouts messages. false has no effect (Gmail ignores it). |
| `size` | `integer` | No | Message size in bytes to compare against, e.g. 10485760 (10 MB). Requires Size Comparison. |
| `sizeComparison` | `string` | No | Whether matching messages must be larger or smaller than Size. Requires Size. |
| `addLabelIds` | `string[]` | No | IDs of labels to add to matching messages, e.g. ["Label_42", "STARRED"]. Use List Labels to find label IDs. |
| `removeLabelIds` | `string[]` | No | IDs of labels to remove from matching messages. Use INBOX to skip the inbox (archive), UNREAD to mark as read, SPAM to never send to spam. |

## Run it

**MCP**

```ts
import { Client } from "@modelcontextprotocol/sdk/client/index.js"
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js"
import { PipedreamClient } from "@pipedream/sdk"

const pd = new PipedreamClient({
  projectId: process.env.PIPEDREAM_PROJECT_ID!,
  clientId: process.env.PIPEDREAM_CLIENT_ID!,
  clientSecret: process.env.PIPEDREAM_CLIENT_SECRET!,
  projectEnvironment: "production",
})

const accessToken = await pd.rawAccessToken

const transport = new StreamableHTTPClientTransport(
  new URL("https://remote.mcp.pipedream.net/v3"),
  {
    requestInit: {
      headers: {
        Authorization: `Bearer ${accessToken}`,
        "x-pd-project-id": process.env.PIPEDREAM_PROJECT_ID!,
        "x-pd-environment": "production",
        "x-pd-external-user-id": "{external_user_id}", // any stable ID for this user in your system
        "x-pd-app-slug": "gmail",
      },
    },
  },
)

const mcp = new Client({ name: "my-agent", version: "1.0.0" })
await mcp.connect(transport)

const { tools } = await mcp.listTools()

// listTools() hands your model this tool's input schema, so it can
// fill the arguments itself:
const result = await mcp.callTool({
  name: "gmail-create-filter",
  arguments: {
    from: "From",
    to: "To",
  },
})
```

**TypeScript**

```ts
import { PipedreamClient } from "@pipedream/sdk"

const pd = new PipedreamClient({
  projectId: process.env.PIPEDREAM_PROJECT_ID!,
  clientId: process.env.PIPEDREAM_CLIENT_ID!,
  clientSecret: process.env.PIPEDREAM_CLIENT_SECRET!,
  projectEnvironment: "production",
})

const result = await pd.actions.run({
  id: "gmail-create-filter",
  externalUserId: "{external_user_id}", // any stable ID for this user in your system
  configuredProps: {
    gmail: { authProvisionId: "apn_xxxxxxx" },
    from: "From",
    to: "To",
  },
})

console.log(result)
```

**cURL**

```bash
curl -X POST https://api.pipedream.com/v1/connect/{project_id}/actions/run \
  -H "Content-Type: application/json" \
  -H "X-PD-Environment: production" \
  -H "Authorization: Bearer {access_token}" \
  -d '{
    "external_user_id": "{external_user_id}",
    "id": "gmail-create-filter",
    "configured_props": {
      "gmail": { "authProvisionId": "apn_xxxxxxx" },
      "from": "From",
      "to": "To"
    }
  }'
```

---

- App: https://pipedream.com/apps/gmail.md · All apps: https://pipedream.com/apps
