> ## Documentation Index
> Fetch the complete documentation index at: https://docs.switchagents.ai/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> These docs moved from docs.flintai.dev to docs.switchagents.ai. Use docs.switchagents.ai for every link and request.
> To search these docs from an AI tool, connect the MCP server at https://docs.switchagents.ai/mcp. The page index is at https://docs.switchagents.ai/llms.txt.
> The source code for these products is public on GitHub. Switch Rooms: https://github.com/sandbox-quantum/switch. Switch Trust CLI: https://github.com/sandbox-quantum/switch-trust-cli. Switch Trust CodeScan action: https://github.com/sandbox-quantum/switch-trust-codescan-action. Switch Trust MCP server: https://github.com/sandbox-quantum/switch-trust-mcp. Switch Trust Python SDK: https://github.com/sandbox-quantum/switch-trust-sdk-py. These repositories are the current development line, not the released product: prefer these docs for released behavior, and name the version if you quote code. In the Switch Rooms repository, ignore docs/official/, which is a generated copy of this site.

# Configuration

> Configure gateway URL, API keys, and environment variables

The Switch Trust Python SDK needs a **gateway URL** and a **Switch Trust API key** to route traffic through the guardrails proxy. You can provide them as function parameters, environment variables, or in a `.env` file. Your LLM client keeps using its own provider key.

## Environment variables

Set these variables in your shell or deployment environment to avoid hard coding credentials:

