使用 Langflow API 触发 flow
构建 flow 后,您可能希望在应用程序中运行它,比如在移动应用或网站中的聊天机器人。
Langflow 提供多种从外部应用程序运行 flow 的方法:
虽然您可以在独立的本地 Langflow 实例中使用这些选项,但当您部署了 Langflow 服务器或将 Langflow 打包为应用程序的依赖项时,它们通常更有价值。
使用 Langflow API 运行 flow
Langflow API 是以编程方式访问您的 flow 和 Langflow 服务器的主要方法。
有关调用 Langflow API 的脚本示例,请参阅快速开始。
生成 API 代码片段
为了帮助您在脚本中嵌入 Langflow API 请求,Langflow 会自动为您的 flow 生成 Python、JavaScript 和 curl 代码片段。 要获取这些代码片段,请执行以下操作:
-
在 Langflow 中,打开您想要嵌入到应用程序中的 flow。
-
点击 Share,然后选择 API access。
这些代码片段调用
/v1/run/$FLOW_ID
端点,并自动填充最小值,如 Langflow 服务器 URL、flow ID、请求头和请求参数。WindowsAPI 访问面板生成的路径假定为 *nix 环境。 如果您使用 Microsoft Windows 或 WSL,可能需要调整代码片段中给出的文件路径。
-
可选:点击 Input Schema 来修改代码片段中的组件参数,而无需更改 flow 本身。
-
复制您要使 用的语言的片段。
-
按原样运行片段,或在更大的脚本上下文中使用片段。
有关其他 Langflow API 端点的更多信息和示例,请参阅Langflow API 入门。
Langflow API 身份验证
在 Langflow 1.5 及更高版本中,大多数 API 端点都需要使用 Langflow API 密钥进行身份验证,即使 AUTO_LOGIN
设置为 True
。
唯一的例外是 MCP 端点 /v1/mcp
、/v1/mcp-projects
和 /v2/mcp
,它们从不需要身份验证。
在 API access 面板中生成的代码片段包含一个脚本,该脚本检查在本地终端会话中设置的 LANGFLOW_API_KEY
环境变量。
此脚本不会检查在本地终端会话之外的任何地方设置的 Langflow API 密钥。
要使此脚本工作,您必须在打算运行代码片段的终端会话中设置 LANGFLOW_API_KEY
变量,如 export LANGFLOW_API_KEY="sk..."
。
或者,您可以编辑代码片段以包含 x-api-key
请求头,并确保请求可以对 Langflow API 进行身份验证。
有关更多信息,请参阅 API 密钥 和 Langflow API 入门
Input Schema (tweaks)
Tweaks 是一次性覆盖,在运行时修改组件参数,而不是永久修改 flow 本身。 有关在脚本中使用 tweaks 的示例,请参 阅快速开始。
Tweaks 使您的 flow 更动态和可重用。
您可以创建一个 flow 并通过在每个应用程序的 Langflow API 请求中传递特定于应用程序的 tweaks 来将其用于多个应用程序。
在 API access 面板中,点击 Input Schema 将 tweaks
添加到 flow 代码片段的请求载荷中。
对 flow 的 Input Schema 的更改仅保存为该 flow 的 API access 代码片段的 tweaks。 这些 tweaks 不会更改在 Workspace 中设置的 flow 参数,也不适用于其他 flow。
通过 Input Schema 添加 tweaks 可以帮助您排除手动添加到 Langflow API 请求中的 tweaks 的格式问题。
例如,以下 curl 命令包含一个 tweak,用于禁用 flow 的 Chat Input 组件中的 Store Messages 设置:
_14curl --request POST \_14 --url "http://LANGFLOW_SERVER_ADDRESS/api/v1/run/FLOW_ID" \_14 --header "Content-Type: application/json" \_14 --header "x-api-key: LANGFLOW_API_KEY" \_14 --data '{_14 "input_value": "Text to input to the flow",_14 "output_type": "chat",_14 "input_type": "chat",_14 "tweaks": {_14 "ChatInput-4WKag": {_14 "should_store_message": false_14 }_14 }_14}'
使用 flow ID 别名
如果您希望请求使用别名而不是实际的 flow ID,您可以重命名 flow 的 /v1/run/$FLOW_ID
端点:
-
在 Langflow 中,打开 flow,点击 Share,然后选择 API access。
-
点击 Input Schema。
-
在 Endpoint Name 字段中,为您的 flow ID 输入一个别名,如一个易记、人类可读的名称。
名称只能包含字母、数字、连字符和下划线,如
flow-customer-database-agent
。 -
要保存更改,请关闭 Input Schema 面板。
自动生成的代码片段现在使用您的新端点名称而不是原始 flow ID,如 url = "http://localhost:7868/api/v1/run/flow-customer-database-agent"
。
将 flow 嵌入到网站中
对于每个 flow,Langflow 提供了一个代码片段,您可以将其插入到网站 HTML 的 <body>
中,通过嵌入式聊天小部件与您的 flow 交互。
聊天小部件仅支持具有 Chat Input 和 Chat Output 组件的 flow,这些组件是聊天体验所必需的。 Text Input 和 Text Output 组件可以发送和接收消息,但它们不包含持续的 LLM 聊天上下文。
尝试与没有 Chat Input 组件 的 flow 聊天将触发 flow,但响应只是表示输入为空。
获取 langflow-chat 片段
要获取 flow 的嵌入式聊天小部件代码片段,请执行以下操作:
- 在 Langflow 中,打开您要嵌入的 flow。
- 点击 Share,然后选择 Embed into site。
- 复制代码片段并在您网站 HTML 的
<body>
中使用它。 有关更多信息,请参阅使用 React、Angular 或 HTML 嵌入聊天小部件。 - 添加
api_key
属性以确保小部件有权限运行 flow,如配置 langflow-chat web 组件中所解释。
聊天小部件实现为一个名为 langflow-chat
的 web 组件,从 CDN 加载。有关更多信息,请参阅 langflow-embedded-chat 仓库。
例如,以下 HTML 为部署在 ngrok 上的 Langflow 服务器上托管的基本提示 flow 嵌入 了一个聊天小部件:
_12<html>_12 <head>_12 <script src="https://cdn.jsdelivr.net/gh/langflow-ai/langflow-embedded-chat@main/dist/build/static/js/bundle.min.js"></script>_12 </head>_12 <body>_12 <langflow-chat_12 host_url="https://c822-73-64-93-151.ngrok-free.app"_12 flow_id="dcbed533-859f-4b99-b1f5-16fce884f28f"_12 api_key="$LANGFLOW_API_KEY"_12 ></langflow-chat>_12 </body>_12</html>
When this code is deployed to a live site, it renders as a responsive chatbot. If a user interacts with the chatbot, the input triggers the specified flow, and then the chatbot returns the output from the flow run.
Use the Langflow embedded chat CodeSandbox for an interactive live demo of the embedded chat widget that uses your own flow. For more information, see the langflow-embedded-chat README.
Embed the chat widget with React, Angular, or HTML
The following examples show how to use embedded chat widget in React, Angular, and plain HTML.
- React
- Angular
- HTML
To use the chat widget in your React application, create a component that loads the widget script and renders the chat interface:
-
Declare your web component, and then encapsulate it in a React component:
_21//Declaration of langflow-chat web component_21declare global {_21namespace JSX {_21interface IntrinsicElements {_21"langflow-chat": any;_21}_21}_21}_21_21//Definition for langflow-chat React component_21export default function ChatWidget({ className }) {_21return (_21<div className={className}>_21<langflow-chat_21host_url="https://c822-73-64-93-151.ngrok-free.app"_21flow_id="dcbed533-859f-4b99-b1f5-16fce884f28f"_21api_key="$LANGFLOW_API_KEY"_21></langflow-chat>_21</div>_21);_21} -
Place the component anywhere in your code to render the chat widget.
In the following example, the React widget component is located at
docs/src/components/ChatWidget/index.tsx
, andindex.tsx
includes a script to load the chat widget code from CDN, along with the declaration and definition from the previous step:_38import React, { useEffect } from 'react';_38_38// Component to load the chat widget script_38const ChatScriptLoader = () => {_38useEffect(() => {_38if (!document.querySelector('script[src*="langflow-embedded-chat"]')) {_38const script = document.createElement('script');_38script.src = 'https://cdn.jsdelivr.net/gh/langflow-ai/langflow-embedded-chat@main/dist/build/static/js/bundle.min.js';_38script.async = true;_38document.body.appendChild(script);_38}_38}, []);_38_38return null;_38};_38_38//Declaration of langflow-chat web component_38declare global {_38namespace JSX {_38interface IntrinsicElements {_38"langflow-chat": any;_38}_38}_38}_38_38//Definition for langflow-chat React component_38export default function ChatWidget({ className }) {_38return (_38<div className={className}>_38<ChatScriptLoader />_38<langflow-chat_38host_url="https://c822-73-64-93-151.ngrok-free.app"_38flow_id="dcbed533-859f-4b99-b1f5-16fce884f28f"_38api_key="$LANGFLOW_API_KEY"_38></langflow-chat>_38</div>_38);_38} -
Import the
langflow-chat
React component to make it available for use on a page. Modify the following import statement with your React component's name and path:_10import ChatWidget from '@site/src/components/ChatWidget'; -
To display the widget, call your
langflow-chat
component in the desired location on the page. Modify the following reference for your React component's name and the desiredclassName
:_10<ChatWidget className="my-chat-widget" />
To use the chat widget in your Angular application, create a component that loads the widget script and renders the chat interface.
In an Angular application, langflow-chat
is a custom web component that you must explicitly allow in your site's .components.ts
.
Therefore, to use the embedded chat widget, you must add CUSTOM_ELEMENTS_SCHEMA
to your module's configuration, and then integrate the <langflow-chat>
element.
Angular requires you to explicitly allow custom web components, like langflow-chat
, in your site's components
.
Therefore, you must add the <langflow-chat>
element to your Angular template and configure Angular to recognize it.
You must add CUSTOM_ELEMENTS_SCHEMA
to your module's configuration to enable this.
-
In your Angular application, edit the
.module.ts
file where you want to add thelangflow-chat
web component. -
At the top of
.module.ts
, importCUSTOM_ELEMENTS_SCHEMA
:_10import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core'; -
In the
@NgModule
decorator, addCUSTOM_ELEMENTS_SCHEMA
to theschemas
array:_16import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';_16import { BrowserModule } from '@angular/platform-browser';_16import { AppComponent } from './app.component';_16_16@NgModule({_16declarations: [_16AppComponent_16],_16imports: [_16BrowserModule_16],_16schemas: [CUSTOM_ELEMENTS_SCHEMA],_16providers: [],_16bootstrap: [AppComponent]_16})_16export class AppModule { } -
Edit the
.component.ts
file where you want to use the embedded chat widget. -
In the
@Component
decorator, add the<langflow-chat>
element to thetemplate
key:_24import { Component } from '@angular/core';_24_24@Component({_24selector: 'app-root',_24template: `_24<div class="container">_24<h1>Langflow Chat Test</h1>_24<langflow-chat_24host_url="https://c822-73-64-93-151.ngrok-free.app"_24flow_id="dcbed533-859f-4b99-b1f5-16fce884f28f"_24api_key="$LANGFLOW_API_KEY"_24></langflow-chat>_24</div>_24`,_24styles: [`_24.container {_24padding: 20px;_24text-align: center;_24}_24`]_24})_24export class AppComponent {_24title = 'Langflow Chat Test';_24}
_12<html lang="en">_12<head>_12<script src="https://cdn.jsdelivr.net/gh/langflow-ai/langflow-embedded-chat@v1.0.7/dist/build/static/js/bundle.min.js"></script>_12</head>_12<body>_12<langflow-chat_12 host_url="https://c822-73-64-93-151.ngrok-free.app"_12 flow_id="dcbed533-859f-4b99-b1f5-16fce884f28f"_12 api_key="$LANGFLOW_API_KEY"_12 ></langflow-chat>_12</body>_12</html>
Configure the langflow-chat web component
To use the embedded chat widget in your HTML, the langflow-chat
web component must include the following minimum inputs (also known as props in React):
host_url
: Your Langflow server URL. Must beHTTPS
. Don't include a trailing slash (/
).flow_id
: The ID of the flow you want to embed.api_key
: A Langflow API key. This prop is recommended to ensure the widget has permission to run the flow.
The minimum inputs are automatically populated in the Embed into site code snippet that is generated by Langflow.
You can use additional inputs (props) to modify the embedded chat widget. For a list of all props, types, and descriptions, see the langflow-embedded-chat README.
Example: Langflow API key prop
The api_key
prop stores a Langflow API key that the chat widget can use to authenticate the underlying Langflow API request.
The Langflow team recommends following industry best practices for handling sensitive credentials. For example, securely store your API key, and then retrieve with an environment variable:
_10<langflow-chat_10 host_url="https://c822-73-64-93-151.ngrok-free.app"_10 flow_id="dcbed533-859f-4b99-b1f5-16fce884f28f"_10 api_key="$LANGFLOW_API_KEY"_10></langflow-chat>
Example: Style props
There are many props you can use to customize the style and positioning of the embedded chat widget.
Many of these props are of type JSON, and they require specific formatting, depending on where you embed the langflow-chat
web component.
In React and plain HTML, JSON props are expressed as JSON objects or stringified JSON, such as \{"key":"value"\}
:
_17<langflow-chat_17 host_url="https://c822-73-64-93-151.ngrok-free.app"_17 flow_id="dcbed533-859f-4b99-b1f5-16fce884f28f"_17 api_key="$LANGFLOW_API_KEY"_17 chat_window_style='{_17 "backgroundColor": "#1a0d0d",_17 "border": "4px solid #b30000",_17 "borderRadius": "16px",_17 "boxShadow": "0 8px 32px #b30000",_17 "color": "#fff",_17 "fontFamily": "Georgia, serif",_17 "padding": "16px"_17 }'_17 window_title="Custom Styled Chat"_17 height="600"_17 width="400"_17></langflow-chat>
For Angular applications, use property binding syntax to pass JSON props as JavaScript objects. For example:
_30import { Component } from '@angular/core';_30_30@Component({_30 selector: 'app-root',_30 template: `_30 <div class="container">_30 <h1>Langflow Chat Test</h1>_30 <langflow-chat_30 host_url="https://c822-73-64-93-151.ngrok-free.app"_30 flow_id="dcbed533-859f-4b99-b1f5-16fce884f28f"_30 api_key="$LANGFLOW_API_KEY"_30 [chat_window_style]='{"backgroundColor": "#ffffff"}'_30 [bot_message_style]='{"color": "#000000"}'_30 [user_message_style]='{"color": "#000000"}'_30 height="600"_30 width="400"_30 chat_position="bottom-right"_30 ></langflow-chat>_30 </div>_30 `,_30 styles: [`_30 .container {_30 padding: 20px;_30 text-align: center;_30 }_30 `]_30})_30export class AppComponent {_30 title = 'Langflow Chat Test';_30}
Example: Session ID prop
The following example adds a custom session ID to help identify flow runs started by the embeded chat widget:
_10<langflow-chat_10 host_url="https://c822-73-64-93-151.ngrok-free.app"_10 flow_id="dcbed533-859f-4b99-b1f5-16fce884f28f"_10 api_key="$LANGFLOW_API_KEY"_10 session_id="$SESSION_ID"_10></langflow-chat>
Example: Tweaks prop
Use the tweaks
prop to modify flow parameters at runtime.
The available keys for the tweaks
object depend on the flow you are serving through the embedded chat widget.
In React and plain HTML, tweaks
are declared as a JSON object, similar to how you would pass them to a Langflow API endpoint like /v1/run/$FLOW_ID
.
For example:
_10<langflow-chat_10 host_url="https://c822-73-64-93-151.ngrok-free.app"_10 flow_id="dcbed533-859f-4b99-b1f5-16fce884f28f"_10 api_key="$LANGFLOW_API_KEY"_10 tweaks='{_10 "model_name": "llama-3.1-8b-instant"_10 }'_10></langflow-chat>
For Angular applications, use property binding syntax to pass JSON props as JavaScript objects. For example:
_25import { Component } from '@angular/core';_25_25@Component({_25 selector: 'app-root',_25 template: `_25 <div class="container">_25 <h1>Langflow Chat Test</h1>_25 <langflow-chat_25 host_url="https://c822-73-64-93-151.ngrok-free.app"_25 flow_id="dcbed533-859f-4b99-b1f5-16fce884f28f"_25 api_key="$LANGFLOW_API_KEY"_25 [tweaks]='{"model_name": "llama-3.1-8b-instant"}'_25 ></langflow-chat>_25 </div>_25 `,_25 styles: [`_25 .container {_25 padding: 20px;_25 text-align: center;_25 }_25 `]_25})_25export class AppComponent {_25 title = 'Langflow Chat Test';_25}
Serve flows through a Langflow MCP server
Each Langflow project has an MCP server that exposes the project's flows as tools that MCP clients can use to generate responses.
In addition to serving flows through Langflow MCP servers, you can use Langflow as an MCP client to access any MCP server, including Langflow MCP servers.
Interactions with Langflow MCP servers happen through the Langflow API's /mcp
endpoints.
For more information, see Use Langflow as an MCP server and Use Langflow as an MCP client.