Call Workflows from Pages
You can bind and trigger Workflows from Pages Functions by deploying a Workers project with your Workflow definition and then invoking that Worker using service bindings or a standard fetch()
call.
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:
- Deploy your Workflow in a Worker
- Create a Service Binding to that Worker in your Pages project
- 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:
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
from the Workflows API:
Your Pages Function would resemble the following:
To learn more about binding to resources from Pages Functions, including how to bind via the Cloudflare dashboard, refer to the bindings documentation for Pages Functions.
:::note "Service Bindings vs. fetch"
We recommend using 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 to create
a new Workflow instance for each incoming HTTP call to the Worker:
Your Pages Function can then make a regular fetch
call to the Worker:
You can also choose to authenticate these requests by passing a shared secret in a header and validating that in your Worker.
- Learn more about how to programatically call and trigger Workflows from the Workers API
- Understand how to send events and parameters when triggering a Workflow
- Review the Rules of Workflows and best practices for writing Workflows