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.getThreadsByResourceId()

The .getThreadsByResourceId() function retrieves all threads associated with a specific resource ID from storage. Threads can be sorted by creation or modification time in ascending or descending order.

Usage ExampleDirect link to Usage Example

await memory?.getThreadsByResourceId({ resourceId: "user-123" });

ParametersDirect link to Parameters

resourceId:

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

orderBy?:

ThreadOrderBy
Field to sort threads by. Accepts 'createdAt' or 'updatedAt'. Default: 'createdAt'

sortDirection?:

ThreadSortDirection
Sort order direction. Accepts 'ASC' or 'DESC'. Default: 'DESC'

ReturnsDirect link to Returns

StorageThreadType[]:

Promise
A promise that resolves to an array of threads associated with the given resource ID.

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();

const thread = await memory?.getThreadsByResourceId({
resourceId: "user-123",
orderBy: "updatedAt",
sortDirection: "ASC",
});

console.log(thread);

On this page