Skip to content

Part 3: OpenAI Client Integration

This guide explains how to use the Defense API firewall with an OpenAI Python client, replacing the default base_url and api_key with your proxy configuration.


Prerequisites

  • Completed Sign-Up Process
  • Configured a Firewall in the Defense API Dashboard
  • Python 3.7+ installed
  • openai Python package installed (pip install openai)

Step 1: Typical OpenAI "Hello World" Example

from openai import OpenAI

client = OpenAI(
    api_key="sk-your-openai-key"
)

response = client.chat.completions.create(
    model="gpt-3.5-turbo",
    messages=[{"role": "user", "content": "Hello, world!"}]
)

Step 2: Replace Base URL and API Key with Defense API Firewall

To route your OpenAI requests through the Defense API firewall:

Get your Defense API Firewall URL and API Key:

  1. Log in to the Defense API Dashboard and navigate to your firewall.
  2. Navigate to the "Firewalls" section.
  3. Copy the Proxy URL and API Key for your configured firewall.

Update your Python code:

from openai import OpenAI

client = OpenAI(
    api_key="your-defense-api-proxy-key"
    base_url="your-defense-api-proxy-url"
)

response = client.chat.completions.create(
    model="gpt-3.5-turbo",
    messages=[{"role": "user", "content": "Hello, world!"}]
)

Step 3: Verify the Integration

  1. Run your Python script.
  2. Check the Defense API analytics to ensure requests are routed through the proxy.
  3. Verify that the responses are returned as expected.

Troubleshooting

  • Connection Errors? Ensure the proxy URL is correct and accessible.
  • Authentication Issues? Double-check your Defense API - API key.
  • Unexpected Responses? Verify that your firewall rules allow the intended traffic.