docs/docs/Lfx/lfx-serve.mdx
import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem'; import LfxPrereqs from '@site/docs/_partial-lfx-run-serve-prereqs.mdx'; import LfxComponentCategories from '@site/docs/_partial-lfx-component-categories.mdx';
lfx serve starts a FastAPI server that exposes your flows as HTTP API endpoints at POST /flows/{flow_id}/run.
A LANGFLOW_API_KEY is required because lfx serve starts a publicly accessible server.
To execute a flow once and stream results to stdout instead, see Run flows with LFX.
Both lfx serve and lfx run accept a .json flow file or a .py Python script.
Generate a Langflow API key.
For LFX, generate a secure token locally:
uv run python -c "import secrets; print(secrets.token_urlsafe(32))"
This is different from creating an API key through the Langflow UI or CLI, which stores the key in the Langflow database. LFX only needs a secure token string to authenticate requests.
Set your environment variables. Use a .env file or export directly:
.env file:
LANGFLOW_API_KEY="sk..."
OPENAI_API_KEY="sk-..."
Export:
export LANGFLOW_API_KEY="sk..."
export OPENAI_API_KEY="sk-..."
Start the server with your flow:
With a .env file:
uv run lfx serve simple-agent-flow.json --env-file .env
With exported variables:
uv run lfx serve simple-agent-flow.json
Copy the flow_id from the startup output:
LFX Server
Flow loaded: simple-agent-flow.json (c1dab29d-3364-58ef-8fef-99311d32ee42)
Server: http://127.0.0.1:8000
Run flows at: POST /flows/{flow_id}/run
API key: x-api-key header or ?x-api-key= query parameter
In a new terminal, set your API key and flow ID, then send a test request:
export LANGFLOW_API_KEY="sk..."
export FLOW_ID="c1dab29d-3364-58ef-8fef-99311d32ee42"
import os
import requests
flow_id = os.environ["FLOW_ID"]
api_key = os.environ["LANGFLOW_API_KEY"]
url = f"http://localhost:8000/flows/{flow_id}/run"
headers = {
"Content-Type": "application/json",
"x-api-key": api_key,
}
payload = {
"input_value": "Hello, world!",
}
response = requests.post(url, headers=headers, json=payload, timeout=60)
response.raise_for_status()
print(response.text)
const flowId = process.env.FLOW_ID;
const apiKey = process.env.LANGFLOW_API_KEY;
const options = {
method: "POST",
headers: {
"Content-Type": "application/json",
"x-api-key": apiKey,
},
body: JSON.stringify({
input_value: "Hello, world!",
}),
};
fetch(`http://localhost:8000/flows/${flowId}/run`, options)
.then(async (response) => {
if (!response.ok) {
throw new Error(`HTTP ${response.status}`);
}
console.log(await response.text());
})
.catch((error) => console.error(error));
curl -X POST http://localhost:8000/flows/$FLOW_ID/run \
-H "Content-Type: application/json" \
-H "x-api-key: $LANGFLOW_API_KEY" \
-d '{"input_value": "Hello, world!"}'
To serve multiple flows, pass a directory to lfx serve to load all .json flows at server startup:
uv run lfx serve ./flows/