Next.js Integration
Spyglasses integrates seamlessly with Next.js applications through our official package. Follow these steps to set up Spyglasses in your Next.js project.
Installation
-
Install the package:
with yarn:
with pnpm:
-
Add your API key: Create or update your
.env.local
file with your Spyglasses API key:
Configuration
Create or update your middleware.ts
file in the root of your project:
Docker
If you deploy with Docker, set your API key in your Dockerfile or docker-compose:
Dockerfile:
docker-compose:
Environment Variables at Runtime
For platforms that inject environment variables at runtime, you can reference them directly in your middleware:
Wrapping Existing Middleware
If you have existing middleware, you can compose it with Spyglasses in two ways:
1. Middleware Chaining (Recommended)
2. Middleware Composition (Advanced)
Tip: Use chaining unless you know you need composition.
Deployment
When deploying your Next.js application, make sure to add your Spyglasses API key to your environment variables:
Vercel
- Go to Project Settings > Environment Variables
- Add
SPYGLASSES_API_KEY
- Deploy to apply changes
Netlify
- Go to Site Settings > Build & Deploy > Environment
- Add
SPYGLASSES_API_KEY
- Trigger a new deploy
AWS Amplify
- Go to App Settings > Environment Variables
- Add
SPYGLASSES_API_KEY
- Redeploy your application
Verifying Installation
After deploying your application with Spyglasses, you can verify the installation by checking your Spyglasses dashboard. If everything is set up correctly, you should start seeing data from your Next.js application.
Troubleshooting
API Key Not Found
- Verify environment variable is set
- Check for typos in variable name
- Ensure deployment includes environment variables
Middleware Not Running
- Confirm
middleware.ts
is in the correct location - Check matcher configuration
- Verify Next.js version (13.0.0 or higher required)
Performance Issues
- Enable debug mode to check for errors
- Verify matcher configuration isn't too broad
- Check network latency to collector endpoint
If you're not seeing data in your Spyglasses dashboard:
- Verify your API key is correct
- Check that the middleware is properly configured
- Ensure your deployment includes the environment variables
- Check your browser console for any errors
Need help? Contact support@spyglasses.io