# Run your Flow

import { Aside, Steps, Tabs, CardGrid, LinkCard, FileTree } from "@astrojs/starlight/components";

Now that you've defined and compiled your flow, it's time to execute it!

In this guide, we'll set up an Edge Worker to process your flow tasks, trigger your first flow, and observe its execution.

<Aside type="caution" title="Prerequisites">
Before starting, make sure you have completed:
- [Create your first flow](/get-started/flows/create-flow/) guide
- [Compile flow to SQL](/get-started/flows/compile-flow/) guide
</Aside>

<Steps>

1. ### Create a worker function

   Create a new Edge Function that will process tasks for your flow:

   ```bash frame="none"
   npx supabase functions new greet_user_worker
   ```

   Replace contents of `index.ts` file with the following:

   ```typescript title="supabase/functions/greet_user_worker/index.ts"
   import { EdgeWorker } from "jsr:@pgflow/edge-worker";
   import GreetUser from '../_flows/greet_user.ts';

   // Pass the flow definition to the Edge Worker
   EdgeWorker.start(GreetUser);
   ```

   <Aside title="The Edge Worker will:">
   1. Poll for tasks on the `greet_user` queue
   2. Process tasks based on the step slug
   3. Return the appropriate result for each step
   4. Handle retries if any step fails
   </Aside>

2. ### Disable JWT verification

   Edit `supabase/config.toml` to disable JWT verification for local development:

   ```diff lang="toml"
     [functions.greet_user_worker]
     enabled = true
   - verify_jwt = true
   + verify_jwt = false
     import_map = "./functions/greet_user_worker/deno.json"
   ```

3. ### Start the Edge Runtime

   Start the Edge Runtime to make your worker function available:

   ```bash frame="none"
   npx supabase functions serve
   ```

   This will start the Edge Runtime server but not yet start your worker.
   You should see similar output in your terminal:

   ```
   Setting up Edge Functions runtime...
   Serving functions on http://127.0.0.1:54321/functions/v1/<function-name>
   Using supabase-edge-runtime-1.67.4 (compatible with Deno v1.45.2)
   serving the request with supabase/functions/greet_user_worker
   ```

4. ### Start your worker

   In a new terminal, send an HTTP request to start your worker:

   ```bash frame="none"
   curl http://localhost:54321/functions/v1/greet_user_worker
   ```

   You should see output in your Edge Runtime terminal indicating the worker has started:

   ```
   [Info] [INFO] worker_id=unknown module=DenoAdapter DenoAdapter logger instance created and working.

   [Info] [INFO] worker_id=unknown module=DenoAdapter HTTP Request: null
   ```

   <Aside type="caution">
   If you have troubles starting the worker, make sure you disabled JWT verification in the previous step.
   </Aside>

5. ### Trigger your first flow

   Now let's start a flow run! Using Supabase Studio:

   1. Open Supabase Studio in your browser (typically at http://localhost:54323)
   2. Navigate to the **SQL Editor**
   3. Execute this SQL to start your flow:

   ```sql
   SELECT * FROM pgflow.start_flow(
     flow_slug => 'greet_user',
     input => '{"first_name": "Alice", "last_name": "Smith"}'::jsonb
   );
   ```

   The output should look like:

   ```
   run_id        | flow_slug    | status  | input                                         | output | remaining_steps
   --------------+--------------+---------+-----------------------------------------------+--------+-----------------
   <run_id UUID> | greet_user   | started | {"first_name": "Alice", "last_name": "Smith"} | null   | 2
   ```

6. ### Monitor execution

   Your worker should start processing tasks immediately. You should see log output in the Edge Runtime terminal as each step executes:

   ```
   [Info] [INFO] worker_id=<long UUID> module=ExecutionController Scheduling execution of task 1

   [Info] [INFO] worker_id=<long UUID> module=ExecutionController Scheduling execution of task 2
   ```

   You can check the run status using Supabase Studio:

   In the SQL Editor, run this query:

   ```sql
   SELECT * FROM pgflow.runs
   WHERE flow_slug = 'greet_user'
   ORDER BY started_at DESC
   LIMIT 1;
   ```

   As steps complete, you'll see the `remaining_steps` count decrease. When it reaches 0, the run will be marked as `completed`:

   ```
   run_id        | flow_slug    | status    | input                                         | output                              | remaining_steps
   --------------+--------------+-----------+-----------------------------------------------+-------------------------------------+-----------------
   <run_id UUID> | greet_user   | completed | {"first_name": "Alice", "last_name": "Smith"} | {"greeting": "Hello, Alice Smith!"} | 0
   ```

</Steps>

## Where next?

<CardGrid>
  <LinkCard
    title="How pgflow works"
    href="/concepts/how-pgflow-works/"
    description="Understand how pgflow executes flows behind the scenes"
  />
  <LinkCard
    title="Ship to Supabase"
    href="/deploy/supabase/deploy-first-flow/"
    description="Deploy your flows to production on Supabase's hosted platform"
  />
  <LinkCard
    title="Make flows resilient"
    href="/reference/configuration/step-execution/"
    description="Configure retry behavior, timeouts, and error handling for your flows"
  />
  <LinkCard
    title="Observe runs with SQL"
    href="/deploy/monitor-execution/"
    description="Learn how to monitor and debug your flow execution with SQL queries"
  />
</CardGrid>