Skip to content

@pgflow/client API

TypeScript client library for starting pgflow flows, monitoring execution, and handling real-time updates via Supabase.

npm install @pgflow/client
  • Supabase project with pgflow schema installed
  • Real-time enabled for live updates
  • Proper permissions for the pgflow schema
  • pgflow added to exposed schemas in Supabase configuration

Main client class for interacting with pgflow flows.

new PgflowClient(supabase: SupabaseClient, options?: PgflowClientOptions)
ParameterTypeRequiredDescription
supabaseSupabaseClientYesInitialized Supabase client instance
optionsPgflowClientOptionsNoClient configuration options

PgflowClientOptions:

realtimeStabilizationDelayMs (number, default: 300)
Section titled “realtimeStabilizationDelayMs (number, default: 300)”

Delay in milliseconds after subscribing to Supabase Realtime channels. This addresses a known Supabase Realtime limitation where the backend routing isn’t fully established when the SUBSCRIBED event is emitted. Increase to 400-500ms if experiencing missed events or connection issues.

startFlow<TFlow>(flow_slug, input, run_id?)

Section titled “startFlow<TFlow>(flow_slug, input, run_id?)”

Starts a new flow execution.

Parameters:

ParameterTypeRequiredDescription
flow_slugstringYesFlow identifier (must match a flow definition)
inputobjectYesFlow input data (must be JSON-serializable)
run_idstringNoCustom run identifier (UUID generated if not provided)

Returns: Promise<FlowRun>

Type Parameter:

  • TFlow - Flow definition type for type-safe input/output (when using with @pgflow/dsl)

Retrieves an existing flow run.

Parameters:

ParameterTypeRequiredDescription
run_idstringYesUnique run identifier

Returns: Promise<FlowRun | null> - FlowRun instance or null if not found


Cleans up resources for a specific run (unsubscribes from real-time updates).

Parameters:

ParameterTypeRequiredDescription
run_idstringYesRun identifier to dispose

Returns: void


Cleans up resources for all runs managed by this client instance.

Returns: void

Represents a single flow execution instance.

PropertyTypeDescription
run_idstringUnique run identifier
flow_slugstringFlow identifier
statusFlowRunStatusCurrent run status
inputobjectFlow input data
outputobject | nullFlow output data (available when completed)
errorError | nullError object (available when failed)
error_messagestring | nullError message (available when failed)
started_atDate | nullTimestamp when run started
completed_atDate | nullTimestamp when run completed
failed_atDate | nullTimestamp when run failed
remaining_stepsnumberNumber of steps remaining to complete

Waits for the run to reach a terminal status (completed or failed).

Parameters:

ParameterTypeRequiredDescription
status'completed' | 'failed'YesTarget terminal status to wait for
optionsobjectNoWait options

Wait options:

Timeout in milliseconds.

Abort signal to cancel waiting.

Returns: Promise<FlowRun> - Updated FlowRun instance

Throws: Error if timeout is reached or operation is aborted


Accesses a specific step within the flow run.

Parameters:

ParameterTypeRequiredDescription
step_slugstringYesStep identifier

Returns: FlowStep - FlowStep instance for the specified step


Checks if a step exists in the flow run.

Parameters:

ParameterTypeRequiredDescription
step_slugstringYesStep identifier to check

Returns: boolean - true if the step exists, false otherwise


Subscribes to run-level events.

Parameters:

ParameterTypeRequiredDescription
event'started' | 'completed' | 'failed' | '*'YesEvent type to listen for
handler(event: FlowRunEvent) => voidYesEvent handler function

Returns: () => void - Unsubscribe function

Event Payloads:

Flow run events are discriminated unions - each event type has different fields:

