@connectrpc/connect-next

中文文档
2.3.0发布于 2026/10/27Apache-2.0TypeScriptESM + CJS零依赖构建来源已验证近 30 天下载 35.2万

Connect is a family of libraries for building and consuming APIs on different languages and platforms, and @connectrpc/connect brings type-safe APIs with Protobuf to TypeScript.

原始使用文档
文档目录3 节
  1. nextJsApiRouter()
  2. Deploying to Vercel
  3. Getting started

@connectrpc/connect-next

Connect is a family of libraries for building and consuming APIs on different languages and platforms, and @connectrpc/connect brings type-safe APIs with Protobuf to TypeScript.

@connectrpc/connect-next provides a plugin for Next.js, the React Framework for the Web.

nextJsApiRouter()

Provide your Connect RPCs via Next.js API routes. To enable Connect in Next.js, add two files to your project:

.
├── connect.ts
└── pages
    └── api
        └── [[...connect]].ts

Note: Your Connect API routes need to be placed in pages/, but you can use the app/ directory for the App Router at the same time.

The new file connect.ts is where you register your RPCs:

// connect.ts
import { ConnectRouter } from "@connectrpc/connect";

export default function (router: ConnectRouter) {
  // implement rpc Say(SayRequest) returns (SayResponse)
  router.rpc(ElizaService, ElizaService.methods.say, async (req) => ({
    sentence: `you said: ${req.sentence}`,
  }));
}

pages/api/[[..connect]].ts is a Next.js catch-all API route:

// pages/api/[[..connect]].ts
import { nextJsApiRouter } from "@connectrpc/connect-next";
import { createValidateInterceptor } from "@connectrpc/validate";
import routes from "../../connect";

const { handler } = nextJsApiRouter({
  interceptors: [createValidateInterceptor()],
  routes,
});
export default handler;

// Required: Connect parses request bodies itself, so the Next.js body
// parser must be disabled. Without this, RPCs will fail.
export const config = {
  api: {
    bodyParser: false,
  },
};

With that server running, you can make requests with any Connect or gRPC-Web client. Note that Next.js serves all your RPCs with the /api prefix.

curl with the Connect protocol:

curl \
    --header "Content-Type: application/json" \
    --data '{"sentence": "I feel happy."}' \
    --http2-prior-knowledge \
    http://localhost:3000/api/connectrpc.eliza.v1.ElizaService/Say

Node.js with the gRPC-web protocol (using a transport from @connectrpc/connect-node):

import { createClient } from "@connectrpc/connect";
import { createGrpcWebTransport } from "@connectrpc/connect-node";
import { ElizaService } from "./gen/eliza_connect.js";

const transport = createGrpcWebTransport({
  baseUrl: "http://localhost:3000/api",
  httpVersion: "1.1",
});

const client = createClient(ElizaService, transport);
const { sentence } = await client.say({ sentence: "I feel happy." });
console.log(sentence); // you said: I feel happy.

A client for the web browser actually looks identical to this example - it would simply use createConnectTransport from @connectrpc/connect-web instead.

Note that support for gRPC is limited, since many gRPC clients require HTTP/2, and Next.js does not support the Node.js http2 module.

Deploying to Vercel

Currently, @connectrpc/connect-next does not support the Vercel Edge runtime. It requires the Node.js server runtime, which is used by default when deploying to Vercel.

Getting started

To get started with Connect, head over to the docs for a tutorial, or take a look at our example.

版本与发布记录

@connectrpc/connect-next 共发布 34 个版本,首次发布于 2023/08/21;过去 12 个月发布了 4 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
2.3.0latest2026/10/2714 天前
2.2.02026/09/072 个月前
2.1.22026/06/134 个月前
2.1.12025/11/1811 个月前
1.7.02025/09/081.2 年前
2.1.02025/08/251.2 年前

其他发布通道:next → 1.4.0-rc1,alpha → 2.0.0-alpha.1,beta → 2.0.0-beta.2,rc → 2.0.0-rc.3。

关于 @connectrpc/connect-next 的常见问题

@connectrpc/connect-next 是什么?

Connect is a family of libraries for building and consuming APIs on different languages and platforms, and @connectrpc/connect brings type-safe APIs with Protobuf to TypeScript.

如何安装 @connectrpc/connect-next?

在项目目录运行 npm install @connectrpc/connect-next,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:next(^15.0.2 || ^16.0.0)、@bufbuild/protobuf(^2.7.0)、@connectrpc/connect(2.3.0)、@connectrpc/connect-node(2.3.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@connectrpc/connect-next 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@connectrpc/connect-next 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@connectrpc/connect-next 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=22。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@connectrpc/connect-next 可以商用吗?

@connectrpc/connect-next 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@connectrpc/connect-next 的下载量有多少?

根据 npm 官方下载统计,@connectrpc/connect-next 在 2026/10/11 – 2026/11/09 这 30 天内被下载 352,016 次,日均约 11,734 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@connectrpc/connect-next 还在维护吗?

最新版本 2.3.0 发布于 2026/10/27(14 天前);至今共发布 34 个版本,其中过去 12 个月发布了 4 个;首次发布于 2023/08/21。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。