Skip to main content
Migrating to PromptGuard is designed to be seamless. This guide walks you through migrating any existing OpenAI integration with minimal code changes and zero downtime.

Migration Overview

PromptGuard acts as a secure proxy that’s 100% compatible with OpenAI’s API. The migration typically requires changing just 2 lines of code:
  1. API Key: Switch from OpenAI key to PromptGuard key
  2. Base URL: Route requests through PromptGuard’s secure proxy

Pre-Migration Checklist

  • OpenAI API integration currently working
  • PromptGuard account created (sign up)
  • PromptGuard API key obtained (get one here)
  • Development environment for testing

Step-by-Step Migration

Step 1: Environment Setup

Add your PromptGuard API key to your environment. See Authentication for detailed setup instructions.
.env
# Keep existing OpenAI key for rollback capability
OPENAI_API_KEY=sk-xxxxxxxxxxxxxxxx

# Add PromptGuard key
PROMPTGUARD_API_KEY=your_api_key_here

Step 2: Update Client Configuration

Modify your OpenAI client initialization:
import OpenAI from 'openai';

const openai = new OpenAI({
  apiKey: process.env.OPENAI_API_KEY,
});
That’s it! Your existing code works unchanged.

Step 3: Update Error Handling

Enhance your error handling to account for PromptGuard’s security features:
async function makeAIRequest(messages, model="gpt-5-nano") {
  try {
    const completion = await openai.chat.completions.create({
      model,
      messages
    });

    return {
      success: true,
      response: completion.choices[0].message.content
    };

  } catch (error) {
    // PromptGuard-specific error handling
    if (error.message.includes('policy_violation')) {
      return {
        success: false,
        error: 'security_block',
        message: 'Request blocked by security policy',
        suggestion: 'Please rephrase your request and try again'
      };
    }

    // Re-throw other errors
    throw error;
  }
}

Step 4: Test Your Migration

Verify your core use cases work with PromptGuard:
  1. Test basic functionality: Make a simple request
  2. Test security features: Try a potentially malicious prompt
  3. Test your models: Verify all models you use work correctly
See First Request for testing examples.

Step 5: Monitor Your Migration

After migrating, monitor your requests in the dashboard:
  • View security events and blocked requests
  • Monitor latency and performance
  • Track usage and costs
PromptGuard adds minimal latency (typically ~0.15s). Monitor your dashboard to see actual performance impact.

Framework-Specific Examples

Express.js / Node.js

const OpenAI = require('openai');

const openai = new OpenAI({
  apiKey: process.env.OPENAI_API_KEY
});

FastAPI / Python

from openai import OpenAI

client = OpenAI(
    api_key=os.environ.get("OPENAI_API_KEY")
)

Rollback Plan

If you need to rollback, simply revert the two changes:
  1. Change PROMPTGUARD_API_KEY back to OPENAI_API_KEY
  2. Remove the baseURL parameter
Your code will work exactly as before.

Next Steps

Need Help?

Contact support or check our troubleshooting guide.