// 'started' event
{
event_type: 'run:started';
run_id: string;
flow_slug: string;
status: 'started';
input: object;
started_at: string; // ISO timestamp
remaining_steps: number;
}
// 'completed' event
{
event_type: 'run:completed';
run_id: string;
flow_slug: string;
status: 'completed';
output: object;
completed_at: string; // ISO timestamp
}
// 'failed' event
{
event_type: 'run:failed';
run_id: string;
flow_slug: string;
status: 'failed';
error_message: string;
failed_at: string; // ISO timestamp
}

Note: Event timestamps are ISO strings. The corresponding FlowRun properties convert these to Date objects.

Represents a single step within a flow run.

PropertyTypeDescription
run_idstringRun identifier this step belongs to
step_slugstringStep identifier
statusFlowStepStatusCurrent step status
outputobject | nullStep output data (available when completed)
errorError | nullError object (available when failed)
error_messagestring | nullError message (available when failed)
started_atDate | nullTimestamp when step started
completed_atDate | nullTimestamp when step completed
failed_atDate | nullTimestamp when step failed

Waits for the step to reach a specific status.

Parameters:

ParameterTypeRequiredDescription
status'started' | 'completed' | 'failed'YesTarget status to wait for
optionsobjectNoWait options

Wait options:

Timeout in milliseconds.

Abort signal to cancel waiting.

Returns: Promise<FlowStep> - Updated FlowStep instance

Throws: Error if timeout is reached or operation is aborted


Subscribes to step-level events.

Parameters:

ParameterTypeRequiredDescription
event'started' | 'completed' | 'failed' | '*'YesEvent type to listen for
handler(event: FlowStepEvent) => voidYesEvent handler function

Returns: () => void - Unsubscribe function

Event Payloads:

Flow step events are discriminated unions - each event type has different fields:

// 'started' event
{
event_type: 'step:started';
run_id: string;
step_slug: string;
status: 'started';
started_at: string; // ISO timestamp
}
// 'completed' event
{
event_type: 'step:completed';
run_id: string;
step_slug: string;
status: 'completed';
output: object;
completed_at: string; // ISO timestamp
}
// 'failed' event
{
event_type: 'step:failed';
run_id: string;
step_slug: string;
status: 'failed';
error_message: string;
failed_at: string; // ISO timestamp
}

Note: Event timestamps are ISO strings. The corresponding FlowStep properties convert these to Date objects.

Subscribe to flow-level status changes.

EventTriggerPayload
'started'Run begins executionFlowRunEvent with status 'started'
'completed'Run completes successfullyFlowRunEvent with status 'completed' and output
'failed'Run failsFlowRunEvent with status 'failed' and error_message
'*'Any status changeFlowRunEvent with current status

Subscribe to individual step status changes.

EventTriggerPayload
'started'Step begins executionFlowStepEvent with status 'started'
'completed'Step completes successfullyFlowStepEvent with status 'completed' and output
'failed'Step failsFlowStepEvent with status 'failed' and error_message
'*'Any status changeFlowStepEvent with current status

Flow run states.

ValueDescription
'started'Run is executing
'completed'Run completed successfully
'failed'Run failed with an error

Individual step states.

ValueDescription
'created'Step created but not yet started
'started'Step is executing
'completed'Step completed successfully
'failed'Step failed with an error

When using with @pgflow/dsl, the client provides full type inference for flow inputs, outputs, and step access.

import { Flow } from '@pgflow/dsl';
const MyFlow = new Flow<{ url: string }>({ slug: 'myFlow' })
.step({ slug: 'step1' }, async (input) => ({ data: 'result' }));
// Type-safe flow execution
const run = await pgflow.startFlow<typeof MyFlow>(
MyFlow.slug,
{ url: 'https://example.com' } // TypeScript validates this
);
// Type-safe step access
const step = run.step('step1'); // TypeScript knows this step exists

The client can be loaded via CDN for browser environments.

<script src="https://unpkg.com/@pgflow/client"></script>
<script>
const pgflow = window.pgflow.createClient(supabase);
</script>

See the build and release documentation for CDN usage details.

Chat with Author