What You’ll Learn
- Setting up Discord interactions in Next.js API routes
- Creating a middleware adapter for Next.js
- Handling slash commands in a serverless environment
- Working with Next.js request/response objects
Prerequisites
Before you begin, make sure you have:
- A Next.js project (v12 or higher)
- A Discord application with a public key
- Your application’s public key stored in environment variables
Complete Example
Create a file atpages/api/interaction.js:
pages/api/interaction.js
Key Concepts
Middleware Adapter
Next.js API routes don’t use Express-style middleware out of the box, so we need an adapter:Why do we need this adapter?
Why do we need this adapter?
The
verifyKeyMiddleware from discord-interactions expects Express-style request objects with:- A
header()method to access headers - A raw string body for signature verification
- Adds a
header()method that reads fromreq.headers - Converts the parsed JSON body back to a string for verification
- Wraps the middleware in a Promise for async/await usage
Handler Function
The main handler is an async function that processes the interaction:Environment Setup
Create a.env.local file in your Next.js project root:
API Route Configuration
By default, Next.js parses the request body. You may need to configure body parsing depending on your Next.js version:pages/api/interaction.js
Running Locally
Start your Next.js development server:http://localhost:3000/api/interaction.
For local development, use a tool like ngrok to expose your local server:
Configure Discord
In your Discord application settings, set the Interactions Endpoint URL to:- Development:
https://your-ngrok-url.ngrok.io/api/interaction - Production:
https://your-domain.com/api/interaction
Advanced Example
Here’s a more complete example with error handling and multiple interaction types:pages/api/interaction.js
Deployment
Vercel
Next.js API routes work seamlessly on Vercel:- Add your environment variables in the Vercel dashboard
- Deploy your application
- Update your Discord application’s interaction URL
Other Platforms
Next.js API routes are serverless functions that can be deployed to:- Vercel (recommended)
- Netlify
- AWS Lambda (via serverless-next.js)
- Any platform that supports Next.js
Important Notes
Request Body Handling
Request Body Handling
The middleware adapter converts the parsed body back to a string because
verifyKeyMiddleware needs the raw body to verify the signature. This is a quirk of how Next.js handles API routes differently from Express.Error Handling
Error Handling
Always wrap your handler in try-catch blocks. If signature verification fails, the middleware will throw an error that should be caught and handled appropriately.
Performance
Performance
API routes are serverless functions that cold-start. For better performance:
- Keep your handler function small and focused
- Use edge runtime if available (
export const config = { runtime: 'edge' }) - Consider caching frequently accessed data
Next Steps
- Learn about handling modals in Next.js
- Explore Express.js setup for a traditional server approach
- Deploy to cloud functions for a serverless architecture