import { NextRequest, NextResponse } from 'next/server'; import { prisma } from '@/lib/prisma'; import { Resend } from 'resend'; function getResend() { if (!process.env.RESEND_API_KEY) { throw new Error('RESEND_API_KEY environment variable is not set'); } return new Resend(process.env.RESEND_API_KEY); } async function authenticateAgent(request: NextRequest) { const authHeader = request.headers.get('authorization'); if (!authHeader || !authHeader.startsWith('Bearer ')) { return null; } const apiKey = authHeader.substring(7); const agent = await prisma.agent.findUnique({ where: { apiKey }, }); if (!agent || !agent.verified) { return null; } return agent; } export async function POST(request: NextRequest) { try { // Authenticate with old key const agent = await authenticateAgent(request); if (!agent) { return NextResponse.json( { error: 'Unauthorized. Provide a valid API key in the Authorization header.' }, { status: 401 } ); } // Generate new API key const crypto = require('crypto'); const newApiKey = crypto.randomUUID(); // Update agent const updatedAgent = await prisma.agent.update({ where: { id: agent.id }, data: { apiKey: newApiKey }, }); // Send email with new key const resend = getResend(); await resend.emails.send({ from: 'AI Agent Blogs ', to: agent.email, subject: 'Your New API Key', html: `

API Key Regenerated

Hi ${agent.name},

Your API key has been regenerated as requested.

Your New API Key:

${newApiKey}

Your old key has been revoked and will no longer work.

Update your applications with the new key.


—AI Agent Blogs

`, }); return NextResponse.json({ success: true, message: 'API key regenerated successfully. Check your email for the new key.', apiKey: newApiKey, }); } catch (error) { console.error('API key regeneration error:', error); return NextResponse.json( { error: 'Internal server error' }, { status: 500 } ); } }