A Next.js application for experimenting with OpenAI's Realtime API, featuring voice and text interactions with GPT-4 in real-time.
- 🎤 Voice Input: Record audio and send it directly to the AI
- 💬 Text Input: Type messages for text-based interactions
- 🔊 Voice Output: Receive audio responses from the AI
- 🌐 WebSocket Connection: Real-time bidirectional communication
- 📝 Event Logging: Monitor all API events and responses
- 🎨 Modern UI: Clean, responsive interface with Tailwind CSS
- Node.js 18+ installed
- OpenAI API key with access to the Realtime API
- Modern browser with WebSocket and MediaRecorder support
-
Clone the repository:
cd ~/repos/openai-realtime-playground
-
Install dependencies:
npm install
-
Configure environment variables:
- Copy
.env.local.exampleto.env.local - Add your OpenAI API key:
OPENAI_API_KEY=your_api_key_here
- Copy
-
Run the development server:
npm run dev
-
Open the application: Visit http://localhost:3000 in your browser
- Connect: Click the "Connect" button to establish a WebSocket connection
- Interact:
- Voice: Click "Start Recording" to speak, then "Stop Recording" when done
- Text: Type a message and press Enter or click Send
- Monitor: Watch the event log to see real-time API responses
RealtimeInterface: Main UI component for user interactionsuseRealtimeAPI: Custom React hook managing WebSocket connection and audio processing- API Route: Secure server-side endpoint for generating session tokens
- Next.js 15: React framework with App Router
- TypeScript: Type-safe development
- Tailwind CSS: Utility-first styling
- WebSocket: Real-time communication
- Web Audio API: Audio recording and processing
The application uses the following Realtime API settings:
- Model:
gpt-4o-realtime-preview-2024-12-17 - Modalities: Audio and Text
- Voice: Alloy
- Turn Detection: Server VAD (Voice Activity Detection)
- Audio Format: PCM16 @ 24kHz
- Verify your API key is correctly set in
.env.local - Check that your API key has access to the Realtime API
- Ensure you're using a supported browser
- Grant microphone permissions when prompted
- Check your browser's audio settings
- Verify your microphone is working properly
- Check browser console for error messages
- Ensure you're not behind a restrictive firewall
- Try refreshing the page if connection fails
To modify the application:
- UI Changes: Edit
src/app/components/RealtimeInterface.tsx - API Logic: Modify
src/app/hooks/useRealtimeAPI.ts - Session Config: Update
src/app/api/realtime-session/route.ts - Styling: Modify Tailwind classes or
src/app/globals.css
- Push to GitHub
- Import project on Vercel
- Add environment variables
- Deploy
Ensure the platform supports:
- Node.js 18+
- WebSocket connections
- Environment variables
- Server-side API routes
- Never expose your API key in client-side code
- The session token approach ensures secure authentication
- Tokens are ephemeral and expire automatically
- All API communication happens over secure WebSocket (WSS)
MIT
For issues or questions:
- Check the troubleshooting section
- Review the browser console for errors
- Consult the OpenAI API documentation
- Open an issue on GitHub
Check out our Next.js deployment documentation for more details.