Ai Coding Tools

How to Code an AI Chatbot in 2 Hours Using Cursor

By BTW Team3 min read

Comparing tools? See the AI tools tested on the show.

In short

A step-by-step guide to building your first AI chatbot with Cursor, promising a functional outcome within just two hours.

Building an AI chatbot may seem daunting, but with the right tools and a clear plan, you can have one up and running in just two hours. If you're an indie hacker, solo founder, or side project builder, you know time is money. That's where Cursor comes in. This powerful coding tool streamlines the process, allowing you to focus on building rather than getting lost in complexity.

In this guide, we’ll walk you through the steps to create an AI chatbot using Cursor in 2026. Let’s dive in!

Prerequisites: What You Need Before You Start

Before we jump into the coding, here’s what you’ll need:

  1. Cursor Account: Free tier available; Pro version starts at $20/month.
  2. Basic Programming Knowledge: Familiarity with JavaScript or Python will help.
  3. OpenAI API Key: Required for chatbot functionality (pricing varies based on usage).
  4. Text Editor: Any code editor you prefer (VS Code, Sublime, etc.).
  5. 2 Hours of Focused Time: Set aside uninterrupted time to complete the project.

Step 1: Setting Up Your Environment

  1. Create a New Project in Cursor: Open Cursor, and create a new project.

  2. Install Required Packages: Use the built-in terminal in Cursor to run the following command:

    npm install openai express dotenv
    
  3. Set Up Environment Variables: Create a .env file in your project directory and add your OpenAI API key:

    OPENAI_API_KEY=your_api_key_here
    

Expected Output: Your project structure should look like this:

/your-project
  ├── .env
  ├── package.json
  └── index.js

Step 2: Coding the Chatbot Logic

  1. Create the Main File: Open index.js and set up a basic Express server:

    const express = require('express');
    const { Configuration, OpenAIApi } = require('openai');
    require('dotenv').config();
    
    const app = express();
    const port = 3000;
    
    app.use(express.json());
    
    const configuration = new Configuration({
        apiKey: process.env.OPENAI_API_KEY,
    });
    const openai = new OpenAIApi(configuration);
    
    app.post('/chat', async (req, res) => {
        const userMessage = req.body.message;
    
        const response = await openai.createChatCompletion({
            model: "gpt-3.5-turbo",
            messages: [{ role: "user", content: userMessage }],
        });
    
        res.json(response.data.choices[0].message);
    });
    
    app.listen(port, () => {
        console.log(`Chatbot listening at http://localhost:${port}`);
    });
    

Expected Output: Your server should now be running, and you can test it using Postman or another API client.

Step 3: Testing Your Chatbot

  1. Run Your Server: In Cursor, run the command:

    node index.js
    
  2. Send a Test Request: Use Postman to send a POST request to http://localhost:3000/chat with the following JSON body:

    {
        "message": "Hello, how can I use this chatbot?"
    }
    

Expected Output: You should receive a response from the chatbot based on the input message.

Troubleshooting Common Issues

  • Error: "API key missing": Ensure your .env file is correctly set up with your OpenAI API key.
  • Server not running: Check for syntax errors in your code or ensure you’re in the correct directory.
  • Response is empty: Ensure your request body matches the expected format.

What's Next: Enhancing Your Chatbot

Now that you have a basic chatbot, consider the following enhancements:

  1. Add More Intents: Expand the chatbot’s capabilities by adding more predefined responses.
  2. Integrate with a Frontend: Use frameworks like React or Vue to create a user-friendly interface.
  3. Deploy Your Chatbot: Use services like Heroku or Vercel for deployment.

Conclusion: Start Here

Creating an AI chatbot in just two hours is entirely feasible with Cursor. This tool simplifies the coding process and allows you to focus on what matters: building a product that serves your users.

If you're looking to dive deeper into AI and coding tools, check out the Built This Week podcast for real-world insights and experiences from builders like you.

Watch real AI builds every week

Built This Week is a weekly podcast where Jordan Metzner and Sam Nadler build something real with AI every episode, test new AI tools and talk with founders who build with AI.

See the tools

Watch real AI builds every week

Built This Week is a weekly podcast where Jordan Metzner and Sam Nadler build something real with AI every episode, test new AI tools and talk with founders who build with AI.

See the tools
Ai Coding Tools

3 AI Coding Tools That Are Overrated (and Why You Should Avoid Them)

3 AI Coding Tools That Are Overrated (and Why You Should Avoid Them) In the everevolving landscape of AI coding tools, it's easy to get swept up in the hype. As indie hackers and s

Nov 30, 20263 min read
Ai Coding Tools

5 Ways AI Coding Tools Can Accelerate Your MVP Development in 2026

5 Ways AI Coding Tools Can Accelerate Your MVP Development in 2026 In 2026, the landscape for indie hackers and solo founders has evolved significantly, especially with the advent

Nov 26, 20264 min read
Ai Coding Tools

5 AI Coding Tools That Make Legacy Code Feel Brand New in 2026

5 AI Coding Tools That Make Legacy Code Feel Brand New in 2026 If you’re a developer stuck maintaining legacy code, you know the frustration. It’s like navigating a maze blindfolde

Nov 23, 20264 min read
Ai Coding Tools

10 AI Coding Tools That Will Save You Hours of Debugging in 2026

10 AI Coding Tools That Will Save You Hours of Debugging in 2026 Debugging can feel like a neverending cycle of frustration. Whether you're a solo founder or an indie hacker, spend

Nov 18, 20265 min read
Ai Coding Tools

10 Common Mistakes When Using AI Coding Tools for MVP Development

10 Common Mistakes When Using AI Coding Tools for MVP Development As a founder in 2026, you’re likely aware that AI coding tools can significantly speed up the MVP development proc

Nov 16, 20264 min read
Ai Coding Tools

5 AI Coding Myths Every Beginner Developer Should Ditch in 2026

5 AI Coding Myths Every Beginner Developer Should Ditch in 2026 As a beginner developer, you’ve probably come across a ton of buzz surrounding AI coding tools. But with all the hyp

Nov 12, 20263 min read