Skip to main content

Mastra.getWorkspaceById()

The .getWorkspaceById() method returns a workspace registered with the Mastra instance. By default, it throws when no workspace exists for the ID. Pass { optional: true } to get undefined instead, for example to check whether a workspace exists before creating it.

Usage example
Direct link to Usage example

const workspace = mastra.getWorkspaceById('workspace-123')

Check for a workspace without throwing:

const existing = mastra.getWorkspaceById('workspace-456', { optional: true })
if (!existing) {
// Create and register the workspace
}

Parameters
Direct link to Parameters

id:

string
The ID of the workspace to return.

options?:

{ optional?: boolean }
Set optional to true to return undefined for an unknown ID instead of logging and throwing an error.

Returns
Direct link to Returns

workspace:

Workspace | undefined
The registered workspace. undefined only when optional is true and no workspace exists for the ID.
On this page