UNPKG

workflow

Version:

Workflow SDK - Build durable, resilient, and observable workflows

117 lines (81 loc) • 4 kB
--- title: fetch-in-workflow description: Use the workflow fetch step function instead of global fetch in workflows. type: troubleshooting summary: Resolve the fetch-in-workflow error by using the workflow fetch function. prerequisites: - /docs/foundations/workflows-and-steps related: - /docs/api-reference/workflow/fetch --- <CopyPrompt text="Fix `fetch` usage inside workflow functions. Search workflow files for direct global `fetch(...)` calls and libraries such as AI SDK calls that use fetch. For basic HTTP calls inside a `&quot;use workflow&quot;` function, import `{ fetch }` from `workflow` and replace the global call. For SDK/client calls that need normal Node.js or provider behavior, move the call into a helper function with `&quot;use step&quot;` and call that step from the workflow. Keep all step inputs and outputs serializable. Verify the workflow starts and replays without the fetch-in-workflow error." /> This error occurs when you try to use `fetch()` directly in a workflow function, or when a library (like the AI SDK) tries to call `fetch()` under the hood. ## Error message ```text Global "fetch" is unavailable in workflow functions. Use the "fetch" step function from "workflow" to make HTTP requests. ``` ## Why this happens Workflow functions run in a sandboxed environment without direct access to `fetch()`. Many libraries make HTTP requests under the hood. For example, the AI SDK's `generateText()` function calls `fetch()` to make HTTP requests to AI providers. When these libraries run inside a workflow function, they fail because the global `fetch` is not available. ## Quick fix Import the `fetch` step function from the `workflow` package and assign it to `globalThis.fetch` inside your workflow function. This version of `fetch` is a step function that wraps the standard `fetch` API, automatically handling serialization and providing retry capabilities. This will also make `fetch()` available to all functions and libraries in the current workflow function. **Before:** ```typescript lineNumbers title="workflows/ai.ts" import { generateText } from "ai"; export async function chatWorkflow(prompt: string) { "use workflow"; // Error - generateText() calls fetch() under the hood const result = await generateText({ // [!code highlight] model: "spacexai/grok-4.6", // [!code highlight] prompt, // [!code highlight] }); // [!code highlight] return result.text; } ``` **After:** ```typescript lineNumbers title="workflows/ai.ts" import { generateText } from "ai"; import { fetch } from "workflow"; // [!code highlight] export async function chatWorkflow(prompt: string) { "use workflow"; globalThis.fetch = fetch; // [!code highlight] // Now generateText() can make HTTP requests via the fetch step const result = await generateText({ model: "spacexai/grok-4.6", prompt, }); return result.text; } ``` ## Common scenarios ### AI SDK integration This is the most common scenario - using AI SDK functions that make HTTP requests: ```typescript lineNumbers import { generateText, streamText } from "ai"; import { fetch } from "workflow"; // [!code highlight] export async function aiWorkflow(userMessage: string) { "use workflow"; globalThis.fetch = fetch; // [!code highlight] // generateText makes an HTTP request under the hood const response = await generateText({ model: "spacexai/grok-4.6", prompt: userMessage, }); return response.text; } ``` ### Direct API calls You can also use the fetch step function directly for your own HTTP requests: ```typescript lineNumbers import { fetch } from "workflow"; export async function dataWorkflow() { "use workflow"; // Use fetch directly for HTTP requests const response = await fetch("https://api.example.com/data"); // [!code highlight] const data = await response.json(); return data; } ``` For more details on the `fetch` step function, see the [fetch API reference](/docs/api-reference/workflow/fetch).