Ai Coding Tools

How to Build an AI Chatbot with Only 5 Lines of Code

By BTW Team4 min read

How to Build an AI Chatbot with Only 5 Lines of Code (2026)

Building an AI chatbot might sound complex, but what if I told you it can be done in just five lines of code? As indie hackers, we often juggle multiple projects and want to automate tasks without diving deep into lengthy code. This guide is here to show you how to create a simple AI chatbot that can respond to user queries, leveraging existing tools and frameworks.

Time Estimate

You can finish this setup in about 30 minutes, assuming you have a basic understanding of coding and access to the necessary tools.

Prerequisites

  1. Node.js installed: You need Node.js to run your JavaScript code.
  2. An API Key: For a chatbot, we’ll be using OpenAI’s API. You can sign up for an account and get your API key from OpenAI.
  3. Basic text editor: Any code editor will work (VS Code, Sublime, etc.)

Step-by-Step Guide

Step 1: Set Up Your Project

  1. Create a new directory for your chatbot project:
    mkdir ai-chatbot && cd ai-chatbot
    
  2. Initialize a new Node.js project:
    npm init -y
    
  3. Install the OpenAI SDK:
    npm install openai
    

Step 2: Write Your Chatbot Code

Create a new file named chatbot.js and add the following code:

const { Configuration, OpenAIApi } = require("openai");
const configuration = new Configuration({ apiKey: "YOUR_API_KEY" });
const openai = new OpenAIApi(configuration);

async function chat(input) {
  const response = await openai.createChatCompletion({ model: "gpt-3.5-turbo", messages: [{ role: "user", content: input }] });
  console.log(response.data.choices[0].message.content);
}

chat("Hello, how can I help you?");

Step 3: Run Your Chatbot

In your terminal, run the chatbot with:

node chatbot.js

You should see a response from the AI based on your input.

Expected Outputs

When you run the chatbot, it should output a relevant response based on the input you provided. For example, if you input "Hello, how can I help you?", the AI might respond with suggestions on support topics.

Troubleshooting

  • Invalid API Key: Ensure your API key is correct and has permission for the OpenAI API.
  • Network Issues: Make sure you have a stable internet connection since the API calls are made over the internet.
  • Node.js Errors: Check that you have Node.js installed correctly and are using the right version.

What's Next

Once you have the basic chatbot running, consider these enhancements:

  • Integrate with a web app: Use frameworks like Express.js to create a web interface.
  • Add more conversation context: Store user messages and provide ongoing conversations.
  • Deploy your chatbot: Use platforms like Heroku or Vercel for deployment.

Tools Comparison

To help you expand your chatbot capabilities, here's a comparison of AI chatbot tools you might consider:

| Tool | Pricing | Best For | Limitations | Our Verdict | |------------------|----------------------|-------------------------------|--------------------------------------|------------------------------| | OpenAI GPT-3.5 | $0-100/month based on usage | Text-based chatbots | Limited to text, can be expensive | Great for simple chatbots | | Dialogflow | Free tier + $20/mo | Voice and text chatbots | Complexity increases with features | Good for voice integration | | Rasa | Free, self-hosted | Customizable chatbots | Requires more setup and coding | We use this for advanced bots | | Microsoft Bot Framework | Free | Enterprise-level bots | Can be overkill for small projects | Skip if you're indie | | ManyChat | Free tier + $10/mo | Marketing chatbots | Limited to Facebook and SMS | Useful for marketing funnels | | Tidio | Free tier + $18/mo | Customer service chatbots | Limited features in free tier | We use for customer support |

Conclusion

If you're looking to build a simple AI chatbot with minimal code, start with the setup we've outlined. Implementing this in just five lines of code allows you to quickly prototype and iterate on your ideas. As you become more comfortable, consider integrating more features and deploying it for real-world use.

For a deeper dive into tools and strategies for building your projects, check out our podcast, Built This Week, where we share our own experiences as builders.

Follow Our Building Journey

Weekly podcast episodes on tools we're testing, products we're shipping, and lessons from building in public.

Subscribe

Never miss an episode

Subscribe to Built This Week for weekly insights on AI tools, product building, and startup lessons from Ryz Labs.

Subscribe
Ai Coding Tools

Top 7 Advanced AI Coding Tools to Enhance Your Workflow in 2026

Top 7 Advanced AI Coding Tools to Enhance Your Workflow in 2026 In 2026, the landscape of coding is being transformed by AI tools that promise to streamline workflows, reduce error

Mar 30, 20264 min read
Ai Coding Tools

How to Write Code 2x Faster with AI Tools in 30 Minutes

How to Write Code 2x Faster with AI Tools in 30 Minutes As indie hackers and solo founders, we often find ourselves battling the clock. Writing code can be a timeconsuming process,

Mar 30, 20264 min read
Ai Coding Tools

How to Increase Your Coding Efficiency by 50% with AI Tools in 30 Minutes

How to Increase Your Coding Efficiency by 50% with AI Tools in 30 Minutes As a solo founder or indie hacker, coding can often feel like a timesink. You might be juggling multiple t

Mar 30, 20264 min read
Ai Coding Tools

Cursor vs GitHub Copilot: The 2026 Comparison for Developers

Cursor vs GitHub Copilot: The 2026 Comparison for Developers As a developer in 2026, the choices for AI coding tools can feel overwhelming. Cursor and GitHub Copilot are two of the

Mar 30, 20263 min read
Ai Coding Tools

Cursor vs GitHub Copilot: The Ultimate Showdown for Developers 2026

Cursor vs GitHub Copilot: The Ultimate Showdown for Developers 2026 As a developer, finding the right tools to streamline your coding process can feel like searching for a needle i

Mar 30, 20263 min read
Ai Coding Tools

Bolt.new vs GitHub Copilot: Which AI Coding Companion is Best for Developers?

Bolt.new vs GitHub Copilot: Which AI Coding Companion is Best for Developers? As a developer, finding the right coding companion can feel like searching for a needle in a haystack.

Mar 30, 20263 min read