API Keys
This page may contain outdated information. It will be updated as soon as possible.
Langflow provides an API key functionality that allows users to access their individual components and flows without traditional login authentication. The API key is a user-specific token that can be included in the request header or query parameter to authenticate API calls. This documentation outlines how to generate, use, and manage API keys in Langflow.
-The default user and password are set using the LANGFLOW_SUPERUSER and LANGFLOW_SUPERUSER_PASSWORD environment variables. The default values are langflow and langflow, respectively.
Generate an API key
+Langflow provides an API key functionality that allows users to access their individual components and flows without traditional login authentication. The API key is a user-specific token that can be included in the request header, query parameter, or as a command line argument to authenticate API calls. This documentation outlines how to generate, use, and manage API keys in Langflow.
+The default user and password are set using the LANGFLOW_SUPERUSER and LANGFLOW_SUPERUSER_PASSWORD environment variables. The default values are langflow and langflow, respectively.
Generate an API key
Generate a user-specific token to use with Langflow.
-Generate an API key with the Langflow UI
+Generate an API key with the Langflow UI
-
-
-
-
Click on the "API Key" icon.
-
-
- -
-
Click on "Create new secret key".
-
- -
-
Give it an optional name.
-
- -
-
Click on "Create secret key".
-
- -
-
Copy the API key and store it in a secure location.
-
+ - Click your user icon and select Settings. +
- Click Langflow API, and then click Add New. +
- Name your key, and then click Create Secret Key. +
- Copy the API key and store it in a secure location.
Generate an API key with the Langflow CLI
+Generate an API key with the Langflow CLI
_13langflow api-key_13# or_13python -m langflow api-key_13╭─────────────────────────────────────────────────────────────────────╮_13│ API Key Created Successfully: │_13│ │_13│ sk-O0elzoWID1izAH8RUKrnnvyyMwIzHi2Wk-uXWoNJ2Ro │_13│ │_13│ This is the only time the API key will be displayed. │_13│ Make sure to store it in a secure location. │_13│ │_13│ The API key has been copied to your clipboard. Cmd + V to paste it. │_13╰──────────────────────────────
Use the Langflow API key
+Authenticate requests with the Langflow API key
Include your API key in API requests to authenticate requests to Langflow.
-Use the x-api-key header
-Include the x-api-key in the HTTP header when making API requests:
_10curl -X POST \\_10 <http://localhost:3000/api/v1/run/><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:
_38import requests_38from typing import Optional_38_38BASE_API_URL = "<http://localhost:3001/api/v1/process>"_38FLOW_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"}}_38TWEAKS = {}_38_38def 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_38inputs = {"text":""}_38api_key = "<your api key>"_38print(run_flow(inputs, flow_id=FLOW_ID, tweaks=TWEAKS, apiKey=api_key))
Use the query parameter
-Include the API key as a query parameter in the URL:
-
_10curl -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": {}}'
With Python using requests:
_37import requests_37_37BASE_API_URL = "<http://localhost:3001/api/v1/process>"_37FLOW_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"}}_37TWEAKS = {}_37_37def 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_37inputs = {"text":""}_37api_key = "<your api key>"_37print(run_flow(inputs, flow_id=FLOW_ID, tweaks=TWEAKS, apiKey=api_key))
Security Considerations
+Include the API key in the HTTP header
+To use the API key when making API requests with cURL, include the API key in the HTTP header.
+
_10curl -X POST \_10 "http://127.0.0.1:7860/api/v1/run/*`YOUR_FLOW_ID`*?stream=false" \_10 -H 'Content-Type: application/json' \_10 -H 'x-api-key: *`YOUR_API_KEY`*' \_10 -d '{"inputs": {"text":""}, "tweaks": {}}'
To instead pass the API key as a query parameter, do the following:
+
_10curl -X POST \_10 "http://127.0.0.1:7860/api/v1/run/*`YOUR_FLOW_ID`*?x-api-key=*`YOUR_API_KEY`*?stream=false" \_10 -H 'Content-Type: application/json' \_10 -d '{"inputs": {"text":""}, "tweaks": {}}'
To use the API key when making API requests with the Python requests library, include the API key as a variable string.
_93import argparse_93import json_93from argparse import RawTextHelpFormatter_93import requests_93from typing import Optional_93import warnings_93try:_93 from langflow.load import upload_file_93except ImportError:_93 warnings.warn("Langflow provides a function to help you upload files to the flow. Please install langflow to use it.")_93 upload_file = None_93_93BASE_API_URL = "http://127.0.0.1:7860"_93FLOW_ID = "*`YOUR_FLOW_ID`*"_93ENDPOINT = "" # You can set a specific endpoint name in the flow settings_93_93# You can tweak the flow by adding a tweaks dictionary_93# e.g {"OpenAI-XXXXX": {"model_name": "gpt-4"}}_93TWEAKS = {_93 "ChatInput-8a86T": {},_93 "Prompt-pKfl9": {},_93 "ChatOutput-WcGpD": {},_93 "OpenAIModel-5UyvQ": {}_93}_93_93def run_flow(message: str,_93 endpoint: str,_93 output_type: str = "chat",_93 input_type: str = "chat",_93 tweaks: Optional[dict] = None,_93 api_key: Optional[str] = None) -> dict:_93 """_93 Run a flow with a given message and optional tweaks._93_93 :param message: The message to send to the flow_93 :param endpoint: The ID or the endpoint name of the flow_93 :param tweaks: Optional tweaks to customize the flow_93 :return: The JSON response from the flow_93 """_93 api_url = f"{BASE_API_URL}/api/v1/run/{endpoint}"_93_93 payload = {_93 "input_value": message,_93 "output_type": output_type,_93 "input_type": input_type,_93 }_93 headers = None_93 if tweaks:_93 payload["tweaks"] = tweaks_93 if api_key:_93 headers = {"x-api-key": api_key}_93 response = requests.post(api_url, json=payload, headers=headers)_93 return response.json()_93_93def main():_93 parser = argparse.ArgumentParser(description="""Run a flow with a given message and optional tweaks._93Run it like: python <your file>.py "your message here" --endpoint "your_endpoint" --tweaks '{"key": "value"}'""",_93 formatter_class=RawTextHelpFormatter)_93 parser.add_argument("message", type=str, help="The message to send to the flow")_93 parser.add_argument("--endpoint", type=str, default=ENDPOINT or FLOW_ID, help="The ID or the endpoint name of the flow")_93 parser.add_argument("--tweaks", type=str, help="JSON string representing the tweaks to customize the flow", default=json.dumps(TWEAKS))_93 parser.add_argument("--api_key", type=str, help="API key for authentication", default=None)_93 parser.add_argument("--output_type", type=str, default="chat", help="The output type")_93 parser.add_argument("--input_type", type=str, default="chat", help="The input type")_93 parser.add_argument("--upload_file", type=str, help="Path to the file to upload", default=None)_93 parser.add_argument("--components", type=str, help="Components to upload the file to", default=None)_93_93 args = parser.parse_args()_93 try:_93 tweaks = json.loads(args.tweaks)_93 except json.JSONDecodeError:_93 raise ValueError("Invalid tweaks JSON string")_93_93 if args.upload_file:_93 if not upload_file:_93 raise ImportError("Langflow is not installed. Please install it to use the upload_file function.")_93 elif not args.components:_93 raise ValueError("You need to provide the components to upload the file to.")_93 tweaks = upload_file(file_path=args.upload_file, host=BASE_API_URL, flow_id=args.endpoint, components=[args.components], tweaks=tweaks)_93_93 response = run_flow(_93 message=args.message,_93 endpoint=args.endpoint,_93 output_type=args.output_type,_93 input_type=args.input_type,_93 tweaks=tweaks,_93 api_key=args.api_key_93 )_93_93 print(json.dumps(response, indent=2))_93_93if __name__ == "__main__":_93 main()
To pass the API key to your script with a command line argument, do the following:
+
_10python your_script.py "*`YOUR_INPUT_MESSAGE`*" --api_key "*`YOUR_API_KEY`*"
Security considerations
- Visibility: For security reasons, the API key cannot be retrieved again through the UI.
- Scope: The key allows access only to the flows and components of the specific user to whom it was issued.
Custom API endpoint
-Under Project Settings > Endpoint Name, you can pick a custom name for the endpoint used to call your flow from the API.
-Revoke an API Key
-To revoke an API key, delete it from the UI. This action immediately invalidates the key and prevents it from being used again.
