getWorkflowMetadata

Access run IDs and timing information within workflow functions.

Returns additional metadata available in the current workflow function.

You may want to use this function when you need to:

  • Log workflow run IDs
  • Access timing information of a workflow
  • Detect whether encryption is enabled for the current run

If you need to access step context, take a look at getStepMetadata.

import { getWorkflowMetadata } from "workflow"

async function testWorkflow() {
    "use workflow"

    const ctx = getWorkflowMetadata() 
    console.log(ctx.workflowRunId)
}

Detecting workflow runtime

You can use getWorkflowMetadata to detect whether your code is running inside a workflow context. This is useful when building shared utilities that need to behave differently inside and outside of workflows.

Since getWorkflowMetadata throws when called outside a workflow, you can wrap it in a try-catch:

import { getWorkflowMetadata } from "workflow"

function isInWorkflow(): boolean {
    try {
        getWorkflowMetadata()
        return true
    } catch {
        return false
    }
}

For example, a logging utility could include the workflow run ID when available:

import { getWorkflowMetadata } from "workflow"

function log(message: string) {
    try {
        const { workflowRunId } = getWorkflowMetadata()
        console.log(`[workflow:${workflowRunId}] ${message}`)
    } catch {
        console.log(message)
    }
}

Detecting encryption

The features object indicates which capabilities are active for the current run. Library authors can use features.encryption to control whether sensitive data is included in step return values, which are serialized to the event log:

import { getWorkflowMetadata } from "workflow"

declare function getUserProfile(userId: string): Promise<{ name: string; ssn: string }>; // @setup

async function fetchUserProfile(userId: string) {
    "use step"

    const { features } = getWorkflowMetadata() 
    const profile = await getUserProfile(userId)

    if (!features.encryption) {
        // Omit sensitive fields from the return value,
        // since it will be stored unencrypted in the event log
        const { ssn, ...safe } = profile
        return safe
    }

    return profile
}

API signature

Parameters

This function does not accept any parameters.

Returns

NameTypeDescription
workflowNamestringThe name of the workflow.
workflowRunIdstringUnique identifier for the workflow run.
workflowStartedAtDateTimestamp when the workflow run started.
urlstringThe URL where the workflow can be triggered.
features{ encryption: boolean; }Feature flags indicating which capabilities are active for this workflow run.