|
| 1 | +--- |
| 2 | +title: Trigger Workflows |
| 3 | +pcx_content_type: concept |
| 4 | +sidebar: |
| 5 | + order: 3 |
| 6 | + |
| 7 | +--- |
| 8 | + |
| 9 | +TODO - intro |
| 10 | + |
| 11 | +## Workers API (Bindings) |
| 12 | + |
| 13 | +You can interact with Workflows programmatically from any Worker script by creating a binding to a Workflow. A Worker can bind to multiple Workflows, including Workflows defined in other Workers projects (scripts) within your account. |
| 14 | + |
| 15 | +You can interact with a Workflow: |
| 16 | + |
| 17 | +* Directly over HTTP via the [`fetch`](/workers/runtime-apis/handlers/fetch/) handler |
| 18 | +* From a [Queue consumer](/queues/configuration/javascript-apis/#consumer) inside a `queue` handler |
| 19 | +* From a [Cron Trigger](/workers/configuration/cron-triggers/) inside a `scheduled` handler |
| 20 | +* Within a [Durable Object](/durable-objects/). |
| 21 | + |
| 22 | +:::note |
| 23 | + |
| 24 | +New to Workflows? Start with the [Workflows tutorial](/workflows/get-started/guide/) to deploy your first Workflow and familiarize yourself with Workflows concepts. |
| 25 | + |
| 26 | +::: |
| 27 | + |
| 28 | +To bind to a Workflow from your Workers code, you need to define a [binding](/workers/wrangler/configuration/) to a specific Workflow. For example, to bind to the Workflow defined in the [get started guide](/workflows/get-started/guide/), you would configure a `wrangler.toml` with the below: |
| 29 | + |
| 30 | +```toml title="wrangler.toml" |
| 31 | +name = "workflows-tutorial" |
| 32 | +main = "src/index.ts" |
| 33 | +compatibility_date = "2024-10-15" |
| 34 | + |
| 35 | +[[workflows]] |
| 36 | +# The name of the Workflow |
| 37 | +name = "workflows-tutorial" |
| 38 | +# The binding name, which must be a valid JavaScript variable name. This will |
| 39 | +# be how you call (run) your Workflow from your other Workers handlers or |
| 40 | +# scripts. |
| 41 | +binding = "MY_WORKFLOW" |
| 42 | + # script_name is required during for the beta. |
| 43 | + # Must match the "name" of your Worker at the top of wrangler.toml |
| 44 | +script_name = "workflows-tutorial" |
| 45 | +# Must match the class defined in your code that extends the Workflow class |
| 46 | +class_name = "MyWorkflow" |
| 47 | +``` |
| 48 | + |
| 49 | +The `binding = "MY_WORKFLOW"` line defines the JavaScript variable that our Workflow methods are accessible on, including `create` (which triggers a new instance) or `get` (which returns the status of an existing instance). |
| 50 | + |
| 51 | +```ts title="src/index.ts" |
| 52 | +interface Env { |
| 53 | + MY_WORKFLOW: Workflow; |
| 54 | +} |
| 55 | + |
| 56 | +export default { |
| 57 | + async fetch(req: Request, env: Env) { |
| 58 | + // |
| 59 | + const instanceId = new URL(req.url).searchParams.get("instanceId") |
| 60 | + |
| 61 | + // If an ?instanceId=<id> query parameter is provided, fetch the status |
| 62 | + // of an existing Workflow by its ID. |
| 63 | + if (instanceId) { |
| 64 | + let instance = await env.MY_WORKFLOW.get(id); |
| 65 | + return Response.json({ |
| 66 | + status: await instance.status(), |
| 67 | + }); |
| 68 | + } |
| 69 | + |
| 70 | + // Else, create a new instance of our Workflow, passing in any (optional) params |
| 71 | + // and return the ID. |
| 72 | + const newId = await crypto.randomUUID(); |
| 73 | + let instance = await env.MY_WORKFLOW.create(newId, {}); |
| 74 | + return Response.json({ |
| 75 | + id: instance.id, |
| 76 | + details: await instance.status(), |
| 77 | + }); |
| 78 | + |
| 79 | + return Response.json({ result }); |
| 80 | + }, |
| 81 | +}; |
| 82 | +``` |
| 83 | + |
| 84 | +## REST API (HTTP) |
| 85 | + |
| 86 | +TODO |
0 commit comments