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:
API Key : Switch from OpenAI key to PromptGuard key
Base URL : Route requests through PromptGuard’s secure proxy
Pre-Migration Checklist
Step-by-Step Migration
Step 1: Environment Setup
Add your PromptGuard API key to your environment. See the Quickstart for detailed setup instructions.
# Keep existing OpenAI key for rollback capability
OPENAI_API_KEY = sk-xxxxxxxxxxxxxxxx
# Add PromptGuard key
PROMPTGUARD_API_KEY = pg_live_xxxxxxxx
Step 2: Update Client Configuration
Modify your OpenAI client initialization:
Node.js (Before)
Node.js (After)
Python (Before)
Python (After)
import OpenAI from 'openai' ;
const openai = new OpenAI ({
apiKey: process . env . OPENAI_API_KEY ,
});
import OpenAI from 'openai' ;
const openai = new OpenAI ({
apiKey: process . env . PROMPTGUARD_API_KEY ,
baseURL: 'https://api.promptguard.co/api/v1'
});
from openai import OpenAI
client = OpenAI(
api_key = os.environ.get( "OPENAI_API_KEY" )
)
from openai import OpenAI
client = OpenAI(
api_key = os.environ.get( "PROMPTGUARD_API_KEY" ),
base_url = "https://api.promptguard.co/api/v1"
)
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 ;
}
}
def make_ai_request ( messages , model = "gpt-5-nano" ):
try :
completion = client.chat.completions.create(
model = model,
messages = messages
)
return {
"success" : True ,
"response" : completion.choices[ 0 ].message.content
}
except Exception as error:
error_str = str (error)
# PromptGuard-specific error handling
if "policy_violation" in error_str:
return {
"success" : False ,
"error" : "security_block" ,
"message" : "Request blocked by security policy" ,
"suggestion" : "Please rephrase your request and try again"
}
# Re-throw other errors
raise error
Step 4: Test Your Migration
Verify your core use cases work with PromptGuard:
Test basic functionality : Make a simple request
Test security features : Try a potentially malicious prompt
Test your models : Verify all models you use work correctly
See the Quickstart 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
});
const OpenAI = require ( 'openai' );
const openai = new OpenAI ({
apiKey: process . env . PROMPTGUARD_API_KEY ,
baseURL: 'https://api.promptguard.co/api/v1'
});
FastAPI / Python
from openai import OpenAI
client = OpenAI(
api_key = os.environ.get( "OPENAI_API_KEY" )
)
from openai import OpenAI
client = OpenAI(
api_key = os.environ.get( "PROMPTGUARD_API_KEY" ),
base_url = "https://api.promptguard.co/api/v1"
)
Rollback Plan
If you need to rollback, simply revert the two changes:
Change PROMPTGUARD_API_KEY back to OPENAI_API_KEY
Remove the baseURL parameter
Your code will work exactly as before.
Next Steps
Integration Guides Detailed setup for Node.js, Python, React, and more
Security Configuration Customize protection for your use case
Monitoring Dashboard Track security events and performance
Troubleshooting Common issues and solutions
Need Help?
Contact support or check our troubleshooting guide .