@upstash/workflow 1.4.0-rc → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (223) hide show
  1. package/README.md +9 -0
  2. package/astro.d.mts +18 -2
  3. package/astro.d.ts +18 -2
  4. package/astro.js +129 -29
  5. package/astro.mjs +1 -1
  6. package/{chunk-UH6O5IJY.mjs → chunk-J3PXPHSB.mjs} +129 -29
  7. package/cloudflare.d.mts +16 -2
  8. package/cloudflare.d.ts +16 -2
  9. package/cloudflare.js +129 -29
  10. package/cloudflare.mjs +1 -1
  11. package/docs/agents/examples.mdx +8 -0
  12. package/docs/agents/features.mdx +374 -0
  13. package/docs/agents/getting-started.mdx +139 -0
  14. package/docs/agents/overview.mdx +40 -0
  15. package/docs/agents/patterns/evaluator-optimizer.mdx +72 -0
  16. package/docs/agents/patterns/orchestrator-workers.mdx +95 -0
  17. package/docs/agents/patterns/parallelization.mdx +77 -0
  18. package/docs/agents/patterns/prompt-chaining.mdx +84 -0
  19. package/docs/basics/best-practices/at-least-one-step.mdx +57 -0
  20. package/docs/basics/best-practices/business-logic-in-steps.mdx +52 -0
  21. package/docs/basics/best-practices/deterministic-code.mdx +132 -0
  22. package/docs/basics/best-practices/idempotency.mdx +38 -0
  23. package/docs/basics/best-practices/nested-context-methods.mdx +38 -0
  24. package/docs/basics/best-practices/overview.mdx +35 -0
  25. package/docs/basics/best-practices/promise-any.mdx +9 -0
  26. package/docs/basics/best-practices/return-step-results.mdx +80 -0
  27. package/docs/basics/best-practices/workflow-limits.mdx +62 -0
  28. package/docs/basics/client/cancel.mdx +137 -0
  29. package/docs/basics/client/dlq/callback.mdx +21 -0
  30. package/docs/basics/client/dlq/delete.mdx +120 -0
  31. package/docs/basics/client/dlq/list.mdx +96 -0
  32. package/docs/basics/client/dlq/restart.mdx +164 -0
  33. package/docs/basics/client/dlq/resume.mdx +167 -0
  34. package/docs/basics/client/logs.mdx +139 -0
  35. package/docs/basics/client/notify.mdx +79 -0
  36. package/docs/basics/client/trigger.mdx +139 -0
  37. package/docs/basics/client/waiters.mdx +31 -0
  38. package/docs/basics/client.mdx +40 -0
  39. package/docs/basics/how.mdx +65 -0
  40. package/docs/basics/serve/advanced.mdx +435 -0
  41. package/docs/basics/serve.mdx +142 -0
  42. package/docs/changelog.mdx +174 -0
  43. package/docs/examples/allInOne.mdx +377 -0
  44. package/docs/examples/authWebhook.mdx +567 -0
  45. package/docs/examples/customRetry.mdx +309 -0
  46. package/docs/examples/customerOnboarding.mdx +230 -0
  47. package/docs/examples/dynamicWorkflow.mdx +203 -0
  48. package/docs/examples/eCommerceOrderFulfillment.mdx +281 -0
  49. package/docs/examples/imageProcessing.mdx +339 -0
  50. package/docs/examples/paymentRetry.mdx +438 -0
  51. package/docs/examples/waitForEvent.mdx +201 -0
  52. package/docs/features/dlq/callback.mdx +42 -0
  53. package/docs/features/dlq/delete.mdx +29 -0
  54. package/docs/features/dlq/restart.mdx +33 -0
  55. package/docs/features/dlq/resume.mdx +37 -0
  56. package/docs/features/dlq.mdx +39 -0
  57. package/docs/features/failure-callback.mdx +102 -0
  58. package/docs/features/failureFunction/advanced.mdx +36 -0
  59. package/docs/features/failureFunction/reliability.mdx +33 -0
  60. package/docs/features/flow-control/monitor.mdx +22 -0
  61. package/docs/features/flow-control/parallelism.mdx +45 -0
  62. package/docs/features/flow-control/rate-period.mdx +46 -0
  63. package/docs/features/flow-control.mdx +164 -0
  64. package/docs/features/invoke/serveMany.mdx +109 -0
  65. package/docs/features/invoke.mdx +39 -0
  66. package/docs/features/notify.mdx +127 -0
  67. package/docs/features/parallel-steps.mdx +41 -0
  68. package/docs/features/retries/prevent-retries.mdx +140 -0
  69. package/docs/features/retries.mdx +93 -0
  70. package/docs/features/wait-for-event.mdx +104 -0
  71. package/docs/features/wait.mdx +44 -0
  72. package/docs/features/webhooks.mdx +116 -0
  73. package/docs/getstarted.mdx +200 -0
  74. package/docs/howto/cancel.mdx +50 -0
  75. package/docs/howto/changes.mdx +33 -0
  76. package/docs/howto/configure.mdx +42 -0
  77. package/docs/howto/failures.mdx +185 -0
  78. package/docs/howto/get-token.mdx +18 -0
  79. package/docs/howto/local-development/development-server.mdx +130 -0
  80. package/docs/howto/local-development/local-tunnel.mdx +73 -0
  81. package/docs/howto/middlewares.mdx +263 -0
  82. package/docs/howto/migrations.mdx +335 -0
  83. package/docs/howto/multi-region.mdx +107 -0
  84. package/docs/howto/parallel-runs.mdx +52 -0
  85. package/docs/howto/readonly-token.mdx +19 -0
  86. package/docs/howto/realtime/basic.mdx +315 -0
  87. package/docs/howto/realtime/human-in-the-loop.mdx +454 -0
  88. package/docs/howto/redact-fields.mdx +61 -0
  89. package/docs/howto/retry-now.mdx +41 -0
  90. package/docs/howto/schedule.mdx +104 -0
  91. package/docs/howto/security.mdx +143 -0
  92. package/docs/howto/singleton.mdx +137 -0
  93. package/docs/howto/start.mdx +69 -0
  94. package/docs/howto/use-webhooks.mdx +256 -0
  95. package/docs/integrations/aisdk.mdx +316 -0
  96. package/docs/integrations/anthropic.mdx +51 -0
  97. package/docs/integrations/datadog.mdx +69 -0
  98. package/docs/integrations/openai.mdx +87 -0
  99. package/docs/integrations/prometheus.mdx +65 -0
  100. package/docs/integrations/resend.mdx +82 -0
  101. package/docs/llms-txt.mdx +4 -0
  102. package/docs/openapi.yaml +2944 -0
  103. package/docs/pricing.mdx +22 -0
  104. package/docs/quickstarts/astro.mdx +185 -0
  105. package/docs/quickstarts/cloudflare-workers.mdx +190 -0
  106. package/docs/quickstarts/express.mdx +190 -0
  107. package/docs/quickstarts/fastapi.mdx +282 -0
  108. package/docs/quickstarts/flask.mdx +282 -0
  109. package/docs/quickstarts/hono.mdx +221 -0
  110. package/docs/quickstarts/nextjs-fastapi.mdx +147 -0
  111. package/docs/quickstarts/nextjs-flask.mdx +147 -0
  112. package/docs/quickstarts/nuxt.mdx +169 -0
  113. package/docs/quickstarts/platforms.mdx +78 -0
  114. package/docs/quickstarts/solidjs.mdx +161 -0
  115. package/docs/quickstarts/svelte.mdx +170 -0
  116. package/docs/quickstarts/tanstack-start.mdx +205 -0
  117. package/docs/quickstarts/vercel-nextjs.mdx +233 -0
  118. package/docs/roadmap.mdx +8 -0
  119. package/docs/sdk/workflow-js.mdx +4 -0
  120. package/docs/sdk/workflow-py.mdx +4 -0
  121. package/docs/steps/api.mdx +64 -0
  122. package/docs/steps/call.mdx +183 -0
  123. package/docs/steps/cancel.mdx +25 -0
  124. package/docs/steps/createWebhook.mdx +40 -0
  125. package/docs/steps/invoke.mdx +100 -0
  126. package/docs/steps/notify.mdx +105 -0
  127. package/docs/steps/run.mdx +150 -0
  128. package/docs/steps/sleep.mdx +97 -0
  129. package/docs/steps/sleepUntil.mdx +88 -0
  130. package/docs/steps/waitForEvent.mdx +60 -0
  131. package/docs/steps/waitForWebhook.mdx +78 -0
  132. package/docs/steps.mdx +108 -0
  133. package/docs/troubleshooting/general.mdx +331 -0
  134. package/docs/troubleshooting/vercel.mdx +82 -0
  135. package/express.d.mts +18 -2
  136. package/express.d.ts +18 -2
  137. package/express.js +129 -29
  138. package/express.mjs +1 -1
  139. package/h3.d.mts +18 -2
  140. package/h3.d.ts +18 -2
  141. package/h3.js +129 -29
  142. package/h3.mjs +1 -1
  143. package/hono.d.mts +16 -2
  144. package/hono.d.ts +16 -2
  145. package/hono.js +129 -29
  146. package/hono.mjs +1 -1
  147. package/index.d.mts +49 -21
  148. package/index.d.ts +49 -21
  149. package/index.js +145 -41
  150. package/index.mjs +17 -13
  151. package/nextjs.d.mts +39 -2
  152. package/nextjs.d.ts +39 -2
  153. package/nextjs.js +129 -29
  154. package/nextjs.mjs +1 -1
  155. package/package.json +1 -1
  156. package/react-router.d.mts +15 -2
  157. package/react-router.d.ts +15 -2
  158. package/react-router.js +129 -29
  159. package/react-router.mjs +1 -1
  160. package/{serve-many-CbIV7145.d.mts → serve-many-BKukvLNl.d.mts} +1 -1
  161. package/{serve-many-BCtZg31b.d.ts → serve-many-CrLKQpuZ.d.ts} +1 -1
  162. package/solidjs.d.mts +9 -1
  163. package/solidjs.d.ts +9 -1
  164. package/solidjs.js +129 -29
  165. package/solidjs.mjs +1 -1
  166. package/src/platforms/astro.ts +84 -0
  167. package/src/platforms/cloudflare.ts +127 -0
  168. package/src/platforms/express.ts +147 -0
  169. package/src/platforms/h3.ts +105 -0
  170. package/src/platforms/hono.ts +103 -0
  171. package/src/platforms/nextjs.ts +193 -0
  172. package/src/platforms/react-router.ts +92 -0
  173. package/src/platforms/solidjs.ts +59 -0
  174. package/src/platforms/svelte.ts +99 -0
  175. package/src/platforms/tanstack.ts +96 -0
  176. package/src/src/client/dlq.ts +387 -0
  177. package/src/src/client/filter-types.ts +166 -0
  178. package/src/src/client/index.ts +350 -0
  179. package/src/src/client/types.ts +501 -0
  180. package/src/src/client/utils.ts +307 -0
  181. package/src/src/constants.ts +82 -0
  182. package/src/src/context/api/anthropic.ts +58 -0
  183. package/src/src/context/api/base.ts +52 -0
  184. package/src/src/context/api/index.ts +24 -0
  185. package/src/src/context/api/openai.ts +125 -0
  186. package/src/src/context/api/resend.ts +57 -0
  187. package/src/src/context/auto-executor.ts +707 -0
  188. package/src/src/context/context.ts +681 -0
  189. package/src/src/context/index.ts +1 -0
  190. package/src/src/context/provider.ts +37 -0
  191. package/src/src/context/steps.ts +965 -0
  192. package/src/src/error.ts +203 -0
  193. package/src/src/index.ts +13 -0
  194. package/src/src/middleware/default-callbacks.ts +20 -0
  195. package/src/src/middleware/index.ts +2 -0
  196. package/src/src/middleware/logging.ts +39 -0
  197. package/src/src/middleware/manager.ts +148 -0
  198. package/src/src/middleware/middleware.ts +46 -0
  199. package/src/src/middleware/types.ts +72 -0
  200. package/src/src/qstash/headers.ts +397 -0
  201. package/src/src/qstash/step-config.ts +268 -0
  202. package/src/src/qstash/submit-steps.ts +284 -0
  203. package/src/src/serve/authorization.ts +118 -0
  204. package/src/src/serve/index.ts +475 -0
  205. package/src/src/serve/multi-region/handlers.ts +225 -0
  206. package/src/src/serve/multi-region/utils.ts +88 -0
  207. package/src/src/serve/options.ts +241 -0
  208. package/src/src/serve/serve-many.ts +117 -0
  209. package/src/src/serve/utils.ts +11 -0
  210. package/src/src/types.ts +590 -0
  211. package/src/src/utils.ts +141 -0
  212. package/src/src/workflow-parser.ts +472 -0
  213. package/src/src/workflow-requests.ts +575 -0
  214. package/svelte.d.mts +16 -2
  215. package/svelte.d.ts +16 -2
  216. package/svelte.js +129 -29
  217. package/svelte.mjs +1 -1
  218. package/tanstack.d.mts +14 -2
  219. package/tanstack.d.ts +14 -2
  220. package/tanstack.js +129 -29
  221. package/tanstack.mjs +1 -1
  222. package/{types-ffLPVG5_.d.ts → types-CrgK5P6p.d.mts} +124 -24
  223. package/{types-ffLPVG5_.d.mts → types-CrgK5P6p.d.ts} +124 -24
