Skip to main content

Run Langflow in backend-only mode

Langflow can run in --backend-only mode to expose a Langflow app as an API endpoint, without running the frontend UI. This is also known as "headless" mode. Running Langflow without the frontend is useful for automation, testing, and situations where you just need to serve a flow as a workload without creating a new flow in the UI.

To run Langflow in backend-only mode, pass the --backend-only flag at startup.


_10
python3 -m langflow run --backend-only

The terminal prints Welcome to ⛓ Langflow, and Langflow will now serve requests to its API without the frontend running.

Set up a basic prompting flow in backend-only mode​

This example shows you how to set up a Basic Prompting flow as an endpoint in backend-only mode. However, you can use these same instructions as guidelines for using any type of flow in backend-only mode.

Prerequisites​

Get your flow's ID​

This guide assumes you have created a Basic Prompting flow or have another working flow available.

  1. In the Langflow UI, click API.
  2. Click curl > Copy code to copy the curl command. This command will POST input to your flow's endpoint. It will look something like this:

_12
curl -X POST \
_12
"http://127.0.0.1:7861/api/v1/run/fff8dcaa-f0f6-4136-9df0-b7cb38de42e0?stream=false" \
_12
-H 'Content-Type: application/json'\
_12
-d '{"input_value": "message",
_12
"output_type": "chat",
_12
"input_type": "chat",
_12
"tweaks": {
_12
"ChatInput-8a86T": {},
_12
"Prompt-pKfl9": {},
_12
"ChatOutput-WcGpD": {},
_12
"OpenAIModel-5UyvQ": {}
_12
}}'

The flow ID in this example is fff8dcaa-f0f6-4136-9df0-b7cb38de42e0, a UUID generated by Langflow and used in the endpoint URL. See API to change the endpoint.

  1. To stop Langflow, press Ctrl+C.

Start Langflow in backend-only mode​

  1. Start Langflow in backend-only mode.

_10
python3 -m langflow run --backend-only

The terminal prints Welcome to ⛓ Langflow. Langflow is now serving requests to its API.

  1. Run the curl code you copied from the UI. You should get a result like this:

_10
{"session_id":"ef7e0554-69e5-4e3e-ab29-ee83bcd8d9ef:bf81d898868ac87e1b4edbd96c131c5dee801ea2971122cc91352d144a45b880","outputs":[{"inputs":{"input_value":"hi, are you there?"},"outputs":[{"results":{"result":"Arrr, ahoy matey! Aye, I be here. What be ye needin', me hearty?"},"artifacts":{"message":"Arrr, ahoy matey! Aye, I be here. What be ye needin', me hearty?","sender":"Machine","sender_name":"AI"},"messages":[{"message":"Arrr, ahoy matey! Aye, I be here. What be ye needin', me hearty?","sender":"Machine","sender_name":"AI","component_id":"ChatOutput-ktwdw"}],"component_display_name":"Chat Output","component_id":"ChatOutput-ktwdw","used_frozen_result":false}]}]}%

This confirms Langflow is receiving your POST request, running the flow, and returning the result without running the frontend.

You can interact with this endpoint using the other options in the API menu, including the Python and Javascript APIs.

Query the Langflow endpoint with a Python script​

Using the same flow ID, run a Python sample script to send a query and get a prettified JSON response back.

  1. Create a Python file and name it langflow_api_demo.py.

_15
import requests
_15
import json
_15
_15
def query_langflow(message):
_15
url = "http://127.0.0.1:7861/api/v1/run/fff8dcaa-f0f6-4136-9df0-b7cb38de42e0"
_15
headers = {"Content-Type": "application/json"}
_15
data = {"input_value": message}
_15
_15
response = requests.post(url, headers=headers, json=data)
_15
return response.json()
_15
_15
user_input = input("Enter your message: ")
_15
result = query_langflow(user_input)
_15
_15
print(json.dumps(result, indent=2))

  1. Run the script.

_10
python langflow_api_demo.py

  1. Enter your message when prompted. You will get a prettified JSON response back containing a response to your message.

Configure host and ports in backend-only mode​

To change the host and port, pass the values as additional flags.


_10
python -m langflow run --host 127.0.0.1 --port 7860 --backend-only

Hi, how can I help you?