Skip to main content
Mastra v1 is coming in January 2026. Get ahead by starting new projects with the beta or upgrade your existing project today.

Memory.getThreadsByResourceIdPaginated()

The .getThreadsByResourceIdPaginated() method retrieves threads associated with a specific resource ID with pagination support.

Usage ExampleDirect link to Usage Example

await memory.getThreadsByResourceIdPaginated({
resourceId: "user-123",
page: 0,
perPage: 10,
});

ParametersDirect link to Parameters

resourceId:

string
The ID of the resource whose threads are to be retrieved

page:

number
Page number to retrieve

perPage:

number
Number of threads to return per page

orderBy?:

'createdAt' | 'updatedAt'
Field to sort threads by

sortDirection?:

'ASC' | 'DESC'
Sort order direction

ReturnsDirect link to Returns

result:

Promise<PaginationInfo & { threads: StorageThreadType[] }>
A promise that resolves to paginated thread results with metadata

Extended usage exampleDirect link to Extended usage example

src/test-memory.ts
import { mastra } from "./mastra";

const agent = mastra.getAgent("agent");
const memory = await agent.getMemory();

let currentPage = 0;
let hasMorePages = true;

while (hasMorePages) {
const threads = await memory?.getThreadsByResourceIdPaginated({
resourceId: "user-123",
page: currentPage,
perPage: 25,
orderBy: "createdAt",
sortDirection: "ASC",
});

if (!threads) {
console.log("No threads");
break;
}

threads.threads.forEach((thread) => {
console.log(`Thread: ${thread.id}, Created: ${thread.createdAt}`);
});

hasMorePages = threads.hasMore;
currentPage++;
}

On this page