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

# OpenAI Proxy

## Overview

In this method, Aporia acts as a proxy, forwarding your requests to OpenAI and simultaneously invoking guardrails. The returned response is either the original from OpenAI or a modified version enforced by Aporia's policies.

This integration supports real-time applications through streaming capabilities, making it particularly useful for chatbots.

<Tip>
  If you're just getting started and your app is based on OpenAI or Azure OpenAI, **this method is highly recommended**.

  All you need to do is replace the OpenAI Base URL and add Aporia's API Key header.
</Tip>

## Prerequisites

To use this integration method, ensure you have:

1. [Created an Aporia Guardrails project.](/fundamentals/projects#creating-a-project)

## Integration Guide

### Step 1: Gather Aporia's Base URL and API Key

1. Log into the [Aporia dashboard](https://guardrails.aporia.com).
2. Select your project and click on the **Integration** tab.
3. Under Integration, ensure that **Host URL** is active.
4. Copy the **Host URL**.
5. Click on **"API Keys Table"** to navigate to your keys table.
6. Create a new API key and **save it somewhere safe and accessible**. If you lose this secret key, you'll need to create a new one.

<img src="https://mintcdn.com/aporia/ikR99If3485QaXQB/images/integration-press-table.png?fit=max&auto=format&n=ikR99If3485QaXQB&q=85&s=2f7e09bf0cbb2bb2aee6e2ea3c2d183a" className="block rounded-md" width="628" height="549" data-path="images/integration-press-table.png" />

<img src="https://mintcdn.com/aporia/ikR99If3485QaXQB/images/api-keys-table.png?fit=max&auto=format&n=ikR99If3485QaXQB&q=85&s=bf85a93475760458d3426dc02fc1b361" className="block rounded-md" width="1703" height="539" data-path="images/api-keys-table.png" />

### Step 2: Integrate into Your Code

1. Locate the section in your codebase where you use the OpenAI's API.
2. Replace the existing `base_url` in your code with the URL copied from the Aporia dashboard.
3. Add the `X-APORIA-API-KEY` header to your HTTP requests using the `default_headers` parameter provided by OpenAI's SDK.

## Code Example

Here is a basic example of how to configure the OpenAI client to use Aporia's OpenAI Proxy method:

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

  client = OpenAI(
    api_key='<your OpenAI API key>',
    base_url='<the copied base URL>',
    default_headers={'X-APORIA-API-KEY': '<your Aporia API key>'}
  )

  chat_completion = client.chat.completions.create(
    model="gpt-3.5-turbo",
    messages=[
      {
        "role": "user",
        "content": "Hello world",
      }
    ],
    user="<end-user ID>",
  )
  ```

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

  const openai = new OpenAI({
    apiKey: "<your OpenAI API key>",
    baseURL: "<the copied URL>",
    defaultHeaders: {"X-APORIA-API-KEY": "<your Aporia API key>"},
  });

  async function chat() { 
    const completion = await openai.chat.completions.create({ 
      messages: [{ role: "system", content: "You are a helpful assistant." }],
      model: "gpt-3.5-turbo",
      user: "<end-user ID>",
    });
  }
  ```

  ```javascript LangChain.js theme={null}
  import { ChatOpenAI } from "@langchain/openai";

  const model = new ChatOpenAI({
    apiKey: "<your OpenAI API key>",
    configuration: {
      baseURL: "<the copied URL>",
      defaultHeaders: {"X-APORIA-API-KEY": "<your Aporia API key>"},
    },
    user: "<end-user ID>",
  });

  const response = await model.invoke(
    "What would be a good company name a company that makes colorful socks?"
  );

  console.log(response);
  ```
</CodeGroup>

## Azure OpenAI

To integrate Aporia with Azure OpenAI, use the `X-AZURE-OPENAI-ENDPOINT` header to specify your Azure OpenAI endpoint.

<CodeGroup>
  ```python Python (Azure OpenAI) theme={null}
  from openai import AzureOpenAI

  client = AzureOpenAI(
    azure_endpoint="<Aporia base URL>/azure",  # Note the /azure!
    azure_deployment="<Azure deployment>",
    api_version="<Azure API version>",
    api_key="<Azure API key>",
    default_headers={
      "X-APORIA-API-KEY": "<your Aporia API key>",
      "X-AZURE-OPENAI-ENDPOINT": "<your Azure OpenAI endpoint>",
    }
  )

  chat_completion = client.chat.completions.create(
    model="gpt-3.5-turbo",
    messages=[
      {
        "role": "user",
        "content": "Hello world",
      }
    ],
    user="<end-user ID>",
  )
  ```
</CodeGroup>
