Skip to content
Skip to main content
Sahu4You
  • Services
  • AI Tools
  • Free Tools
  • Blog
  • About
  • Contact
Let's talk
Sahu4You
Sahu4You

Engineering modern web experiences, AI tools, and technical resources. Built by Vikas Sahu.

Stay Updated

Get web engineering insights, AI tools, and growth guides delivered straight to your inbox.

Services

  • All Services
  • E-commerce Solutions
  • Website Development
  • Mobile App Development
  • AI WhatsApp Chatbot

Resources

  • Blog Articles
  • AI Tools Directory
  • Free Utility Tools
  • Tech Guides
  • Full Forms & Wiki

Company

  • About Sahu4You
  • Portfolio & Work
  • Case Studies
  • Contact Us
  • Privacy Policy

Serve In

  • All States & Cities
  • Jaipur, Rajasthan
  • Delhi NCR
  • Mumbai, Maharashtra
  • Bangalore, Karnataka

© 2016–2026 Sahu4You. All rights reserved.

•
Privacy PolicyTerms of ServiceSitemap
hi@sahu4you.com
Table of Contents
Home
Blog
nextjs
nextjs

AI Integration Next.js: Build AI App & Next.js AI Tutorial

Learn AI integration Next.js with this complete Next.js AI tutorial. Discover how to build AI app Next.js with features like AI chatbot Next.js and Next.js 15 AI support.

Vikas Sahu
Vikas SahuAuthor
Updated Jul 30, 2026·6 min read
Feedback
Featured image for AI Integration Next.js: Build AI App & Next.js AI Tutorial

As the creator of GitaGPT and someone who's been integrating AI into web applications for years, here's your essential Next.js AI tutorial. If you want to master AI integration Next.js and learn how to build AI app Next.js, you're in the right place. We'll cover everything from building an AI chatbot Next.js to leveraging the latest Next.js 15 AI capabilities.

Why Integrate AI in Next.js?

Enhanced User Experience

  • Personalized Interactions: AI-powered chatbots and recommendations
  • Intelligent Automation: Automated responses and workflows
  • Smart Content: Dynamic content generation

Business Benefits

  • 24/7 Availability: AI doesn't sleep or take breaks
  • Scalability: Handle thousands of interactions simultaneously
  • Cost Efficiency: Reduce manual customer service costs

AI Integration Strategies

Strategy 1: Chatbot Integration

// components/AIChatbot.tsx
import { useState } from 'react';

interface Message {
  id: string;
  text: string;
  sender: 'user' | 'ai';
  timestamp: Date;
}

export default function AIChatbot() {
  const [messages, setMessages] = useState<Message[]>([]);
  const [input, setInput] = useState('');
  const [isLoading, setIsLoading] = useState(false);

  const sendMessage = async () => {
    if (!input.trim()) return;

    const userMessage: Message = {
      id: Date.now().toString(),
      text: input,
      sender: 'user',
      timestamp: new Date()
    };

    setMessages(prev => [...prev, userMessage]);
    setInput('');
    setIsLoading(true);

    try {
      const response = await fetch('/api/chat', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ message: input })
      });

      const data = await response.json();

      const aiMessage: Message = {
        id: (Date.now() + 1).toString(),
        text: data.response,
        sender: 'ai',
        timestamp: new Date()
      };

      setMessages(prev => [...prev, aiMessage]);
    } catch (error) {
      console.error('Chat error:', error);
    } finally {
      setIsLoading(false);
    }
  };

  return (
    <div className="max-w-md mx-auto bg-white rounded-lg shadow-lg">
      <div className="p-4 border-b">
        <h3 className="text-lg font-semibold">AI Assistant</h3>
      </div>

      <div className="h-96 overflow-y-auto p-4 space-y-4">
        {messages.map((message) => (
          <div
            key={message.id}
            className={`flex ${message.sender= 'user' ? 'justify-end' : 'justify-start'}`}
          >
            <div
              className={`max-w-xs px-4 py-2 rounded-lg ${
                message.sender= 'user'
                  ? 'bg-blue-500 text-white'
                  : 'bg-gray-200 text-gray-800'
              }`}
            >
              {message.text}
            </div>
          </div>
        ))}

        {isLoading && (
          <div className="flex justify-start">
            <div className="bg-gray-200 text-gray-800 px-4 py-2 rounded-lg">
              Typing...
            </div>
          </div>
        )}
      </div>

      <div className="p-4 border-t">
        <div className="flex gap-2">
          <input
            type="text"
            value={input}
            onChange={(e)=> setInput(e.target.value)}
            onKeyPress={(e)=> e.key= 'Enter' && sendMessage()}
            placeholder="Type your message..."
            className="flex-1 px-3 py-2 border rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500"
          />
          <button
            onClick={sendMessage}
            disabled={isLoading}
            className="px-4 py-2 bg-blue-500 text-white rounded-lg hover:bg-blue-600 disabled:opacity-50"
          >
            Send
          </button>
        </div>
      </div>
    </div>
  );
}