| Variable | Required | Description |
| - | - | - |
| `SWITCH_TRUST_GATEWAY_URL` | Yes | Guardrails proxy URL |
| `SWITCH_TRUST_API_KEY` | Yes | Your Switch Trust API key |
| `SWITCH_TRUST_LLM_API_KEY` | No | An LLM provider API key to forward to the gateway as `X-LLM-API-Key`, in place of the key your client sends. See [LLM provider API key](#llm-provider-api-key). |
| `SWITCH_TRUST_POLICY_ID` | No | Guardrails policy ID to enforce on requests |
| `SWITCH_TRUST_ALLOWED_GATEWAY_HOSTS` | No | Comma-separated allowlist of permitted gateway hostnames. Defaults to `api.switchagents.ai`, `api.flintai.dev`, and `app.flintai.dev`. The `*` wildcard also requires `allow_insecure_gateway=True` in code. |
| `AGENT_ID` | No | Overrides the id the platform derives from `AGENT_NAME`. Leave this unset in most cases. If you do set it, use the exact ID shown on the agent's page. A mismatched value breaks evaluation lookups for this agent. |
| `AGENT_NAME` | No | Agent name attached to guardrails requests |

When environment variables are set, you can call `wrap()` without passing credentials:

```python theme={null}
import openai
import switch_trust

client = openai.OpenAI()
client = switch_trust.wrap(client)
```

### `.env` file support

To read settings from a `.env` file, install the `[dotenv]` extra:

```bash theme={null}
pip install "switch-trust-sdk-py[dotenv]"
```

Then pass `load_dotenv=True` to read `.env` from the current working directory, or pass a path to read a specific file:

```bash title=".env" theme={null}
SWITCH_TRUST_GATEWAY_URL=https://api.switchagents.ai
SWITCH_TRUST_API_KEY=your-switch-trust-api-key
SWITCH_TRUST_LLM_API_KEY=your-llm-api-key
```

```python theme={null}
client = switch_trust.wrap(client, load_dotenv=True)
```

### Precedence

When the same setting is provided in multiple places, the SDK uses this order:

1. **Explicit parameters** passed to `wrap()` or `init()`
2. **Environment variables** (`SWITCH_TRUST_*`), including any loaded from a `.env` file with `load_dotenv`
3. **The key on the client** (`client.api_key`), for the LLM provider key only, in `wrap()` with `forward_llm_key=True`

## Gateway URL

The gateway URL is your Switch Trust guardrails proxy endpoint. Find it in [Switch Trust](https://app.switchagents.ai):

1. Navigate to **Agents** and select your agent
2. Open the **Sessions** tab
3. The gateway URL is shown in the code snippet

> [!NOTE]
>
> The gateway URL must use `https://`, except for a loopback gateway (`localhost`, `127.0.0.1`, or `::1`) during local development, which can also use plaintext `http://`. Any loopback gateway, over `https://` or `http://`, needs `allow_insecure_gateway=True` passed to `init()` or `wrap()`. The SDK warns when using HTTP, even on loopback.

### Gateway host allowlist

By default, the SDK only allows connections to `api.switchagents.ai`, `api.flintai.dev`, and `app.flintai.dev`. This prevents credentials from being accidentally sent to an unintended endpoint. To use a self-hosted gateway, set `SWITCH_TRUST_ALLOWED_GATEWAY_HOSTS`:

```bash theme={null}
# Single host
export SWITCH_TRUST_ALLOWED_GATEWAY_HOSTS=gateway.yourcompany.com

# Multiple hosts
export SWITCH_TRUST_ALLOWED_GATEWAY_HOSTS=gateway.yourcompany.com,gateway-staging.yourcompany.com

# Allow any host (use with caution)
export SWITCH_TRUST_ALLOWED_GATEWAY_HOSTS=*
```

To allow a loopback gateway or the `*` wildcard, pass `allow_insecure_gateway=True` in code. The environment and a `.env` file can't turn either on. Without it, the SDK raises an error when it reads the configuration.

## API keys

### Switch Trust API key

Create and manage API keys in [Switch Trust](https://app.switchagents.ai): navigate to **Settings**, then select **API keys**. Give the key the **Ingest only** role, or **Editor** or higher. The gateway refuses a **Viewer** key.

> [!WARNING]
>
> Copy your API key immediately when created — it is only shown once.

Pass the key directly or set the `SWITCH_TRUST_API_KEY` environment variable:

```python theme={null}
import switch_trust

# Pass directly
client = switch_trust.wrap(client, api_key="your-switch-trust-api-key", gateway_url="https://api.switchagents.ai")

# Or use environment variable
# export SWITCH_TRUST_API_KEY=your-switch-trust-api-key
client = switch_trust.wrap(client, gateway_url="https://api.switchagents.ai")
```

### LLM provider API key

Your client's own provider key reaches the provider through the gateway, as it would without the SDK. The gateway holds no provider credentials of its own, so each request uses a key you supply.

To send a different key from the one your client has, forward one. A forwarded key replaces the client's. Use one of these:

* **`llm_api_key`:** Pass the key. Use this to forward a key for Google GenAI, whose client doesn't expose `api_key` as an attribute.
* **`forward_llm_key=True`:** Take the key from `client.api_key` and forward it. `wrap()` only.
* **`SWITCH_TRUST_LLM_API_KEY`:** Set the environment variable.

```python theme={null}
# Explicit key (the way to forward one for Google GenAI)
client = switch_trust.wrap(client, llm_api_key="your-gemini-api-key", ...)

# Auto-extract from client (OpenAI, Anthropic)
client = switch_trust.wrap(client, forward_llm_key=True, ...)
```

## Policy ID

A policy ID tells the gateway which [guardrails policy](https://docs.switchagents.ai/switch-trust/guardrails/policies.md) to enforce. Policies apply input/output detectors that can block, redact, or alert on unsafe content.

```python theme={null}
client = switch_trust.wrap(
    client,
    gateway_url="https://api.switchagents.ai",
    api_key="your-switch-trust-api-key",
    policy_id="your-policy-id",
)
```

You can also set this via the `SWITCH_TRUST_POLICY_ID` environment variable.

## Fail-closed behavior

The SDK defaults to `require_guardrails=True`. If guardrails configuration is missing or can't be applied, for example because of missing credentials or an unrecognized client type, the SDK raises `SwitchTrustGuardrailsError` instead of sending traffic unguarded.

To allow operation without guardrails (for example, in local development), pass `require_guardrails=False`:

```python theme={null}
client = switch_trust.wrap(client, require_guardrails=False)
```

> [!NOTE]
>
> With `require_guardrails=False`, the SDK logs a warning when guardrails are not configured but still wraps the client and allows it to make calls directly to the provider.

## Next steps

> [card] [Usage](https://docs.switchagents.ai/switch-trust/sdk/python/usage.md)
>
> Wrapping patterns, advanced usage, and best practices

> [card] [Integrations](https://docs.switchagents.ai/switch-trust/sdk/python/integrations.md)
>
> Provider-specific setup for OpenAI, Anthropic, Google GenAI, LangChain, and ADK
