DEV Community

Shahdin Salman
Shahdin Salman

Posted on

Building a Light-speed AI Chatbot with Next.js 15, TypeScript, and Gemini 2.5 Flash API

When building real-time AI capabilities into web dashboards, latency is the ultimate user experience killer. If your LLM takes 5 seconds to process a response, your retention drops.

To solve this, we recently built a production-grade infrastructure leveraging Next.js 15, TypeScript, and the Gemini 2.5 Flash model for extreme responsiveness.

Here is the step-by-step engineering breakdown of how to structure the API route and the data flow.


1. The Architecture

Instead of overloading the client-side bundle, we offload the AI orchestration to an optimized Next.js App Router API endpoint. This keeps our secure environment variables hidden and prevents CORS issues.

2. Environment Setup

First, ensure your .env.local contains your Gemini API key:

GEMINI_API_KEY=your_secure_api_key_here
Enter fullscreen mode Exit fullscreen mode
  1. The API Routing Logic (app/api/chat/route.ts) Here is how we handle incoming JSON payloads, enforce type safety using standard TypeScript interfaces, and safely return the generated stream or payload back to our UI components:
import { NextResponse } from 'next/server';
import { GoogleGenAI } from '@google/genai';

// Initialize the SDK with zero runtime overhead
const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });

export async function POST(req: Request) {
  try {
    const { messages } = await req.json();

    if (!messages || !Array.isArray(messages)) {
      return NextResponse.json({ error: 'Invalid prompt payload structure' }, { status: 400 });
    }

    // Leveraging Gemini 2.5 Flash for sub-second responses
    const response = await ai.models.generateContent({
      model: 'gemini-2.5-flash',
      contents: messages,
    });

    return NextResponse.json({ text: response.text });
  } catch (error) {
    console.error('Gemini Execution Error:', error);
    return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 });
  }
}

Enter fullscreen mode Exit fullscreen mode

Key Takeaways for Production
Type Safety First: Using TypeScript interfaces to validate token payloads prevents runtime crashes on the frontend when dynamic AI streams are rendering.

Edge Compatibility: Gemini 2.5 Flash combined with Next.js route segment configs can be deployed to global edge networks to minimize cold starts.

Let's Collaborate
At SpaceAI360, we build and deploy enterprise-grade custom automated workflows, standalone dashboard pipelines, and specialized agents. If you're looking to scale your technical infrastructure without handling the maintenance overhead, we build production-ready systems at our custom AI agents studio.

Top comments (0)