From 8b271527128f5a3a614b36844c74452c98142687 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Tue, 26 Sep 2023 10:55:54 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=9D=20docs(api.mdx):=20add=20documenta?= =?UTF-8?q?tion=20for=20API=20Keys=20in=20Langflow=20=F0=9F=94=80=20chore(?= =?UTF-8?q?sidebars.js):=20uncomment=20API=20guidelines=20in=20the=20sideb?= =?UTF-8?q?ar=20=F0=9F=96=BC=EF=B8=8F=20chore(api-key.png):=20add=20image?= =?UTF-8?q?=20for=20API=20Key=20documentation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/docs/guidelines/api.mdx | 147 +++++++++++++++++++++++++++++++++++ docs/sidebars.js | 2 +- docs/static/img/api-key.png | Bin 0 -> 2947 bytes 3 files changed, 148 insertions(+), 1 deletion(-) create mode 100644 docs/docs/guidelines/api.mdx create mode 100644 docs/static/img/api-key.png diff --git a/docs/docs/guidelines/api.mdx b/docs/docs/guidelines/api.mdx new file mode 100644 index 000000000..97d2db76e --- /dev/null +++ b/docs/docs/guidelines/api.mdx @@ -0,0 +1,147 @@ +import useBaseUrl from "@docusaurus/useBaseUrl"; +import ZoomableImage from "/src/theme/ZoomableImage.js"; + +# 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 + +{/* add image img/api-key.png */} + + + +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: + +```bash +curl -X POST \ + http://localhost:3000/api/v1/process/ \ + -H 'Content-Type: application/json'\ + -H 'x-api-key: '\ + -d '{"inputs": {"text":""}, "tweaks": {}}' +``` + +With Python using `requests`: + +```python +import requests +from typing import Optional + +BASE_API_URL = "http://localhost:3001/api/v1/process" +FLOW_ID = "4441b773-0724-434e-9cee-19d995d8f2df" +# You can tweak the flow by adding a tweaks dictionary +# e.g {"OpenAI-XXXXX": {"model_name": "gpt-4"}} +TWEAKS = {} + +def run_flow(inputs: dict, + flow_id: str, + tweaks: Optional[dict] = None, + apiKey: Optional[str] = None) -> dict: + """ + Run a flow with a given message and optional tweaks. + + :param message: The message to send to the flow + :param flow_id: The ID of the flow to run + :param tweaks: Optional tweaks to customize the flow + :return: The JSON response from the flow + """ + api_url = f"{BASE_API_URL}/{flow_id}" + + payload = {"inputs": inputs} + headers = {} + + if tweaks: + payload["tweaks"] = tweaks + if apiKey: + headers = {"x-api-key": apiKey} + + response = requests.post(api_url, json=payload, headers=headers) + return response.json() + +# Setup any tweaks you want to apply to the flow +inputs = {"text":""} +api_key = "" +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: + +```bash +curl -X POST \ + http://localhost:3000/api/v1/process/?x-api-key= \ + -H 'Content-Type: application/json'\ + -d '{"inputs": {"text":""}, "tweaks": {}}' +``` + +Or with Python: + +```python +import requests + +BASE_API_URL = "http://localhost:3001/api/v1/process" +FLOW_ID = "4441b773-0724-434e-9cee-19d995d8f2df" +# You can tweak the flow by adding a tweaks dictionary +# e.g {"OpenAI-XXXXX": {"model_name": "gpt-4"}} +TWEAKS = {} + +def run_flow(inputs: dict, + flow_id: str, + tweaks: Optional[dict] = None, + apiKey: Optional[str] = None) -> dict: + """ + Run a flow with a given message and optional tweaks. + + :param message: The message to send to the flow + :param flow_id: The ID of the flow to run + :param tweaks: Optional tweaks to customize the flow + :return: The JSON response from the flow + """ + api_url = f"{BASE_API_URL}/{flow_id}" + + payload = {"inputs": inputs} + headers = {} + + if tweaks: + payload["tweaks"] = tweaks + if apiKey: + api_url += f"?x-api-key={apiKey}" + + response = requests.post(api_url, json=payload, headers=headers) + return response.json() + +# Setup any tweaks you want to apply to the flow +inputs = {"text":""} +api_key = "" +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. diff --git a/docs/sidebars.js b/docs/sidebars.js index 166d72f22..116e54616 100644 --- a/docs/sidebars.js +++ b/docs/sidebars.js @@ -17,7 +17,7 @@ module.exports = { collapsed: false, items: [ "guidelines/login", - // "guidelines/api", + "guidelines/api", "guidelines/components", "guidelines/features", "guidelines/collection", diff --git a/docs/static/img/api-key.png b/docs/static/img/api-key.png new file mode 100644 index 0000000000000000000000000000000000000000..eb1c8de37171df0561fa5a5625ce6c18c8189d2d GIT binary patch literal 2947 zcmcImc{CK>9-m=|VI+()_N)<=tt3lj$u`-yY*WToV+q+AA)*Yj80l)wYgByVAK_Mz*@BnFN=x%BXkYjK*00;;Ju>6rQN((6XU#<_7 z1u*|>o(TX*@B)DTqk%Kh0nx8P4axuweXF(fQ?*# zI(A#wZm5y+>1gnihx>=^eD#yzM=kgFL=a*?9x-4tD9oWw&r(u{Ns^nph(Hie1+d%6 z=pf#vWUv~dL9*u`Ff-OB*v;%JB}%U|3}B8K0DCGrh>4G%cFQXjbPL_yuEub?769VA zu3Eq(fjo?DApe(bxXv>1tR?9TG?}(hyNtEZ>j}=7iT;HVR5pSM>Sv_{^a#iF6o#9A z{8j8_x}kWN^bm6`(nPQzPFUQT2E_NQzK6+gv5ZQGjxOATR&<}eZZ%Oke`dvd0;2rh zvmv*$efAI6W2`a1s9Yk<)#%)6;8_xE$u(+qG>s za_|s$2g_l6eRe}&Sp{0EOk3Kru(Rt9f}{#XMSSHKsE?u5o`x09M1o8@xQj zq~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