Project Setup Beginner
In this lesson, you will create a React project, install the AI SDK, set up a backend API endpoint, and build your first AI-powered component.
Step 1: Create a React Project
# Using Vite (recommended)
npm create vite@latest my-ai-react-app -- --template react-ts
cd my-ai-react-app
npm install
Step 2: Install Dependencies
# AI SDK with React hooks npm install ai @ai-sdk/react # Markdown rendering for AI responses npm install react-markdown remark-gfm # Code syntax highlighting (optional) npm install react-syntax-highlighter
Step 3: Set Up the Backend
You need a backend API that proxies requests to the AI provider. Here is a simple Express server:
import express from 'express'; import cors from 'cors'; import { openai } from '@ai-sdk/openai'; import { streamText } from 'ai'; const app = express(); app.use(cors()); app.use(express.json()); app.post('/api/chat', async (req, res) => { const { messages } = req.body; const result = await streamText({ model: openai('gpt-4o'), messages, }); result.pipeDataStreamToResponse(res); }); app.listen(3001, () => console.log('API running on port 3001'));
Step 4: Your First AI Component
import { useChat } from '@ai-sdk/react'; function App() { const { messages, input, handleInputChange, handleSubmit } = useChat({ api: 'http://localhost:3001/api/chat', }); return ( <div> <h1>AI Chat</h1> {messages.map(m => ( <div key={m.id}> <strong>{m.role}:</strong> {m.content} </div> ))} <form onSubmit={handleSubmit}> <input value={input} onChange={handleInputChange} /> <button type="submit">Send</button> </form> </div> ); } export default App;
Setup Complete!
Your React app is connected to an AI backend. In the next lesson, you will build polished, reusable chat components.
Next: Chat Components →Ready to Go Deeper?
Live instructor-led courses from our partners. Affiliate disclosure.
AI & ML Courses - 30% Off
Live instructor-led AI, machine learning, data science, and cloud courses for working professionals. Use code Limited30 at checkout.
EdurekaDataCamp - AI & Data Science
Hands-on Python, machine learning, and AI courses with interactive exercises and real projects.
DataCampedX - Top AI Courses
University-level AI courses from MIT, Harvard, Stanford. Earn certificates that employers recognize.
edX