-
Notifications
You must be signed in to change notification settings - Fork 10.4k
workflows: show how to bind from Pages #18687
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 8 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
56f9abd
workflows: show how to bind from Pages
elithrar 488c3a2
fix component import
elithrar 02bd63c
fix code blocks
elithrar 92fa94b
fix code block
elithrar dbdf586
add next steps for context
elithrar 2685a76
fix link
elithrar e213140
update rules:
elithrar 01a7ab5
fix imports
elithrar f0ce2f5
fix imports
elithrar bbc8251
Apply suggestions from code review
elithrar d00b9f4
fix code block
elithrar f0b720a
fix
elithrar 2bc4fe7
fix
elithrar 8743135
Update rules-of-workflows.mdx
elithrar 5952eef
Update rules-of-workflows.mdx
elithrar 3fc8ae3
Update rules-of-workflows.mdx
elithrar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
142 changes: 142 additions & 0 deletions
142
src/content/docs/workflows/build/call-workflows-from-pages.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| --- | ||
| title: Call Workflows from Pages | ||
| pcx_content_type: concept | ||
| sidebar: | ||
| order: 11 | ||
| --- | ||
|
|
||
| import { WranglerConfig, TypeScriptExample } from "~/components"; | ||
|
|
||
| You can bind and trigger Workflows from [Pages Functions](/pages/functions/) by deploying a Workers project with your Workflow definition and then invoking that Worker using [service bindings](/pages/functions/bindings/#service-bindings) or a standard `fetch()` call. | ||
|
|
||
| :::note | ||
|
|
||
| You will need to deploy your Workflow as a standalone Workers project first before your Pages Function can call it. Visit the Workflows [get started guide](/workflows/get-started/guide/) if you have not yet deployed a Workflow. | ||
|
|
||
| ::: | ||
|
|
||
| ### Use Service Bindings | ||
|
|
||
| [Service Bindings](/workers/runtime-apis/bindings/service-bindings/) allow you to call a Worker from another Worker or a Pages Function without needing to expose it directly. | ||
|
|
||
| To do this, you will need to: | ||
|
|
||
| 1. Deploy your Workflow in a Worker | ||
| 2. Create a Service Binding to that Worker in your Pages project | ||
| 3. Call the Worker remotely using the binding | ||
|
|
||
| For example, if you have a Worker called `workflows-starter`, you would create a new Service Binding in your Pages project as follows, ensuring that the `service` name matches the name of the Worker your Workflow is defined in: | ||
|
|
||
| <WranglerConfig> | ||
| ```toml | ||
| services = [ | ||
| { binding = "WORKFLOW_SERVICE", service = "workflows-starter" } | ||
| ] | ||
| ``` | ||
| </WranglerConfig> | ||
|
|
||
| Your Worker can expose a specific method (or methods) that only other Workers or Pages Functions can call over the Service Binding. | ||
|
|
||
| In the following example, we expose a specific `createInstance` method that accepts our `Payload` and returns the [`InstanceStatus`](/workflows/build/workers-api/#instancestatus) from the Workflows API: | ||
|
|
||
| <TypeScriptExample filename="index.ts"> | ||
| ```ts | ||
| import { WorkerEntrypoint } from "cloudflare:workers"; | ||
elithrar marked this conversation as resolved.
Show resolved
Hide resolved
elithrar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| interface Env { | ||
| MY_WORKFLOW: Workflow; | ||
| } | ||
|
|
||
| type Payload = { | ||
| hello: string; | ||
| } | ||
|
|
||
| export default class WorkflowsService extends WorkerEntrypoint<Env> { | ||
| // Currently, entrypoints without a named handler are not supported | ||
| async fetch() { return new Response(null, {status: 404}); } | ||
|
|
||
| async createInstance(payload: Payload) { | ||
| let instance = await this.env.MY_WORKFLOW.create({ | ||
| params: payload | ||
| }); | ||
|
|
||
| return Response.json({ | ||
| id: instance.id, | ||
| details: await instance.status(), | ||
| }); | ||
| } | ||
| } | ||
| ``` | ||
| </TypeScriptExample> | ||
|
|
||
| Your Pages Function would resemble the following: | ||
|
|
||
| <TypeScriptExample filename="functions/request.ts"> | ||
| ```ts | ||
| interface Env { | ||
| WORKFLOW_SERVICE: Service; | ||
| } | ||
|
|
||
| export const onRequest: PagesFunction<Env> = async (context) => { | ||
| // This payload could be anything from within your app or from your frontend | ||
| let payload = {"hello": "world"} | ||
| return context.env.WORKFLOWS_SERVICE.createInstance(payload) | ||
| }; | ||
| ``` | ||
| </TypeScriptExample> | ||
|
|
||
| Visit the [bindings documentation for Pages Functions](/pages/functions/bindings/#service-bindings) to learn more about binding to resources from Pages Functions, including how to bind via the Cloudflare dashboard. | ||
elithrar marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
elithrar marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ### Using fetch | ||
|
|
||
| :::note "Service Bindings vs. fetch" | ||
|
|
||
| We recommend using [Service Bindings](/workers/runtime-apis/bindings/service-bindings/) when calling a Worker in your own account. | ||
|
|
||
| Service Bindings don't require you to expose a public endpoint from your Worker, don't require you to configure authentication, and allow you to call methods on your Worker directly, avoiding the overhead of managing HTTP requests and responses. | ||
|
|
||
| ::: | ||
|
|
||
| An alternative to setting up a Service Binding is to call the Worker over HTTP by using the Workflows [Workers API](/workflows/build/workers-api/#workflow) to `create` a new Workflow instance for each incoming HTTP call to the Worker: | ||
|
|
||
| <TypeScriptExample filename="index.ts"> | ||
| ```ts | ||
| // This is in the same file as your Workflow definition | ||
| export default { | ||
| async fetch(req: Request, env: Env): Promise<Response> { | ||
| let instance = await env.MY_WORKFLOW.create({ | ||
| params: payload | ||
| }); | ||
| return Response.json({ | ||
elithrar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| id: instance.id, | ||
| details: await instance.status(), | ||
| }); | ||
| }, | ||
| }; | ||
| ``` | ||
| </TypeScriptExample> | ||
|
|
||
| Your [Pages Function](/pages/functions/get-started/) can then make a regular `fetch` call to the Worker: | ||
|
|
||
| <TypeScriptExample filename="functions/request.ts"> | ||
| ```ts | ||
| export const onRequest: PagesFunction<Env> = async (context) => { | ||
| // Other code | ||
| let payload = {"hello": "world"} | ||
| const instanceStatus = await fetch("https://YOUR_WORKER.workers.dev/", { | ||
| method: "POST", | ||
| body: JSON.stringify(payload) // Send a payload for our Worker to pass to the Workflow | ||
| }) | ||
|
|
||
| return Response.json(instanceStatus); | ||
elithrar marked this conversation as resolved.
Show resolved
Hide resolved
elithrar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }; | ||
| ``` | ||
| </TypeScriptExample> | ||
|
|
||
| You can also choose to authenticate these requests by passing a shared secret in a header and validating that in your Worker. | ||
|
|
||
| ### Next steps | ||
|
|
||
| * Learn more about how to programatically call and trigger Workflows from the [Workers API](/workflows/build/workers-api/) | ||
| * Understand how to send [events and parameters](/workflows/build/events-and-parameters/) when triggering a Workflow | ||
| * Review the [Rules of Workflows](/workflows/build/rules-of-workflows/) and best practices for writing Workflows | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.