---
title: "Reference: networkRoute() | AI SDK"
description: API reference for networkRoute(), a function to create network route handlers for streaming network execution in AI SDK-compatible format.
packages:
  - "@mastra/ai-sdk"
  - "@mastra/core"
---

import PropertiesTable from "@site/src/components/PropertiesTable";

# networkRoute()

Creates a network route handler for streaming network execution using the AI SDK format. This function registers an HTTP `POST` endpoint that accepts messages, executes an agent network, and streams the response back to the client in AI SDK-compatible format. Agent networks allow a routing agent to delegate tasks to other agents. You have to use it inside a [custom API route](/docs/v1/server/custom-api-routes).

Use [`handleNetworkStream()`](/reference/v1/ai-sdk/handle-network-stream) if you need a framework-agnostic handler.

## Usage example

This example shows how to set up a network route at the `/network` endpoint that uses an agent with the ID `weatherAgent`.

```typescript title="src/mastra/index.ts"
import { Mastra } from "@mastra/core";
import { networkRoute } from "@mastra/ai-sdk";

export const mastra = new Mastra({
  server: {
    apiRoutes: [
      networkRoute({
        path: "/network",
        agent: "weatherAgent",
      }),
    ],
  },
});
```

You can also use dynamic agent routing based on an `agentId`. The URL `/network/weatherAgent` will resolve to the agent with the ID `weatherAgent`.

```typescript title="src/mastra/index.ts"
import { Mastra } from "@mastra/core";
import { networkRoute } from "@mastra/ai-sdk";

export const mastra = new Mastra({
  server: {
    apiRoutes: [
      networkRoute({
        path: "/network/:agentId",
      }),
    ],
  },
});
```

## Parameters

<PropertiesTable
  content={[
    {
      name: "path",
      type: "string",
      description: "The route path (e.g., `/network` or `/network/:agentId`). Include `:agentId` for dynamic agent routing.",
      isOptional: false,
      defaultValue: "'/network/:agentId'",
    },
    {
      name: "agent",
      type: "string",
      description: "The ID of the routing agent to use for this network route. Required if the path doesn't include `:agentId`.",
      isOptional: true,
    },
    {
      name: "defaultOptions",
      type: "AgentExecutionOptions",
      description: "Default options passed to agent execution. These can include instructions, memory configuration, maxSteps, and other execution settings.",
      isOptional: true,
    },
  ]}
/>

## Additional configuration

You can use [`prepareSendMessagesRequest`](https://ai-sdk.dev/docs/reference/ai-sdk-ui/use-chat#transport.default-chat-transport.prepare-send-messages-request) to customize the request sent to the network route, for example to pass additional configuration to the agent:

```typescript
const { error, status, sendMessage, messages, regenerate, stop } = useChat({
  transport: new DefaultChatTransport({
    api: "http://localhost:4111/network",
    prepareSendMessagesRequest({ messages }) {
      return {
        body: {
          messages,
          // Pass memory config
          memory: {
            thread: "user-1",
            resource: "user-1",
          },
        },
      };
    },
  }),
});
```