惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
SegmentFault 最新的问题
Jina AI
Jina AI
罗磊的独立博客
V
Visual Studio Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
J
Java Code Geeks
U
Unit 42
Microsoft Azure Blog
Microsoft Azure Blog
B
Blog RSS Feed
爱范儿
爱范儿
酷 壳 – CoolShell
酷 壳 – CoolShell
Last Week in AI
Last Week in AI
T
The Blog of Author Tim Ferriss
腾讯CDC
Hugging Face - Blog
Hugging Face - Blog
T
Tailwind CSS Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
I
InfoQ
月光博客
月光博客
博客园_首页
Vercel News
Vercel News
P
Proofpoint News Feed
GbyAI
GbyAI
Y
Y Combinator Blog

Workflow SDK Documentation

Patterns for Defining Tools Human-in-the-Loop Building Durable AI Agents Queueing User Messages Resumable Streams Sleep, Suspense, and Scheduling Streaming Updates from Tools API Reference Workflow Globals Changelog Resilient run start Cookbook Building a World Deploying Astro Express Fastify Hono Getting Started NestJS Next.js Nitro Nuxt Python SvelteKit Vite corrupted-event-log fetch-in-workflow hook-conflict Errors
fetch
2026-05-31 · via Workflow SDK Documentation

Make HTTP requests from workflows with automatic serialization and retry semantics.

Makes HTTP requests from within a workflow. This is a special step function that wraps the standard fetch API, automatically handling serialization and providing retry semantics.

This is useful when you need to call external APIs or services from within your workflow.

fetch is a special type of step function provided and should be called directly inside workflow functions.

import { fetch } from "workflow"

async function apiWorkflow() {
    "use workflow"

    // Fetch data from an API
    const response = await fetch("https://api.example.com/data") 
    return await response.json()
}

Parameters

Accepts the same arguments as web fetch

NameTypeDescription
args[input: string | URL | Request, init?: RequestInit | undefined]

Returns

Returns the same response as web fetch

Basic Usage

Here's a simple example of how you can use fetch inside your workflow.

import { fetch } from "workflow"

async function apiWorkflow() {
    "use workflow"

    // Fetch data from an API
    const response = await fetch("https://api.example.com/data") 
    const data = await response.json()

    // Make a POST request
    const postResponse = await fetch("https://api.example.com/create", { 
        method: "POST",
        headers: {
            "Content-Type": "application/json"
        },
        body: JSON.stringify({ name: "test" })
    })

    return data
}

We call fetch() with a URL and optional request options, just like the standard fetch API. The workflow runtime automatically handles the response serialization.

This API is provided as a convenience to easily use fetch in workflow, but often, you might want to extend and implement your own fetch for more powerful error handing and retry logic.

Customizing Fetch Behavior

Here's an example of a custom fetch wrapper that provides more sophisticated error handling with custom retry logic:

import { FatalError, RetryableError } from "workflow"

export async function customFetch(
    url: string,
    init?: RequestInit
) {
    "use step"

    const response = await fetch(url, init)

    // Handle client errors (4xx) - don't retry
    if (response.status >= 400 && response.status < 500) {
        if (response.status === 429) {
            // Rate limited - retry with backoff from Retry-After header
            const retryAfter = response.headers.get("Retry-After")

            if (retryAfter) {
                // The Retry-After header is either a number (seconds) or an RFC 7231 date string
                const retryAfterValue = /^\d+$/.test(retryAfter)
                    ? parseInt(retryAfter) * 1000  // Convert seconds to milliseconds
                    : new Date(retryAfter);        // Parse RFC 7231 date format

                // Use `RetryableError` to customize the retry
                throw new RetryableError( 
                    `Rate limited by ${url}`, 
                    { retryAfter: retryAfterValue } 
                ) 
            }
        }

        // Other client errors are fatal (400, 401, 403, 404, etc.)
        throw new FatalError( 
            `Client error ${response.status}: ${response.statusText}`
        ) 
    }

    // Handle server errors (5xx) - will retry automatically
    if (!response.ok) {
        throw new Error(
            `Server error ${response.status}: ${response.statusText}`
        )
    }

    return response
}

This example demonstrates:

  • Setting custom maxRetries to 5 retries (6 total attempts including the initial attempt).
  • Throwing FatalError for client errors (400-499) to prevent retries.
  • Handling 429 rate limiting by reading the Retry-After header and using RetryableError.
  • Allowing automatic retries for server errors (5xx).