import Icon from "@site/src/components/icon/icon";
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';

1. Open the **OpenRAG OpenSearch Agent** flow in the Langflow visual editor: From the **Chat** window, click <Icon name="Settings2" aria-hidden="true"/> **Settings**, click **Edit in Langflow**, and then click **Proceed**.

2. Create a [Langflow API key](https://docs.langflow.org/api-keys-and-authentication), which is a user-specific token required to send requests to the Langflow server.
This key doesn't grant access to OpenRAG.

   1. In the Langflow visual editor, click your user icon in the header, and then select **Settings**.
   2. Click **Langflow API Keys**, and then click <Icon name="Plus" aria-hidden="true"/> **Add New**.
   3. Name your key, and then click **Create API Key**.
   4. Copy the API key and store it securely.
   5. Exit the Langflow **Settings** page to return to the visual editor.

3. Click **Share**, and then select **API access** to get pregenerated code snippets that call the Langflow API and run the flow.

   These code snippets construct API requests with your Langflow server URL (`LANGFLOW_SERVER_ADDRESS`), the flow to run (`FLOW_ID`), required headers (`LANGFLOW_API_KEY`, `Content-Type`), and a payload containing the required inputs to run the flow, including a default chat input message.

   In production, you would modify the inputs to suit your application logic. For example, you could replace the default chat input message with dynamic user input.

   <Tabs>
   <TabItem value="python" label="Python">

   ```python
   import requests
   import os
   import uuid

   api_key = 'LANGFLOW_API_KEY'
   url = "http://LANGFLOW_SERVER_ADDRESS/api/v1/run/FLOW_ID"  # The complete API endpoint URL for this flow

   # Request payload configuration
   payload = {
       "output_type": "chat",
       "input_type": "chat",
       "input_value": "hello world!"
   }
   payload["session_id"] = str(uuid.uuid4())

   headers = {"x-api-key": api_key}

   try:
       # Send API request
       response = requests.request("POST", url, json=payload, headers=headers)
       response.raise_for_status()  # Raise exception for bad status codes

       # Print response
       print(response.text)

   except requests.exceptions.RequestException as e:
       print(f"Error making API request: {e}")
   except ValueError as e:
       print(f"Error parsing response: {e}")
   ```

   </TabItem>
   <TabItem value="typescript" label="TypeScript">

   ```typescript
   const crypto = require('crypto');
   const apiKey = 'LANGFLOW_API_KEY';
   const payload = {
       "output_type": "chat",
       "input_type": "chat",
       "input_value": "hello world!"
   };
   payload.session_id = crypto.randomUUID();

   const options = {
       method: 'POST',
       headers: {
           'Content-Type': 'application/json',
           "x-api-key": apiKey
       },
       body: JSON.stringify(payload)
   };

   fetch('http://LANGFLOW_SERVER_ADDRESS/api/v1/run/FLOW_ID', options)
       .then(response => response.json())
       .then(response => console.warn(response))
       .catch(err => console.error(err));
   ```

   </TabItem>
   <TabItem value="curl" label="curl">

   ```bash
   curl --request POST \
     --url 'http://LANGFLOW_SERVER_ADDRESS/api/v1/run/FLOW_ID?stream=false' \
     --header 'Content-Type: application/json' \
     --header "x-api-key: LANGFLOW_API_KEY" \
     --data '{
       "output_type": "chat",
       "input_type": "chat",
       "input_value": "hello world!"
     }'
   ```

   </TabItem>
   </Tabs>

4. Copy your preferred snippet, and then run it:

   * **Python**: Paste the snippet into a `.py` file, save it, and then run it with `python filename.py`.
   * **TypeScript**: Paste the snippet into a `.ts` file, save it, and then run it with `ts-node filename.ts`.
   * **curl**: Paste and run snippet directly in your terminal.

If the request is successful, the response includes many details about the flow run, including the session ID, inputs, outputs, components, durations, and more.

In production, you won't pass the raw response to the user in its entirety.
Instead, you extract and reformat relevant fields for different use cases, as demonstrated in the [Langflow quickstart](https://docs.langflow.org/quickstart#extract-data-from-the-response).
For example, you could pass the chat output text to a front-end user-facing application, and store specific fields in logs and backend data stores for monitoring, chat history, or analytics.
You could also pass the output from one flow as input to another flow.