Skip to main content

API Keys

Introduction

Langflow offers an API Key functionality that allows users to access their individual components and flows without going through traditional login authentication. The API Key is a user-specific token that can be included in the request's header or query parameter to authenticate API calls. The following documentation outlines how to generate, use, and manage these API Keys in Langflow.

Generating an API Key

Through Langflow UI

Docusaurus themed imageDocusaurus themed image
  1. Click on the "API Key" icon.
  2. Click on "Create new secret key".
  3. Give it an optional name.
  4. Click on "Create secret key".
  5. Copy the API key and store it in a secure location.

Using the API Key

Using the x-api-key Header

Include the x-api-key in the HTTP header when making API requests:


_10
curl -X POST \
_10
http://localhost:3000/api/v1/process/<your_flow_id> \
_10
-H 'Content-Type: application/json'\
_10
-H 'x-api-key: <your api key>'\
_10
-d '{"inputs": {"text":""}, "tweaks": {}}'

With Python using requests:


_38
import requests
_38
from typing import Optional
_38
_38
BASE_API_URL = "http://localhost:3001/api/v1/process"
_38
FLOW_ID = "4441b773-0724-434e-9cee-19d995d8f2df"
_38
# You can tweak the flow by adding a tweaks dictionary
_38
# e.g {"OpenAI-XXXXX": {"model_name": "gpt-4"}}
_38
TWEAKS = {}
_38
_38
def run_flow(inputs: dict,
_38
flow_id: str,
_38
tweaks: Optional[dict] = None,
_38
apiKey: Optional[str] = None) -> dict:
_38
"""
_38
Run a flow with a given message and optional tweaks.
_38
_38
:param message: The message to send to the flow
_38
:param flow_id: The ID of the flow to run
_38
:param tweaks: Optional tweaks to customize the flow
_38
:return: The JSON response from the flow
_38
"""
_38
api_url = f"{BASE_API_URL}/{flow_id}"
_38
_38
payload = {"inputs": inputs}
_38
headers = {}
_38
_38
if tweaks:
_38
payload["tweaks"] = tweaks
_38
if apiKey:
_38
headers = {"x-api-key": apiKey}
_38
_38
response = requests.post(api_url, json=payload, headers=headers)
_38
return response.json()
_38
_38
# Setup any tweaks you want to apply to the flow
_38
inputs = {"text":""}
_38
api_key = "<your api key>"
_38
print(run_flow(inputs, flow_id=FLOW_ID, tweaks=TWEAKS, apiKey=api_key))

Using the Query Parameter

Alternatively, you can include the API key as a query parameter in the URL:


_10
curl -X POST \
_10
http://localhost:3000/api/v1/process/<your_flow_id>?x-api-key=<your_api_key> \
_10
-H 'Content-Type: application/json'\
_10
-d '{"inputs": {"text":""}, "tweaks": {}}'

Or with Python:


_37
import requests
_37
_37
BASE_API_URL = "http://localhost:3001/api/v1/process"
_37
FLOW_ID = "4441b773-0724-434e-9cee-19d995d8f2df"
_37
# You can tweak the flow by adding a tweaks dictionary
_37
# e.g {"OpenAI-XXXXX": {"model_name": "gpt-4"}}
_37
TWEAKS = {}
_37
_37
def run_flow(inputs: dict,
_37
flow_id: str,
_37
tweaks: Optional[dict] = None,
_37
apiKey: Optional[str] = None) -> dict:
_37
"""
_37
Run a flow with a given message and optional tweaks.
_37
_37
:param message: The message to send to the flow
_37
:param flow_id: The ID of the flow to run
_37
:param tweaks: Optional tweaks to customize the flow
_37
:return: The JSON response from the flow
_37
"""
_37
api_url = f"{BASE_API_URL}/{flow_id}"
_37
_37
payload = {"inputs": inputs}
_37
headers = {}
_37
_37
if tweaks:
_37
payload["tweaks"] = tweaks
_37
if apiKey:
_37
api_url += f"?x-api-key={apiKey}"
_37
_37
response = requests.post(api_url, json=payload, headers=headers)
_37
return response.json()
_37
_37
# Setup any tweaks you want to apply to the flow
_37
inputs = {"text":""}
_37
api_key = "<your api key>"
_37
print(run_flow(inputs, flow_id=FLOW_ID, tweaks=TWEAKS, apiKey=api_key))

Security Considerations

  • Visibility: The API key won't be retrievable again through the UI for security reasons.
  • Scope: The key only allows access to the flows and components of the specific user to whom it was issued.

Revoking an API Key

To revoke an API key, simply delete it from the UI. This will immediately invalidate the key and prevent it from being used again.

Hi, how can I help you?