Strategy 2: API Route Implementation

// pages/api/chat.ts
import type { NextApiRequest, NextApiResponse } from "next";

export default async function handler(
  req: NextApiRequest,
  res: NextApiResponse,
) {
  if (req.method !== "POST") {
    return res.status(405).json({ error: "Method not allowed" });
  }

  try {
    const { message } = req.body;

    if (!message || message.trim().length === 0) {
      return res.status(400).json({ error: "Message is required" });
    }

    // Call AI service (example with OpenAI)
    const aiResponse = await callAIService(message);

    res.status(200).json({ response: aiResponse });
  } catch (error) {
    console.error("Chat API error:", error);
    res.status(500).json({ error: "Internal server error" });
  }
}

async function callAIService(message: string): Promise<string> {
  const response = await fetch("https://api.openai.com/v1/chat/completions", {
    method: "POST",
    headers: {
      Authorization: `Bearer ${process.env.OPENAI_API_KEY}`,
      "Content-Type": "application/json",
    },
    body: JSON.stringify({
      model: "gpt-3.5-turbo",
      messages: [
        { role: "system", content: "You are a helpful AI assistant." },
        { role: "user", content: message },
      ],
      max_tokens: 150,
      temperature: 0.7,
    }),
  });

  const data = await response.json();
  return data.choices[0].message.content;
}

AI Service Integration Examples

1. OpenAI Integration

// lib/openai.ts
import OpenAI from "openai";

const openai = new OpenAI({
  apiKey: process.env.OPENAI_API_KEY,
});

export async function generateContent(prompt: string) {
  try {
    const completion = await openai.chat.completions.create({
      model: "gpt-3.5-turbo",
      messages: [{ role: "user", content: prompt }],
      max_tokens: 500,
    });

    return completion.choices[0].message.content;
  } catch (error) {
    console.error("OpenAI error:", error);
    throw new Error("Failed to generate content");
  }
}

2. GitaGPT-Style Integration

// lib/gitagpt.ts
interface GitaGPTRequest {
  question: string;
  avatar: "bal-krishna" | "kanha" | "krishna" | "parthsarthi";
  context?: string;
}

export async function getGitaGPTResponse(request: GitaGPTRequest) {
  try {
    const response = await fetch(`${process.env.GITAGPT_API_URL}/chat`, {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
        Authorization: `Bearer ${process.env.GITAGPT_API_KEY}`,
      },
      body: JSON.stringify(request),
    });

    const data = await response.json();
    return data.response;
  } catch (error) {
    console.error("GitaGPT error:", error);
    throw new Error("Spiritual guidance unavailable");
  }
}

Performance Optimization

Caching AI Responses

// lib/aiCache.ts
import { Redis } from "ioredis";

const redis = new Redis(process.env.REDIS_URL);

export async function getCachedAIResponse(key: string) {
  return await redis.get(key);
}

export async function cacheAIResponse(
  key: string,
  response: string,
  ttl: number = 3600,
) {
  await redis.setex(key, ttl, response);
}

// Usage in API route
export default async function handler(
  req: NextApiRequest,
  res: NextApiResponse,
) {
  const { message } = req.body;
  const cacheKey = `ai:${Buffer.from(message).toString("base64")}`;

  // Check cache first
  const cached = await getCachedAIResponse(cacheKey);
  if (cached) {
    return res.json({ response: cached });
  }

  // Generate new response
  const aiResponse = await callAIService(message);

  // Cache the response
  await cacheAIResponse(cacheKey, aiResponse);

  res.json({ response: aiResponse });
}

Security Considerations

Input Validation

// lib/validation.ts
import { z } from "zod";

const chatSchema = z.object({
  message: z.string().min(1).max(1000),
  context: z.string().optional(),
  userId: z.string().optional(),
});

export function validateChatInput(input: unknown) {
  return chatSchema.parse(input);
}

Rate Limiting

// lib/rateLimit.ts
import rateLimit from "express-rate-limit";

export const aiRateLimit = rateLimit({
  windowMs: 15 * 60 * 1000, // 15 minutes
  max: 100, // limit each IP to 100 requests per windowMs
  message: "Too many AI requests, please try again later.",
});

