# Connecting to an external data source

When integrating an external data source (such as a database, file system, etc.) into your application, whether using Streamlit or FastAPI, it’s important to manage your credentials securely and establish a reliable connection. Below are the steps to connect to an external data source in a secure and efficient manner.

#### **1. Set up Credentials for the External Data Source**:

To securely store and access credentials (such as API keys, database passwords, or other sensitive information), use the [**Secrets Management**](/overview/building-apps-in-health-universe/deploying-your-app-to-health-universe/secret-management.md) feature on the Health Universe website.

**Important**: Secrets can only contain letters, numbers, or the `-` and `_` characters. They must start with a letter or number. For example, `DB_PASSWORD`, `API_KEY`, or `DATA_SOURCE_CREDENTIALS`.

#### **2. Access your secret in your code:**

Once you've created the secret, you can retrieve it securely using `os.environ.get()` in your Python code. Make sure to use uppercase for the secret name when accessing it:

```python
import os

DB_PASSWORD = os.environ.get("DB_PASSWORD")
API_KEY = os.environ.get("API_KEY")
```

#### **3. Connect to the External Data Source from your Code:**

Once you have your credentials, you can establish a connection to the external data source. The process for connecting depends on the type of data source. Below are examples for connecting to a database and an API.

* **Connecting to a Database:** To connect to a database (e.g., PostgreSQL, MySQL, etc.), you can use a library like `psycopg2` or `sqlalchemy`.
* For PostgreSQL: `pip install psycopg2`

```python
import psycopg2
import os

# Retrieve credentials from secrets management
DB_HOST = os.environ.get("DB_HOST")
DB_NAME = os.environ.get("DB_NAME")
DB_USER = os.environ.get("DB_USER")
DB_PASSWORD = os.environ.get("DB_PASSWORD")

# Connect to the database
connection = psycopg2.connect(
    host=DB_HOST,
    dbname=DB_NAME,
    user=DB_USER,
    password=DB_PASSWORD
)

# Example function to query the database
def query_database(query):
    with connection.cursor() as cursor:
        cursor.execute(query)
        result = cursor.fetchall()
    return result
```

* **Connecting to an External API:** To connect to an external API, you can use the `requests` library.
* For requests: `pip install requests`

```python
import requests
import os

# Retrieve credentials from secrets management
API_KEY = os.environ.get("API_KEY")

# Example function to call the external API
def query_api(endpoint, params=None):
    headers = {
        "Authorization": f"Bearer {API_KEY}"
    }
    response = requests.get(endpoint, headers=headers, params=params)
    if response.status_code == 200:
        return response.json()
    else:
        return {"error": "Failed to fetch data"}
```

#### **4. Use the External Data Source in Your Streamlit or FastAPI App:**

Once the connection to the external data source is established, you can easily use it within your Streamlit or FastAPI app.

* **Streamlit Example:**

```python
import streamlit as st

st.title("External Data Source Example")

query = st.text_input("Enter SQL query to execute:")
if query:
    result = query_database(query)
    st.write(result)
```

* **FastAPI Example:**

```python
from fastapi import FastAPI

app = FastAPI()

@app.get("/query_database")
async def query_database_endpoint(query: str):
    result = query_database(query)
    return {"result": result}

@app.get("/query_api")
async def query_api_endpoint(endpoint: str, params: dict = None):
    result = query_api(endpoint, params)
    return {"result": result}
```

#### Additional Notes:

* **Security Considerations**: Always retrieve your credentials securely using the Secrets Management process. Never hardcode sensitive data directly into your codebase.
* **Connection Pooling**: For databases, consider using connection pooling to optimize performance and resource usage. Libraries like `SQLAlchemy` or `psycopg2` support pooling.
* **Error Handling**: Always include error handling when working with external data sources, as network issues, incorrect credentials, or unavailable resources may cause failures.

```python
try:
    result = query_database(query)
except Exception as e:
    st.error(f"Error querying the database: {e}")
```

By following these steps, you can securely and efficiently connect to an external data source from your Streamlit or FastAPI application.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.healthuniverse.com/overview/building-apps-in-health-universe/deploying-your-app-to-health-universe/connecting-to-an-external-data-source.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
