@datastax/langflow-client

中文文档
0.4.1发布于 2025/09/01Apache-2.0TypeScriptES Module近 30 天下载 4530

A JavaScript client for the Langflow API

原始使用文档
文档目录16 节
  1. Installation
  2. With npm
  3. With yarn
  4. With pnpm
  5. Prerequisites
  6. Configuration
  7. DataStax Langflow
  8. Open-source Langflow
  9. Usage
  10. Initialization
  11. Running a flow
  12. Streaming
  13. File upload
  14. Logs
  15. Aborting requests
  16. Contributing

JavaScript client for the Langflow API

NPM Version Tests


This package provides an easy way to use the Langflow API to run flows from within server-side JavaScript applications.

Installation

With npm

npm install @datastax/langflow-client

With yarn

yarn add @datastax/langflow-client

With pnpm

pnpm add @datastax/langflow-client

Prerequisites

To use this Langflow client you will need a DataStax account with which you can use DataStax Langflow. You will need a flow before you can call its API. If you don't already have one, you can get started with the Basic Prompting Flow.

You can also use this with an installation of open-source Langflow.

Configuration

DataStax Langflow

You will need a Langflow API key. You provide the API key when you create a new client. You will also need your Langflow ID.

You can generate API keys and find your Langflow ID on the API modal in the Langflow flow editor.

Open-source Langflow

You will need an installation of Langflow. You may also require an API key if you have set up authentication on Langflow.

Usage

You can import the client like so:

import { LangflowClient } from "@datastax/langflow-client";

Initialization

You can then create a new client object with the following options:

  • baseURL: This is set to https://api.langflow.astra.datastax.com by default, if you are running your own instance of Langflow you will need to provide the URL
  • langflowId: The ID of your organisation, which can be found in the API modal of the flow editor. This is not required for your own instance of Langflow
  • apiKey: A Langflow API key that can be generated within your DataStax account or in the settings of open-source Langflow
// using DataStax Langflow, you do not need to provide the baseUrl
const dsLangflowClient = new LangflowClient({ langflowId, apiKey });

// for open-source langflow, you will need to provide a baseUrl and optionally an apiKey
const baseUrl = "http://localhost:7860";
const apiKey = "sk-...";
const osLangflowClient = new LangflowClient({ baseUrl, apiKey });

Running a flow

Langflow documentation for running a flow.

Once you have a client, you can create a reference to a flow using the flowID. This can be found in the API modal in Langflow.

const flow = client.flow(flowId);

You can run a flow by calling run with the text input to the flow:

const response = await client.flow(flowId).run(input);

You can add tweaks to a flow like so:

const response = await client
  .flow(flowId)
  .tweak(tweakName, tweakOptions)
  .run(input);

Or you can pass all tweaks as an object:

const response = await client.flow(flowId).run(input, { tweaks });

You can also pass input and output options, as well as a session ID.

import { InputTypes, OutputTypes } from "@datastax/langflow-client/consts";

const response = await client.flow(flowId).run(input, {
  input_type: InputTypes.CHAT,
  output_type: OutputTypes.CHAT,
  session_id,
  tweaks,
});

The available input types are "chat", "text" and "any". The available output types are "chat", "text", "any" and "debug". The default for both is "chat".

Flow reponses

Langflow is very flexible in its output. So the FlowResponse object gives you raw access to the sessionId and the outputs.

const response = await client.flow(flowId).run(input);
console.log(response.outputs);

There is one convenience function that will return you the first chat output message text. If you only have one chat output component in your flow, this is a useful shortcut to get to that response.

const response = await client.flow(flowId).run(input);
console.log(response.chatOutputText());

Streaming

The Langflow API supports streaming responses. Instead of calling run on a Flow object, you can call stream with the same arguments and the response will be a ReadableStream of objects.

const response = await client.flow(flowId).stream(input);

for await (const event of response) {
  console.log(event);
}

There are three different events: add_message, token, and end. The events mean:

  • add_message: a message being added to the chat and can refer to a human input message or a response from an AI
  • token: a token that is emitted as part of a message being generated by the flow
  • end: all tokens have been returned, this message will also contain a full FlowResponse

Event objects have the format:

{
  event: "add_message" | "token" | "end",
  data: object
}

The event.data is different per event type. The token event type is the simplest and looks like this:

