By the end of this guide, you’ll have PromptGuard protecting your React and Next.js applications. You’ll learn how to set up secure API routes that keep your credentials safe while providing a seamless user experience.
What you’ll learn
In this guide, you’ll learn how to:- Set up PromptGuard with Next.js API routes (App Router and Pages Router)
- Create secure server-side proxies for AI requests
- Handle security blocks gracefully in React components
- Implement proper error handling and loading states
- Set up environment variables securely
- Deploy your protected application
Prerequisites
Before you begin, make sure you have:- ✅ React 18+ or Next.js 13+ installed
- ✅ A PromptGuard account and API key (see Authentication)
- ✅ An existing OpenAI API key (PromptGuard uses a pass-through model)
- ✅ An existing React or Next.js project with OpenAI integration
Security-First Architecture
The recommended architecture for React/Next.js apps:Next.js Integration
API Routes with PromptGuard
Create secure API routes that proxy to PromptGuard:Streaming API Routes
Support real-time streaming responses:Environment Configuration
Configure your Next.js environment securely:React Frontend Components
Basic Chat Component
Create a secure chat interface:Custom Hooks for AI Integration
Create reusable hooks for AI functionality:Server-Side Rendering (SSR)
Handle AI content generation during SSR:Error Boundaries
Create error boundaries for AI-powered components:Performance Optimization
Optimize your React/Next.js app for AI workloads:Using the PromptGuard SDK
For server-side API routes, you can also use the native PromptGuard Node.js SDK which provides additional features beyond the OpenAI proxy:The PromptGuard SDK should only be used in server-side code (API routes, server components). Never expose your API key in client-side code. See the Node.js SDK Reference for all available methods.
Deployment Considerations
Vercel Deployment
Deploy your PromptGuard-powered Next.js app to Vercel:Environment-Specific Configuration
Next Steps
Migration Guide
Complete migration from OpenAI to PromptGuard
Security Configuration
Customize protection for your application
Monitoring Dashboard
Track security events and performance
API Reference
Complete API documentation
Troubleshooting
API Routes Not Working
API Routes Not Working
Solutions:
- Verify
PROMPTGUARD_API_KEYin environment variables - Check API route file paths and export syntax
- Ensure Next.js version compatibility (13+ for App Router)
Streaming Responses Cut Off
Streaming Responses Cut Off
Solutions:
- Increase function timeout in deployment settings
- Check for proxy timeouts in production
- Verify SSE header configuration
High Latency in Development
High Latency in Development
Solutions:
- Enable request caching for repeated queries
- Use development mode optimizations
- Consider connection pooling for high traffic
Security Blocks in Production
Security Blocks in Production
Solutions:
- Review security policy settings in dashboard
- Implement graceful error handling for blocks
- Consider adjusting security level for your use case