
# Intercepting Fetch Requests

Many providers support setting a custom `fetch` function using the `fetch` argument in their factory function.

A custom `fetch` function can be used to intercept and modify requests before they are sent to the provider's API,
and to intercept and modify responses before they are returned to the caller.

Use cases for intercepting requests include:

- Logging requests and responses
- Adding authentication headers
- Modifying request bodies
- Caching responses
- Using a custom HTTP client

## Example

```ts file='index.ts' highlight="5-13"
import { generateText, createGateway } from 'ai';

const gateway = createGateway({
  // example fetch wrapper that logs the input to the API call:
  fetch: async (url, options) => {
    console.log('URL', url);
    console.log('Headers', JSON.stringify(options!.headers, null, 2));
    console.log(
      `Body ${JSON.stringify(JSON.parse(options!.body! as string), null, 2)}`,
    );
    return await fetch(url, options);
  },
});

const { text } = await generateText({
  model: gateway('openai/gpt-4o'),
  prompt: 'Why is the sky blue?',
});
```


## Navigation

- [Generate Text](/v5/cookbook/node/generate-text)
- [Retrieval Augmented Generation](/v5/cookbook/node/retrieval-augmented-generation)
- [Knowledge Base Agent](/v5/cookbook/node/knowledge-base-agent)
- [Generate Text with Chat Prompt](/v5/cookbook/node/generate-text-with-chat-prompt)
- [Generate Text with Image Prompt](/v5/cookbook/node/generate-text-with-image-prompt)
- [Stream Text](/v5/cookbook/node/stream-text)
- [Stream Text with Chat Prompt](/v5/cookbook/node/stream-text-with-chat-prompt)
- [Stream Text with Image Prompt](/v5/cookbook/node/stream-text-with-image-prompt)
- [Stream Text with File Prompt](/v5/cookbook/node/stream-text-with-file-prompt)
- [Generate Object with a Reasoning Model](/v5/cookbook/node/generate-object-reasoning)
- [Generate Object](/v5/cookbook/node/generate-object)
- [Stream Object](/v5/cookbook/node/stream-object)
- [Stream Object with Image Prompt](/v5/cookbook/node/stream-object-with-image-prompt)
- [Record Token Usage After Streaming Object](/v5/cookbook/node/stream-object-record-token-usage)
- [Record Final Object after Streaming Object](/v5/cookbook/node/stream-object-record-final-object)
- [Call Tools](/v5/cookbook/node/call-tools)
- [Call Tools with Image Prompt](/v5/cookbook/node/call-tools-with-image-prompt)
- [Call Tools in Multiple Steps](/v5/cookbook/node/call-tools-multiple-steps)
- [Model Context Protocol (MCP) Tools](/v5/cookbook/node/mcp-tools)
- [Manual Agent Loop](/v5/cookbook/node/manual-agent-loop)
- [Web Search Agent](/v5/cookbook/node/web-search-agent)
- [Model Context Protocol (MCP) Elicitation](/v5/cookbook/node/mcp-elicitation)
- [Embed Text](/v5/cookbook/node/embed-text)
- [Embed Text in Batch](/v5/cookbook/node/embed-text-batch)
- [Intercepting Fetch Requests](/v5/cookbook/node/intercept-fetch-requests)
- [Local Caching Middleware](/v5/cookbook/node/local-caching-middleware)


[Full Sitemap](/sitemap.md)
