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)
ParameterTypeDescription
supabaseSupabaseClientInitialized Supabase client instance

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)
error_messagestring | nullError message (available when failed)
started_atDate | nullTimestamp when run started
completed_atDate | nullTimestamp when run completed or failed

Waits for the run to reach a specific status or one of multiple statuses.

Parameters:

ParameterTypeRequiredDescription
statusFlowRunStatus | FlowRunStatus[]YesTarget status or array of statuses
optionsobjectNoWait options
options.timeoutMsnumberNoTimeout in milliseconds
options.signalAbortSignalNoAbort 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


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
step_slugstringStep identifier
statusFlowStepStatusCurrent step status
outputobject | nullStep output data (available when completed)
error_messagestring | nullError message (available when failed)
started_atDate | nullTimestamp when step started
completed_atDate | nullTimestamp when step completed or failed

Waits for the step to reach a specific status or one of multiple statuses.

Parameters:

ParameterTypeRequiredDescription
statusFlowStepStatus | FlowStepStatus[]YesTarget status or array of statuses
optionsobjectNoWait options
options.timeoutMsnumberNoTimeout in milliseconds
options.signalAbortSignalNoAbort 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: 'my_flow' })
.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.