Skip to main content

Method

client.dataSources.query(parameters: QueryDataSourceParameters): Promise<QueryDataSourceResponse>
Queries pages and data sources that are children of a data source. Supports filtering, sorting, and pagination.

Parameters

data_source_id
string
required
The ID of the data source to query (with or without dashes)
filter
PropertyFilter | TimestampFilter | { and: Filter[] } | { or: Filter[] }
Filter conditions to apply to the query. Can be a single filter, or a compound filter using and or or operators.Supports filtering by:
  • Property values (text, number, checkbox, select, date, etc.)
  • Timestamps (created_time, last_edited_time)
sorts
Array<PropertySort | TimestampSort>
Sort criteria for the results.Property sort:
{
  property: "PropertyName",
  direction: "ascending" | "descending"
}
Timestamp sort:
{
  timestamp: "created_time" | "last_edited_time",
  direction: "ascending" | "descending"
}
start_cursor
string
Pagination cursor returned from a previous query. Used to fetch the next page of results.
page_size
number
Number of results to return per page. Maximum is 100 (default).
filter_properties
string[]
Array of property IDs to include in the response. Only these properties will be returned for each page.
archived
boolean
Filter by archived status. If true, only archived pages are returned. If false, only non-archived pages.
in_trash
boolean
Filter by trash status. If true, only trashed pages are returned. If false, only non-trashed pages.
result_type
'page' | 'data_source'
Filter results to only include pages or data sources. Regular databases only support page children. The default behavior returns both pages and data sources for wikis.
auth
string
Bearer token for authentication. Overrides the client-level auth if provided.

Response

Returns a paginated list of pages and/or data sources.
object
string
Always "list"
type
string
Always "page_or_data_source"
results
Array<PageObjectResponse | DataSourceObjectResponse>
Array of page and/or data source objects matching the query
next_cursor
string | null
Cursor for the next page of results. null if there are no more results.
has_more
boolean
Whether there are more results available

Examples

Query all pages

import { Client } from "@notionhq/client"

const notion = new Client({ auth: process.env.NOTION_API_KEY })

const response = await notion.dataSources.query({
  data_source_id: "a1b2c3d4-e5f6-7890-abcd-ef1234567890"
})

console.log(`Found ${response.results.length} pages`)
for (const page of response.results) {
  console.log(`- ${page.id}`)
}

Filter by property value

const response = await notion.dataSources.query({
  data_source_id: "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
  filter: {
    property: "Status",
    select: {
      equals: "Active"
    }
  }
})

Sort by property

const response = await notion.dataSources.query({
  data_source_id: "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
  sorts: [
    {
      property: "Name",
      direction: "ascending"
    }
  ]
})

Complex filter with AND/OR

const response = await notion.dataSources.query({
  data_source_id: "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
  filter: {
    and: [
      {
        property: "Status",
        select: {
          equals: "Active"
        }
      },
      {
        or: [
          {
            property: "Priority",
            select: {
              equals: "High"
            }
          },
          {
            property: "Priority",
            select: {
              equals: "Medium"
            }
          }
        ]
      }
    ]
  }
})

Paginate through results

import { collectPaginatedAPI } from "@notionhq/client"

// Collect all results automatically
const allPages = await collectPaginatedAPI(
  notion.dataSources.query,
  {
    data_source_id: "a1b2c3d4-e5f6-7890-abcd-ef1234567890"
  }
)

console.log(`Total pages: ${allPages.length}`)

Manual pagination

let cursor = undefined
let hasMore = true

while (hasMore) {
  const response = await notion.dataSources.query({
    data_source_id: "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
    start_cursor: cursor,
    page_size: 50
  })
  
  // Process results
  for (const page of response.results) {
    console.log(page.id)
  }
  
  cursor = response.next_cursor
  hasMore = response.has_more
}

Filter by timestamp

const response = await notion.dataSources.query({
  data_source_id: "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
  filter: {
    timestamp: "last_edited_time",
    last_edited_time: {
      past_week: {}
    }
  },
  sorts: [
    {
      timestamp: "last_edited_time",
      direction: "descending"
    }
  ]
})

Build docs developers (and LLMs) love