The original error "Judge0 connection failed" was caused by several issues:
- Environment Variables: The code was trying to access
process.envvariables in client-side code, but these are only available server-side in Next.js - CORS Issues: Direct API calls to external services from the browser often fail due to CORS policies
- API Configuration: Hardcoded default values were being used instead of proper environment variables
The fix implements a proper Next.js architecture:
- Client-side code (
judge0.ts) now calls internal API routes instead of external APIs - Server-side API routes handle all external Judge0 API communication
- Environment variables are properly configured and accessed server-side only
- Go to RapidAPI Judge0
- Sign up for a free account
- Subscribe to the Judge0 CE (Community Edition) - it's free
- Copy your
X-RapidAPI-Keyfrom the dashboard
Create these files in your Next.js project:
pages/
api/
judge0/
submit.ts (submission endpoint)
result.ts (result fetching endpoint)
test.ts (connection testing endpoint)
- Create a
.env.localfile in your project root - Add your API configuration:
RAPIDAPI_KEY=your_actual_rapidapi_key_here
RAPIDAPI_HOST=judge0-ce.p.rapidapi.comReplace your existing files with the fixed versions:
lib/judge0.ts- Updated client-side functionscomponents/EditorFooter.tsx- Fixed component with better error handling
npm run dev
# or
yarn devyour-project/
├── .env.local (your API keys)
├── lib/
│ └── judge0.ts (client-side functions)
├── components/
│ └── EditorFooter.tsx (React component)
└── pages/
└── api/
└── judge0/
├── submit.ts (submission API route)
├── result.ts (result API route)
└── test.ts (connection test API route)
- Proper environment variable handling - API keys are secure server-side
- CORS compliance - All external API calls happen server-side
- Better error handling - Clear error messages and connection status
- Connection testing - Automatic connection verification on component mount
- Loading states - Visual feedback during execution
- Input processing - Automatic formatting for C++ test cases
- Visual connection status - Real-time connection indicator
- Detailed execution results - Performance metrics, error messages, output comparison
- Test case processing - Automatic input formatting for different data types
- Progressive polling - Efficient result fetching with backoff strategy
- Check API Key: Ensure
RAPIDAPI_KEYin.env.localis correct - Check Subscription: Verify your RapidAPI Judge0 subscription is active
- Check Network: Ensure your server can access external APIs
- Check Logs: Look at browser console and server logs for detailed error messages
- Ensure
.env.localexists in project root - Restart your development server after adding environment variables
- Check that the key name is exactly
RAPIDAPI_KEY
- Judge0 API might be experiencing high load
- Your API key might have rate limits
- Check RapidAPI dashboard for usage limits
The solution creates three internal API endpoints:
- POST /api/judge0/submit - Submit code for execution
- GET /api/judge0/result?token= - Get execution results
- GET /api/judge0/test - Test API connection
- API keys are stored server-side only
- No sensitive information is exposed to the client
- All external API calls are proxied through your Next.js backend
- Rate limiting and error handling are centralized
The current setup works for both development and production. For production deployment:
- Set environment variables in your deployment platform
- Ensure your hosting service allows outbound HTTP requests
- Monitor API usage and implement additional rate limiting if needed
If you encounter issues:
- Check the browser console for client-side errors
- Check server logs for API route errors
- Verify your Judge0 API key and subscription status
- Test the connection using the built-in test endpoint