@genkit-ai/express

中文文档
1.44.0发布于 2026/11/04Apache-2.0TypeScriptESM + CJS近 30 天下载 2.1万

Genkit AI framework plugin for Express server

原始使用文档
文档目录6 节
  1. Installation
  2. Usage
  3. Durable Streaming (Beta)
  4. Initialization Data
  5. Contributing
  6. License

Genkit Express Plugin

This plugin provides utilities for conveninetly exposing Genkit flows and actions via Express HTTP server as REST APIs.

Building with a coding agent? Install the Genkit JS skill first.

npx skills add genkit-ai/skills --skill developing-genkit-js

It teaches your agent the current Genkit JS APIs and common gotchas. Source, manual install and skills for other languages: genkit-ai/skills.

See official documentation for more.

Installation

To use this plugin, install it in your project:

npm i @genkit-ai/express

Usage

import { expressHandler } from '@genkit-ai/express';
import express from 'express';

const simpleFlow = ai.defineFlow('simpleFlow', async (input, { sendChunk }) => {
  const { text } = await ai.generate({
    model: googleAI.model('gemini-flash-latest'),
    prompt: input,
    onChunk: (c) => sendChunk(c.text),
  });
  return text;
});

const app = express();
app.use(express.json());

app.post('/simpleFlow', expressHandler(simpleFlow));

app.listen(8080);

You can also handle auth using context providers:

import { UserFacingError } from 'genkit';
import { ContextProvider, RequestData } from 'genkit/context';

const context: ContextProvider<Context> = (req: RequestData) => {
  if (req.headers['authorization'] !== 'open sesame') {
    throw new UserFacingError('PERMISSION_DENIED', 'not authorized');
  }
  return {
    auth: {
      user: 'Ali Baba',
    },
  };
};

app.post(
  '/simpleFlow',
  authMiddleware,
  expressHandler(simpleFlow, { context })
);

Durable Streaming (Beta)

You can configure flows to use a StreamManager to persist their state. This allows clients to disconnect and reconnect to a stream without losing its state.

To enable durable streaming, provide a streamManager in the expressHandler options. The InMemoryStreamManager is useful for development and testing:

import { InMemoryStreamManager } from 'genkit/beta';

// ...

app.post(
  '/myDurableFlow',
  expressHandler(myFlow, {
    streamManager: new InMemoryStreamManager(),
  })
);

For production environments, you should use a durable StreamManager implementation, such as FirestoreStreamManager or RtdbStreamManager from the @genkit-ai/firebase plugin, or a custom implementation.

Clients can then connect and reconnect to the stream using the streamId:

// Start a new stream
const result = streamFlow({
  url: `http://localhost:8080/myDurableFlow`,
  input: 'tell me a long story',
});
const streamId = await result.streamId; // Save this ID

// ... later, reconnect if needed ...
const reconnectedResult = streamFlow({
  url: `http://localhost:8080/myDurableFlow`,
  streamId: streamId,
});

Flows and actions exposed using the expressHandler function can be accessed using genkit/beta/client library:

import { runFlow, streamFlow } from 'genkit/beta/client';

const result = await runFlow({
  url: `http://localhost:${port}/simpleFlow`,
  input: 'say hello',
});

console.log(result); // hello

// set auth headers (when using auth policies)
const result = await runFlow({
  url: `http://localhost:${port}/simpleFlow`,
  headers: {
    Authorization: 'open sesame',
  },
  input: 'say hello',
});

console.log(result); // hello

// and streamed
const result = streamFlow({
  url: `http://localhost:${port}/simpleFlow`,
  input: 'say hello',
});
for await (const chunk of result.stream) {
  console.log(chunk);
}
console.log(await result.output);

Initialization Data

If your flow or action accepts initialization data (defined via initSchema), you can pass it using the init option in the client:

const result = await runFlow({
  url: `http://localhost:${port}/myFlow`,
  input: 'say hello',
  init: { sessionId: 'abc123', config: { temperature: 0.7 } },
});

// Also works with streaming
const streamed = streamFlow({
  url: `http://localhost:${port}/myFlow`,
  input: 'say hello',
  init: { sessionId: 'abc123' },
});
for await (const chunk of streamed.stream) {
  console.log(chunk);
}
console.log(await streamed.output);

The init data is sent in the request body alongside data and is validated against the action's initSchema on the server side. If the init data does not conform to the initSchema, the request fails with a 400 INVALID_ARGUMENT error before the flow runs.

You can also use startFlowServer to quickly expose multiple flows and actions:

import { startFlowServer } from '@genkit-ai/express';
import { genkit } from 'genkit';

const ai = genkit({});

export const menuSuggestionFlow = ai.defineFlow(
  {
    name: 'menuSuggestionFlow',
  },
  async (restaurantTheme) => {
    // ...
  }
);

startFlowServer({
  flows: [menuSuggestionFlow],
});

You can also configure the server:

startFlowServer({
  flows: [menuSuggestionFlow],
  port: 4567,
  cors: {
    origin: '*',
  },
});

Contributing

The sources for this package are in the main Genkit repo. Please file issues and pull requests against that repo.

Usage information and reference details can be found in official Genkit documentation.

License

Licensed under the Apache 2.0 License.

版本与发布记录

@genkit-ai/express 共发布 112 个版本,首次发布于 2024/12/17;过去 12 个月发布了 40 个版本。

版本发布日期距今
1.44.0latest2026/11/0412 天前
1.43.02026/10/2225 天前
1.43.0-rc.3next2026/10/2225 天前
1.43.0-rc.22026/10/2126 天前
1.43.0-rc.12026/10/091 个月前
1.43.0-rc.02026/10/091 个月前

关于 @genkit-ai/express 的常见问题

@genkit-ai/express 是什么?

Genkit AI framework plugin for Express server

如何安装 @genkit-ai/express?

在项目目录运行 npm install @genkit-ai/express,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:genkit(^1.44.0)、express(^4.21.1 || ^5)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@genkit-ai/express 支持 TypeScript 吗?

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

@genkit-ai/express 支持 ESM 还是 CommonJS?

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

@genkit-ai/express 可以商用吗?

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

@genkit-ai/express 的下载量有多少?

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

@genkit-ai/express 还在维护吗?

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