@@ -0,0 +1,567 @@
1
+ ---
2
+ title: "Auth Provider Webhook"
3
+ ---
4
+
5
+ This example demonstrates an authentication provider webhook process using Upstash Workflow.
6
+ The workflow handles the user creation, trial management, email reminders and notifications.
7
+
8
+ ## Use Case
9
+
10
+ Our workflow will:
11
+
12
+ 1. Receive a webhook event from an authentication provider (e.g. Firebase, Auth0, Clerk etc.)
13
+ 2. Create a new user in our database
14
+ 3. Create a new user in Stripe
15
+ 4. Start a trial in Stripe
16
+ 5. Send a welcome email
17
+ 6. Send a reminder email if the user hasn't solved any questions in the last 7 days
18
+ 7. Send a trial warning email if the user hasn't upgraded 2 days before the trial ends
19
+ 8. Send a trial ended email if the user hasn't upgraded
20
+
21
+ ## Code Example
22
+
23
+ <CodeGroup>
24
+
25
+ ```typescript api/workflow/route.ts
26
+ import { serve } from "@upstash/workflow/nextjs";
27
+ import { WorkflowContext } from '@upstash/qstash/workflow'
28
+
29
+ /**
30
+ * This can be the payload of the user created webhook event coming from your
31
+ * auth provider (e.g. Firebase, Auth0, Clerk etc.)
32
+ */
33
+ type UserCreatedPayload = {
34
+ name: string;
35
+ email: string;
36
+ };
37
+
38
+ export const { POST } = serve<UserCreatedPayload>(async (context) => {
39
+ const { name, email } = context.requestPayload;
40
+
41
+ const { userid } = await context.run("sync user", async () => {
42
+ return await createUserInDatabase({ name, email });
43
+ });
44
+
45
+ await context.run("create new user in stripe", async () => {
46
+ await createNewUserInStripe(email);
47
+ });
48
+
49
+ await context.run("start trial in Stripe", async () => {
50
+ await startTrialInStripe(email);
51
+ });
52
+
53
+ await context.run("send welcome email", async () => {
54
+ await sendEmail(
55
+ email,
56
+ "Welcome to our platform!, You have 14 days of free trial."
57
+ );
58
+ });
59
+
60
+ await context.sleep("wait", 7 * 24 * 60 * 60);
61
+
62
+ // get user stats and send email with them
63
+ const stats = await context.run("get user stats", async () => {
64
+ return await getUserStats(userid);
65
+ });
66
+ await sendProblemSolvedEmail({context, email, stats});
67
+
68
+ // wait until there are two days to the end of trial period
69
+ // and check upgrade status
70
+ await context.sleep("wait for trial warning", 5 * 24 * 60 * 60);
71
+ const isUpgraded = await context.run("check upgraded plan", async () => {
72
+ return await checkUpgradedPlan(email);
73
+ });
74
+
75
+ // end the workflow if upgraded
76
+ if (isUpgraded) return;
77
+
78
+ await context.run("send trial warning email", async () => {
79
+ await sendEmail(
80
+ email,
81
+ "Your trial is about to end in 2 days. Please upgrade your plan to keep using our platform."
82
+ );
83
+ });
84
+
85
+ await context.sleep("wait for trial end", 2 * 24 * 60 * 60);
86
+
87
+ await context.run("send trial end email", async () => {
88
+ await sendEmail(
89
+ email,
90
+ "Your trial has ended. Please upgrade your plan to keep using our platform."
91
+ );
92
+ });
93
+ });
94
+
95
+ async function sendProblemSolvedEmail({
96
+ context: WorkflowContext<UserCreatedPayload>
97
+ email: string,
98
+ stats: { totalProblemsSolved: number }
99
+ }) {
100
+ if (stats.totalProblemsSolved === 0) {
101
+ await context.run("send no answers email", async () => {
102
+ await sendEmail(
103
+ email,
104
+ "Hey, you haven't solved any questions in the last 7 days..."
105
+ );
106
+ });
107
+ } else {
108
+ await context.run("send stats email", async () => {
109
+ await sendEmail(
110
+ email,
111
+ `You have solved ${stats.totalProblemsSolved} problems in the last 7 days. Keep it up!`
112
+ );
113
+ });
114
+ }
115
+ }
116
+
117
+ async function createUserInDatabase({
118
+ name,
119
+ email,
120
+ }: {
121
+ name: string;
122
+ email: string;
123
+ }) {
124
+ console.log("Creating a user in the database:", name, email);
125
+ return { userid: "12345" };
126
+ }
127
+
128
+ async function createNewUserInStripe(email: string) {
129
+ // Implement logic to create a new user in Stripe
130
+ console.log("Creating a user in Stripe for", email);
131
+ }
132
+
133
+ async function startTrialInStripe(email: string) {
134
+ // Implement logic to start a trial in Stripe
135
+ console.log("Starting a trial of 14 days in Stripe for", email);
136
+ }
137
+
138
+ async function getUserStats(userid: string) {
139
+ // Implement logic to get user stats
140
+ console.log("Getting user stats for", userid);
141
+ return {
142
+ totalProblemsSolved: 10_000,
143
+ mostInterestedTopic: "JavaScript",
144
+ };
145
+ }
146
+
147
+ async function checkUpgradedPlan(email: string) {
148
+ // Implement logic to check if the user has upgraded the plan
149
+ console.log("Checking if the user has upgraded the plan", email);
150
+ return false;
151
+ }
152
+
153
+ async function sendEmail(email: string, content: string) {
154
+ // Implement logic to send an email
155
+ console.log("Sending email to", email, content);
156
+ }
157
+ ```
158
+
159
+ ```python main.py
160
+ from fastapi import FastAPI
161
+ from typing import Dict, TypedDict
162
+ from upstash_workflow.fastapi import Serve
163
+ from upstash_workflow import AsyncWorkflowContext
164
+
165
+ app = FastAPI()
166
+ serve = Serve(app)
167
+
168
+
169
+ class UserCreatedPayload(TypedDict):
170
+ name: str
171
+ email: str
172
+
173
+
174
+ class UserStats(TypedDict):
175
+ total_problems_solved: int
176
+ most_interested_topic: str
177
+
178
+
179
+ async def create_user_in_database(name: str, email: str) -> Dict[str, str]:
180
+ print("Creating a user in the database:", name, email)
181
+ return {"userid": "12345"}
182
+
183
+
184
+ async def create_new_user_in_stripe(email: str) -> None:
185
+ # Implement logic to create a new user in Stripe
186
+ print("Creating a user in Stripe for", email)
187
+
188
+
189
+ async def start_trial_in_stripe(email: str) -> None:
190
+ # Implement logic to start a trial in Stripe
191
+ print("Starting a trial of 14 days in Stripe for", email)
192
+
193
+
194
+ async def get_user_stats(userid: str) -> UserStats:
195
+ # Implement logic to get user stats
196
+ print("Getting user stats for", userid)
197
+ return {"total_problems_solved": 10000, "most_interested_topic": "Python"}
198
+
199
+
200
+ async def check_upgraded_plan(email: str) -> bool:
201
+ # Implement logic to check if the user has upgraded the plan
202
+ print("Checking if the user has upgraded the plan", email)
203
+ return False
204
+
205
+
206
+ async def send_email(email: str, content: str) -> None:
207
+ # Implement logic to send an email
208
+ print("Sending email to", email, content)
209
+
210
+
211
+ async def send_problem_solved_email(
212
+ context: AsyncWorkflowContext[UserCreatedPayload], email: str, stats: UserStats
213
+ ) -> None:
214
+ if stats["total_problems_solved"] == 0:
215
+
216
+ async def _send_no_answers_email() -> None:
217
+ await send_email(
218
+ email, "Hey, you haven't solved any questions in the last 7 days..."
219
+ )
220
+
221
+ await context.run("send no answers email", _send_no_answers_email)
222
+ else:
223
+
224
+ async def _send_stats_email() -> None:
225
+ await send_email(
226
+ email,
227
+ f"You have solved {stats['total_problems_solved']} problems in the last 7 days. Keep it up!",
228
+ )
229
+
230
+ await context.run("send stats email", _send_stats_email)
231
+
232
+
233
+ @serve.post("/auth-provider-webhook")
234
+ async def auth_provider_webhook(
235
+ context: AsyncWorkflowContext[UserCreatedPayload],
236
+ ) -> None:
237
+ payload = context.request_payload
238
+ name = payload["name"]
239
+ email = payload["email"]
240
+
241
+ async def _sync_user() -> str:
242
+ return await create_user_in_database(name, email)
243
+
244
+ result = await context.run("sync user", _sync_user)
245
+ userid = result["userid"]
246
+
247
+ async def _create_new_user_in_stripe() -> None:
248
+ await create_new_user_in_stripe(email)
249
+
250
+ await context.run("create new user in stripe", _create_new_user_in_stripe)
251
+
252
+ async def _start_trial_in_stripe() -> None:
253
+ await start_trial_in_stripe(email)
254
+
255
+ await context.run("start trial in Stripe", _start_trial_in_stripe)
256
+
257
+ async def _send_welcome_email() -> None:
258
+ await send_email(
259
+ email, "Welcome to our platform!, You have 14 days of free trial."
260
+ )
261
+
262
+ await context.run("send welcome email", _send_welcome_email)
263
+
264
+ await context.sleep("wait", 7 * 24 * 60 * 60)
265
+
266
+ # get user stats and send email with them
267
+
268
+ async def _get_user_stats() -> UserStats:
269
+ return await get_user_stats(userid)
270
+
271
+ stats: UserStats = await context.run("get user stats", _get_user_stats)
272
+
273
+ await send_problem_solved_email(context, email, stats)
274
+
275
+ # wait until there are two days to the end of trial period and check upgrade status
276
+ await context.sleep("wait for trial warning", 5 * 24 * 60 * 60)
277
+
278
+ async def _check_upgraded_plan() -> bool:
279
+ return await check_upgraded_plan(email)
280
+
281
+ is_upgraded = await context.run("check upgraded plan", _check_upgraded_plan)
282
+
283
+ # end the workflow if upgraded
284
+ if is_upgraded:
285
+ return
286
+
287
+ async def _send_trial_warning_email() -> None:
288
+ await send_email(
289
+ email,
290
+ "Your trial is about to end in 2 days. Please upgrade your plan to keep using our platform.",
291
+ )
292
+
293
+ await context.run("send trial warning email", _send_trial_warning_email)
294
+
295
+ await context.sleep("wait for trial end", 2 * 24 * 60 * 60)
296
+
297
+ async def _send_trial_end_email() -> None:
298
+ await send_email(
299
+ email,
300
+ "Your trial has ended. Please upgrade your plan to keep using our platform.",
301
+ )
302
+
303
+ await context.run("send trial end email", _send_trial_end_email)
304
+
305
+ ```
306
+
307
+ </CodeGroup>
308
+
309
+ ## Code Breakdown
310
+
311
+ ### 1. Sync User
312
+
313
+ We start by creating a new user in our database:
314
+
315
+ <CodeGroup>
316
+
317
+ ```typescript TypeScript
318
+ const { userid } = await context.run("sync user", async () => {
319
+ return await createUserInDatabase({ name, email });
320
+ });
321
+ ```
322
+
323
+ ```python Python
324
+ async def _sync_user() -> str:
325
+ return await create_user_in_database(name, email)
326
+
327
+ result = await context.run("sync user", _sync_user)
328
+ userid = result["userid"]
329
+
330
+ ```
331
+
332
+ </CodeGroup>
333
+
334
+ ### 2. Create New User in Stripe
335
+
336
+ Next, we create a new user in Stripe:
337
+
338
+ <CodeGroup>
339
+
340
+ ```typescript TypeScript
341
+ await context.run("create new user in stripe", async () => {
342
+ await createNewUserInStripe(email);
343
+ });
344
+ ```
345
+
346
+ ```python Python
347
+ async def _create_new_user_in_stripe() -> None:
348
+ await create_new_user_in_stripe(email)
349
+
350
+ await context.run("create new user in stripe", _create_new_user_in_stripe)
351
+
352
+ ```
353
+
354
+ </CodeGroup>
355
+
356
+ ### 3. Start Trial in Stripe
357
+
358
+ We start a trial in Stripe:
359
+
360
+ <CodeGroup>
361
+
362
+ ```typescript TypeScript
363
+ await context.run("start trial in Stripe", async () => {
364
+ await startTrialInStripe(email);
365
+ });
366
+ ```
367
+
368
+ ```python Python
369
+ async def _start_trial_in_stripe() -> None:
370
+ await start_trial_in_stripe(email)
371
+
372
+ await context.run("start trial in Stripe", _start_trial_in_stripe)
373
+
374
+ ```
375
+
376
+ </CodeGroup>
377
+
378
+ ### 4. Send Welcome Email
379
+
380
+ We send a welcome email to the user:
381
+
382
+ <CodeGroup>
383
+
384
+ ```typescript TypeScript
385
+ await context.run("send welcome email", async () => {
386
+ await sendEmail(
387
+ email,
388
+ "Welcome to our platform!, You have 14 days of free trial."
389
+ );
390
+ });
391
+ ```
392
+
393
+ ```python Python
394
+ async def _send_welcome_email() -> None:
395
+ await send_email(
396
+ email, "Welcome to our platform!, You have 14 days of free trial."
397
+ )
398
+
399
+ await context.run("send welcome email", _send_welcome_email)
400
+
401
+ ```
402
+
403
+ </CodeGroup>
404
+
405
+ ### 5. Send Reminder Email
406
+
407
+ After 7 days, we check if the user has solved any questions. If not, we send a reminder email:
408
+
409
+ <CodeGroup>
410
+
411
+ ```typescript TypeScript
412
+ await context.sleep("wait", 7 * 24 * 60 * 60);
413
+
414
+ const stats = await context.run("get user stats", async () => {
415
+ return await getUserStats(userid);
416
+ });
417
+ await sendProblemSolvedEmail({context, email, stats});
418
+ ```
419
+
420
+ ```python Python
421
+ await context.sleep("wait", 7 * 24 * 60 * 60)
422
+
423
+ async def _get_user_stats() -> UserStats:
424
+ return await get_user_stats(userid)
425
+
426
+ stats: UserStats = await context.run("get user stats", _get_user_stats)
427
+
428
+ await send_problem_solved_email(context, email, stats)
429
+
430
+ ```
431
+
432
+ </CodeGroup>
433
+
434
+ The `sendProblemSolvedEmail` method:
435
+
436
+ <CodeGroup>
437
+
438
+ ```typescript TypeScript
439
+ async function sendProblemSolvedEmail({
440
+ context: WorkflowContext<UserCreatedPayload>
441
+ email: string,
442
+ stats: { totalProblemsSolved: number }
443
+ }) {
444
+ if (stats.totalProblemsSolved === 0) {
445
+ await context.run("send no answers email", async () => {
446
+ await sendEmail(
447
+ email,
448
+ "Hey, you haven't solved any questions in the last 7 days..."
449
+ );
450
+ });
451
+ } else {
452
+ await context.run("send stats email", async () => {
453
+ await sendEmail(
454
+ email,
455
+ `You have solved ${stats.totalProblemsSolved} problems in the last 7 days. Keep it up!`
456
+ );
457
+ });
458
+ }
459
+ }
460
+ ```
461
+
462
+ ```python Python
463
+ async def send_problem_solved_email(
464
+ context: AsyncWorkflowContext[UserCreatedPayload], email: str, stats: UserStats
465
+ ) -> None:
466
+ if stats["total_problems_solved"] == 0:
467
+
468
+ async def _send_no_answers_email() -> None:
469
+ await send_email(
470
+ email, "Hey, you haven't solved any questions in the last 7 days..."
471
+ )
472
+
473
+ await context.run("send no answers email", _send_no_answers_email)
474
+ else:
475
+
476
+ async def _send_stats_email() -> None:
477
+ await send_email(
478
+ email,
479
+ f"You have solved {stats['total_problems_solved']} problems in the last 7 days. Keep it up!",
480
+ )
481
+
482
+ await context.run("send stats email", _send_stats_email)
483
+
484
+ ```
485
+
486
+ </CodeGroup>
487
+
488
+ ### 6. Send Trial Warning Email
489
+
490
+ If the user hasn't upgraded 2 days before the trial ends, we send a trial warning email:
491
+
492
+ <CodeGroup>
493
+
494
+ ```typescript TypeScript
495
+ await context.sleep("wait for trial warning", 5 * 24 * 60 * 60);
496
+
497
+ const isUpgraded = await context.run("check upgraded plan", async () => {
498
+ return await checkUpgradedPlan(email);
499
+ });
500
+
501
+ if (isUpgraded) return;
502
+
503
+ await context.run("send trial warning email", async () => {
504
+ await sendEmail(
505
+ email,
506
+ "Your trial is about to end in 2 days. Please upgrade your plan to keep using our platform."
507
+ );
508
+ });
509
+ ```
510
+
511
+ ```python Python
512
+ await context.sleep("wait for trial warning", 5 * 24 * 60 * 60)
513
+
514
+ async def _check_upgraded_plan() -> bool:
515
+ return await check_upgraded_plan(email)
516
+
517
+ is_upgraded = await context.run("check upgraded plan", _check_upgraded_plan)
518
+
519
+ # end the workflow if upgraded
520
+ if is_upgraded:
521
+ return
522
+
523
+ async def _send_trial_warning_email() -> None:
524
+ await send_email(
525
+ email,
526
+ "Your trial is about to end in 2 days. Please upgrade your plan to keep using our platform.",
527
+ )
528
+
529
+ await context.run("send trial warning email", _send_trial_warning_email)
530
+
531
+ ```
532
+
533
+ </CodeGroup>
534
+
535
+ If they upgraded, we end the workflow by returning.
536
+
537
+ ### 7. Send Trial Ended Email
538
+
539
+ If the user hasn't upgraded after the trial ends, we send a trial ended email:
540
+
541
+ <CodeGroup>
542
+
543
+ ```typescript TypeScript
544
+ await context.sleep("wait for trial end", 2 * 24 * 60 * 60);
545
+
546
+ await context.run("send trial end email", async () => {
547
+ await sendEmail(
548
+ email,
549
+ "Your trial has ended. Please upgrade your plan to keep using our platform."
550
+ );
551
+ });
552
+ ```
553
+
554
+ ```python Python
555
+ await context.sleep("wait for trial end", 2 * 24 * 60 * 60)
556
+
557
+ async def _send_trial_end_email() -> None:
558
+ await send_email(
559
+ email,
560
+ "Your trial has ended. Please upgrade your plan to keep using our platform.",
561
+ )
562
+
563
+ await context.run("send trial end email", _send_trial_end_email)
564
+
565
+ ```
566
+
567
+ </CodeGroup>