Skip to content

Commit b6cd4bb

Browse files
authored
workflows: doc type params for create
1 parent 6333d9b commit b6cd4bb

File tree

1 file changed

+43
-4
lines changed

1 file changed

+43
-4
lines changed

src/content/docs/workflows/build/workers-api.mdx

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,13 +171,16 @@ Create (trigger) a new instance of the given Workflow.
171171

172172
* <code>create(options?: WorkflowInstanceCreateOptions): Promise&lt;WorkflowInstance&gt;</code>
173173

174-
* `options` - optional properties to pass when creating an instance.
174+
* `options` - optional properties to pass when creating an instance, includng a user-provided ID and payload parameters.
175175

176-
An ID is automatically generated, but a user-provided ID can be specified (up to 64 characters [^1]). This can be useful when mapping Workflows to users, merchants or other identifiers in your system.
176+
An ID is automatically generated, but a user-provided ID can be specified (up to 64 characters [^1]). This can be useful when mapping Workflows to users, merchants or other identifiers in your system. You can also provide a JSON object as the `params` property, allowing you to pass data for the Workflow instance to act on as its [`WorkflowEvent`](/workflows/build/events-and-parameters/).
177177

178178
```ts
179-
// Create a new Workflow instance with your own ID:
180-
let instance = await env.MY_WORKFLOW.create({ id: myIdDefinedFromOtherSystem })
179+
// Create a new Workflow instance with your own ID and pass params to the Workflow instance
180+
let instance = await env.MY_WORKFLOW.create({
181+
id: myIdDefinedFromOtherSystem,
182+
params: { "hello": "world }
183+
})
181184
return Response.json({
182185
id: instance.id,
183186
details: await instance.status(),
@@ -186,6 +189,42 @@ return Response.json({
186189
187190
Returns a `WorkflowInstance`.
188191
192+
<Render file="workflows-type-parameters">
193+
194+
To provide an optional type parameter to the `Workflow`, pass a type argument with your type when defining your Workflow bindings:
195+
196+
```ts
197+
interface User {
198+
email: string;
199+
createdTimestamp: number;
200+
}
201+
202+
interface Env {
203+
// Pass our User type as the type parameter to the Workflow definition
204+
MY_WORKFLOW: Workflow<User>;
205+
}
206+
207+
export default {
208+
async fetch(request, env, ctx) {
209+
// More likely to come from your database or via the request body!
210+
const user: User = {
211+
email: user@example.com,
212+
createdTimestamp: Date.now()
213+
}
214+
215+
let instance = await env.MY_WORKFLOW.create({
216+
// params expects the type User
217+
params: user
218+
})
219+
220+
return Response.json({
221+
id: instance.id,
222+
details: await instance.status(),
223+
});
224+
}
225+
}
226+
```
227+
189228
### get
190229
191230
Get a specific Workflow instance by ID.

0 commit comments

Comments
 (0)