Skip to main content
Glama
route.ts2.22 kB
import { NextRequest, NextResponse } from 'next/server'; import { getSession } from '@/lib/auth'; import { prisma } from '@/lib/prisma'; import { generateApiKey } from '@/lib/auth'; // Get all API keys for the current user export async function GET(request: NextRequest) { try { // Check authentication const session = await getSession(); if (!session || !session.user) { return NextResponse.json( { error: 'Authentication required' }, { status: 401 } ); } if (!session.user.id) { return NextResponse.json( { error: 'Invalid user session' }, { status: 401 } ); } const userId = parseInt(session.user.id); // Fetch user's API keys const keys = await prisma.apiKey.findMany({ where: { userId }, orderBy: { createdAt: 'desc' }, select: { id: true, name: true, createdAt: true, expiresAt: true, }, }); return NextResponse.json({ keys }); } catch (error) { console.error('Error fetching API keys:', error); return NextResponse.json( { error: 'Failed to fetch API keys' }, { status: 500 } ); } } // Create a new API key export async function POST(request: NextRequest) { try { // Check authentication const session = await getSession(); if (!session || !session.user) { return NextResponse.json( { error: 'Authentication required' }, { status: 401 } ); } if (!session.user.id) { return NextResponse.json( { error: 'Invalid user session' }, { status: 401 } ); } const userId = parseInt(session.user.id); const { name } = await request.json(); if (!name) { return NextResponse.json( { error: 'API key name is required' }, { status: 400 } ); } // Generate new API key const apiKey = await generateApiKey(userId, name); return NextResponse.json({ success: true, apiKey }); } catch (error) { console.error('Error creating API key:', error); return NextResponse.json( { error: 'Failed to create API key' }, { status: 500 } ); } }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/thomasdavis/blah'

If you have feedback or need assistance with the MCP directory API, please join our Discord server