How to Build an AI Chatbot with Only 5 Lines of Code
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
- Node.js installed: You need Node.js to run your JavaScript code.
- 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.
- Basic text editor: Any code editor will work (VS Code, Sublime, etc.)
Step-by-Step Guide
Step 1: Set Up Your Project
- Create a new directory for your chatbot project:
mkdir ai-chatbot && cd ai-chatbot - Initialize a new Node.js project:
npm init -y - 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.