# Query Data Source — Notion

> Filter and sort the pages (rows) inside a Notion database (data source) by their property values. Discover exact property names and option values with Retrieve Database Schema first, then build a filter against them. A filter is a JSON…

- Key: `notion-query-database`
- Type: Action (Read-only)
- Version: 1.1.1
- App: Notion (`notion`) — https://pipedream.com/apps/notion.md
- This page (HTML): https://pipedream.com/apps/notion/actions/query-database
- Hints: read-only · open-world
- Source: https://github.com/PipedreamHQ/pipedream/blob/master/components/notion/actions/query-database/query-database.mjs

## Description

Filter and sort the pages (rows) inside a Notion database (data source) by their property values. **Discover exact property names and option values with Retrieve Database Schema first**, then build a filter against them. A `filter` is a JSON object: a single condition `{ "property": "Status", "select": { "equals": "Escaped" } }`, or a compound `{ "and": [ ... ] }` / `{ "or": [ ... ] }`. The condition key matches the property type — e.g. `select`, `status`, `multi_select`, `number` (`{ "greater_than": 5 }`), `checkbox` (`{ "equals": true }`), `rich_text`/`title` (`{ "contains": "..." }`), `date`. Omit `filter` to return all rows. Provide the **data source ID** (use **Search** with `filter: data_source` to resolve a database name). [See the documentation](https://developers.notion.com/reference/filter-data-source-entries)

## Props

| Prop | Type | Required | Description |
|---|---|---|---|
| `dataSourceId` | `string` | Yes | The ID of the database's data source. Use Search (filter: data_source) to resolve a database name into its ID. |
| `filter` | `string` | No | A JSON object describing the filter. Two shapes for single-condition filters: (1) Property filter — { "property": "<column-name>", "<type>": { "<operator>": <value> } }, where <type> matches the database column type (title, rich_text, number, select, status, checkbox, date, people, …) and <operator> is type-specific. Example: { "property": "Status", "select": { "equals": "Escaped" } }. Filtering by title needs { "property": "<title-column-name>", "title": { "contains": "..." } } — call Retrieve Database Schema first if you don't already know the title column's exact name. (2) Timestamp filter — uses a top-level timestamp key (NOT property) set to "created_time" or "last_edited_time", plus a matching operator object. Example: { "timestamp": "created_time", "created_time": { "past_week": {} } }. Compound filters use { "and": [ ... ] } or { "or": [ ... ] }. Omit this prop to return all rows. See the documentation. |
| `sorts` | `string` | No | Sort order as a JSON array string. Example: [ { "property": "Name", "direction": "ascending" } ]. See the documentation. |
| `pageSize` | `integer` | No | The number of items from the full list desired in the response (max 100) |
| `startCursor` | `string` | No | Pagination cursor from a previous response's next_cursor. Omit for the first page. |

## 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": "notion",
      },
    },
  },
)

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: "notion-query-database",
  arguments: {
    dataSourceId: "Data Source ID",
    filter: "Filter",
  },
})
```

**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: "notion-query-database",
  externalUserId: "{external_user_id}", // any stable ID for this user in your system
  configuredProps: {
    notion: { authProvisionId: "apn_xxxxxxx" },
    dataSourceId: "Data Source ID",
    filter: "Filter",
  },
})

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": "notion-query-database",
    "configured_props": {
      "notion": { "authProvisionId": "apn_xxxxxxx" },
      "dataSourceId": "Data Source ID",
      "filter": "Filter"
    }
  }'
```

---

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