Real-World Examples

E-commerce AI Assistant

// components/EcommerceAI.tsx
export default function EcommerceAI() {
  const [query, setQuery] = useState('');
  const [suggestions, setSuggestions] = useState([]);

  const handleQuery = async () => {
    const response = await fetch('/api/ecommerce-ai', {
      method: 'POST',
      body: JSON.stringify({ query }),
    });

    const data = await response.json();
    setSuggestions(data.suggestions);
  };

  return (
    <div className="p-4">
      <h3 className="text-lg font-semibold mb-4">Shopping Assistant</h3>
      <input
        type="text"
        value={query}
        onChange={(e)=> setQuery(e.target.value)}
        placeholder="What are you looking for?"
        className="w-full px-3 py-2 border rounded"
      />
      <button
        onClick={handleQuery}
        className="mt-2 px-4 py-2 bg-blue-500 text-white rounded"
      >
        Ask AI
      </button>

      {suggestions.length > 0 && (
        <div className="mt-4">
          <h4 className="font-medium mb-2">AI Suggestions:</h4>
          <ul className="space-y-2">
            {suggestions.map((suggestion, index) => (
              <li key={index} className="p-2 bg-gray-50 rounded">
                {suggestion}
              </li>
            ))}
          </ul>
        </div>
      )}
    </div>
  );
}

Deployment Considerations

Environment Variables

# .env.local
OPENAI_API_KEY=your_openai_api_key
AI_API_URL=your_ai_service_url
AI_API_KEY=your_ai_service_key
REDIS_URL=your_redis_url

Vercel Configuration

// vercel.json
{
  "functions": {
    "pages/api/chat.ts": {
      "maxDuration": 30
    }
  },
  "env": {
    "OPENAI_API_KEY": "@openai_api_key"
  }
}

Conclusion

AI integration in Next.js opens up endless possibilities for creating intelligent, user-friendly applications. Start small, optimize performance, and test thoroughly.

Key Takeaways:

  1. Start Small: Begin with simple AI features and expand
  2. Optimize Performance: Use caching for better UX
  3. Security First: Validate inputs and implement rate limiting
  4. Cost Management: Monitor API usage and optimize prompts
  5. Test Thoroughly: Ensure AI features work reliably

If you're ready to add AI to your Next.js application, I can help you implement the right solution for your specific needs. Get in touch to discuss your AI integration requirements.


Need help implementing AI features in your Next.js app? Contact me for expert guidance.

Related Guides & Deep Dives

Handpicked for you
WordPress to Next.js Migration
nextjs·Sep 2, 2025

WordPress to Next.js Migration

Learn how to migrate from WordPress to Next.js with this comprehensive guide. Discover when to migrate, planning strategies, and step-by-step implementation.

Read →
Spice Money Login: B2B Web Portal, App Download & Guide (2026)
finance·Oct 23, 2026

Spice Money Login: B2B Web Portal, App Download & Guide (2026)

Complete guide to Spice Money login on the official B2B web portal (b2b.spicemoney.com). Learn desktop web login, Adhikari app download, RD Service setup, and CSC VLE integration.

Read →
Aadhaar Card Download: How to Download e-Aadhaar PDF Online (2026)
guide·Oct 13, 2026

Aadhaar Card Download: How to Download e-Aadhaar PDF Online (2026)

Aadhaar card download from UIDAI in 3 ways — myaadhaar.uidai.gov.in, mAadhaar app, and DigiLocker. Step-by-step guide covering e-Aadhaar PDF password, Aadhaar update, biometric lock, and troubleshooting.

Read →

Did you find this guide helpful?

Your feedback helps us keep our guides accurate and actionable.

Related Topics
AI integration Next.jsNext.js AI tutorialbuild AI app Next.jsAI chatbot Next.jsNext.js 15 AI
Feedback
Vikas Sahu
Written byVikas SahuAuthor

Founder & Lead Engineer at Sahu4You (since 2016). 10+ years building high-performance Next.js web applications, custom software, and AI solutions for businesses across India.

Explore Agency Services & Consulting →·Follow·More stories from Vikas Sahu →
Keep Reading
Previous Story

Aaj Ka Rashifal 2026: Daily Horoscope in Hindi & Dainik Rashifal

Apr 11, 2026
Next Story

10 Android Tips and Tricks 2026: Speed, Battery & Privacy

Sep 1, 2025
Explore Topic

Want to learn more about nextjs?

Check out all guides, reviews, and insights in our nextjs collection.

See All nextjs Guides

In this story

Found this helpful?

Share this guide with friends or bookmark it to your personal reading list.