{
  "event": "token",
  "data": {
    "chunk": "hello ",
    "id": "6686ff20-0c95-40bb-8879-fd90ed3d634e",
    "timestamp": "2025-02-12 22:18:04 UTC"
  }
}

There's more documentation and examples of a streaming response in the Langflow docs.

File upload

Images

The Langflow v1 file upload API supports uploading image files to a flow, that can then be used in that flow.

Chat input components support files as input as well as text. You need to upload your file first, using the file upload function, then provide the file path to the flow as a tweak.

const buffer = await readFile(path);
const file = new File([buffer], "image.jpg", { type: "image/jpeg" });

const flow = client.flow(flowId)
const file = await flow.uploadFile(file);
console.log(file);
// => { flowId: "XXX", filePath: "YYY" }

const response = await flow
  .tweak("ChatInput-abcd": { files: file.filePath })
  .run("What can you see in this image?");

[!WARNING]
DataStax Langflow doesn't make file upload available, you will receive a 501 Not Implemented error.

File uploads

The Langflow v2 file upload API supports uploading files to a user. These can then be used with the File component.

[!WARNING] The v2 file upload and the File component don't support image uploads. For images you should use the v1 file upload API.

You can upload files like this:

const buffer = await readFile(path);
const file = new File([buffer], "document.pdf", { type: "application/pdf" });

const fileUpload = await client.files.upload(file);
console.log(file);
// => { path: "abc123/document.pdf", name: "document", ... }

You can then send them to the file component in a flow using a tweak.

const flow = client.flow(flowId);
flow.tweak("File-abc123", {
  path: fileUpload.path,
});

You can also list your uploaded files with the files.list() function:

const files = await client.files.list();

console.log(files);
// [{ path: "...", }, ...]

[!NOTE] TODO: other file methods available through the API: download, edit, delete, and delete all

Logs

Langflow documentation for the logs API.

Fetching the logs

You can fetch the logs for the your Langflow instance.

const logs = await client.logs.fetch();

When fetching the logs, you can pass a timestamp and either a number of lines_before or lines_after the timestamp. For example, the following code will get the 10 log lines that happened after 1 hour ago:

const logs = await client.logs.fetch({
  timestamp: Date.now() - 60 * 60 * 1000,
  lines_after: 10,
});

Streaming the logs

You can also stream the logs by requesting the streaming endpoint.

for await (const log of await client.logs.stream()) {
  console.log(log);
}

Aborting requests

You can use the standard AbortController to cancel requests by passing a signal to the run or uploadFile functions. The functions will reject with a DOMException error with the name AbortError or, if you use AbortSignal.timeout, TimeoutError.

For example, when running the following code, if the entire request takes longer than 500ms, then the promise will reject and the error message will be, "The operation was aborted due to timeout".

const signal = AbortSignal.timeout(500);
try {
  const response = await client.flow(flowId).run(input, { signal });
} catch (error) {
  console.error(error.message);
}

Contributing

To run and contribute to this library you can clone the repository:

git clone [email protected]:datastax/langflow-client-ts.git
cd langflow-client-ts

Install the dependencies:

npm install

Run the tests:

npm test

Transpile the TypeScript to JavaScript:

npm run build

Transpile the TypeScript to JavaScript in watch mode:

npm run build:watch

Lint the code:

npm run lint

Format the code:

npm run format

Check the formatting of the code:

npm run format:check

版本与发布记录

@datastax/langflow-client 共发布 8 个版本,首次发布于 2025/01/17;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.4.1latest2025/09/011.2 年前
0.4.02025/06/271.4 年前
0.3.12025/06/021.4 年前
0.3.02025/05/301.5 年前
0.2.12025/03/011.7 年前
0.2.02025/02/131.7 年前

关于 @datastax/langflow-client 的常见问题

@datastax/langflow-client 是什么?

A JavaScript client for the Langflow API

如何安装 @datastax/langflow-client?

在项目目录运行 npm install @datastax/langflow-client,或使用 pnpm add / yarn add / bun add。

@datastax/langflow-client 支持 TypeScript 吗?

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

@datastax/langflow-client 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@datastax/langflow-client 可以商用吗?

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

@datastax/langflow-client 的下载量有多少?

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

@datastax/langflow-client 还在维护吗?

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