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

# Secure OpenAI-compatible SDKs with the LLM Gateway

> Point any OpenAI-compatible SDK at Aptible's LLM Gateway for a compliant, audited path to your models.

## Overview

This guide walks through creating an LLM Key and configuring an OpenAI-compatible SDK to call Aptible's LLM Gateway instead of OpenAI directly, so every request is authenticated, audited, and covered by your organization's [Model Access Policy](/docs/llm-gateway/model-access-policies) and [Spend Limit](/docs/llm-gateway/cost-control).

## Create an LLM Key

<Steps>
  <Step title="Start your trial or sign in">
    [Sign up](https://app.aptible.com/signup) for or [sign in](https://app.aptible.com/login) to your Aptible account. Then navigate to [LLM Gateway](https://app.aptible.com/llm-gateway) to activate your trial. You'll receive \$10 of LLM credits to get you started, which can be used with any models your organization allows.

    <Warning>
      **For those processing PHI:** you'll need to execute a BAA before you can start processing PHI using the LLM Gateway. Go to **Settings > [Agreements](https://app.aptible.com/settings/legal/agreements)** to execute one.
    </Warning>
  </Step>

  <Step title="Create an LLM key">
    Click **Create Key** on the [LLM Gateway](https://app.aptible.com/llm-gateway) page and give it a descriptive name so you can identify its usage later when reviewing costs and request history.

    LLM Keys are scoped to your whole organization — any member can create and manage them, and a single key gives you access to every model your organization's [model access policy](/docs/llm-gateway/model-access-policies) allows.

    Once created, copy the key's token and endpoint URL. You won't be able to see the token again after you close this window.

    <Tip>
      If a key's token is ever lost or compromised, revoke it from [LLM Gateway](https://app.aptible.com/llm-gateway) and create a new one — see [LLM Keys](/docs/llm-gateway/llm-keys) for details.
    </Tip>
  </Step>
</Steps>

## Configure your SDK

Point your SDK's base URL at the LLM Gateway's OpenAI-compatible endpoint, `https://llm-gateway.aptible.com/openai`, and use your LLM Key as the API key. See [Supported APIs](/docs/llm-gateway/supported-apis) for the full list of OpenAI-compatible endpoints.

<CodeGroup>
  ```python Python theme={null}
  from openai import OpenAI

  client = OpenAI(
      api_key="<YOUR_LLM_KEY>",
      base_url="https://llm-gateway.aptible.com/openai/v1",
  )

  response = client.chat.completions.create(
      model="gpt-5",
      messages=[
          {"role": "user", "content": "Tell me a joke about cloud security"}
      ],
  )

  print(response.choices[0].message.content)
  ```

  ```javascript Node.js theme={null}
  import OpenAI from "openai";

  const client = new OpenAI({
    apiKey: "<YOUR_LLM_KEY>",
    baseURL: "https://llm-gateway.aptible.com/openai/v1",
  });

  const response = await client.chat.completions.create({
    model: "gpt-5",
    messages: [
      { role: "user", content: "Tell me a joke about cloud security" },
    ],
  });

  console.log(response.choices[0].message.content);
  ```

  ```bash curl theme={null}
  curl https://llm-gateway.aptible.com/openai/v1/chat/completions \
    -H "Authorization: Bearer <YOUR_LLM_KEY>" \
    -H "Content-Type: application/json" \
    -d '{
      "model": "gpt-5",
      "messages": [
        {"role": "user", "content": "Tell me a joke about cloud security"}
      ]
    }'
  ```
</CodeGroup>

Replace `"gpt-5"` with any model ID from [Supported Models](/docs/llm-gateway/supported-models) that your organization's [Model Access Policy](/docs/llm-gateway/model-access-policies) allows.

<Tip>
  Environment variables also work, if your SDK or framework reads them: set `OPENAI_API_KEY` to your LLM Key and `OPENAI_BASE_URL` to `https://llm-gateway.aptible.com/openai/v1`.
</Tip>

## Verify it's working

Run the snippet above. If you get a response back, requests are flowing through the LLM Gateway. You can confirm this by checking the key’s request history in [LLM Gateway](https://app.aptible.com/llm-gateway) to confirm, or see [Audit Logging](/docs/llm-gateway/audit-logging) for more ways to review your request.

## Troubleshooting

<AccordionGroup>
  <Accordion title="401 Unauthorized">
    Double-check your API key is set to a valid, unrevoked LLM Key, and that the base URL points at `https://llm-gateway.aptible.com/openai/v1` (or `https://llm-gateway.aptible.com/openai`, depending on whether your SDK appends `/v1` itself).
  </Accordion>

  <Accordion title="400 for an unrecognized or blocked model">
    Check that the model you're requesting is a valid model ID from [Supported Models](/docs/llm-gateway/supported-models), and that it's allowed by your organization's [Model Access Policy](/docs/llm-gateway/model-access-policies).
  </Accordion>

  <Accordion title="402 Payment Required">
    Your organization has reached its monthly spend limit. See [Cost Visibility & Control](/docs/llm-gateway/cost-control).
  </Accordion>
</AccordionGroup>
