Ai Coding Tools

How to Build a Simple App with GPT-4 in Under 2 Hours

By BTW Team4 min read

How to Build a Simple App with GPT-4 in Under 2 Hours

If you’re a solo founder or indie hacker looking to leverage AI in your projects, GPT-4 is a powerful tool that can help you build a functional app quickly. The thought of coding might seem daunting, but with the right approach, you can have a simple app up and running in less than two hours. Let’s dive into how you can do this, what tools you’ll need, and the potential pitfalls to watch out for.

Prerequisites: What You Need to Get Started

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

  • A basic understanding of coding: You don’t need to be a pro, but familiarity with concepts like variables and functions will help.
  • An OpenAI API key: Sign up at OpenAI and get access to GPT-4. Pricing starts at $0.003 per token, which is quite affordable for small projects.
  • A code editor: We recommend Visual Studio Code, which is free and feature-rich.
  • Node.js installed: This will allow you to run your app locally. Installation is straightforward and takes about 10 minutes.

Step 1: Setting Up Your Environment

  1. Install Node.js: Head to nodejs.org and download the latest version. Follow the installation instructions for your OS.
  2. Create a new project directory: Open your terminal and run:
    mkdir my-gpt-app
    cd my-gpt-app
    npm init -y
    
  3. Install necessary packages: You’ll need axios for API calls and express for creating a simple web server. Run:
    npm install axios express
    

Step 2: Writing Your App

Now, let’s create a simple Express server that interacts with GPT-4. Here’s a step-by-step breakdown:

  1. Create a file named server.js:

    const express = require('express');
    const axios = require('axios');
    const app = express();
    const PORT = process.env.PORT || 3000;
    
    app.use(express.json());
    
    app.post('/api/gpt', async (req, res) => {
        const userInput = req.body.input;
        try {
            const response = await axios.post('https://api.openai.com/v1/chat/completions', {
                model: 'gpt-4',
                messages: [{ role: 'user', content: userInput }]
            }, {
                headers: {
                    'Authorization': `Bearer YOUR_API_KEY`
                }
            });
            res.json(response.data.choices[0].message.content);
        } catch (error) {
            res.status(500).send('Error communicating with GPT-4');
        }
    });
    
    app.listen(PORT, () => {
        console.log(`Server is running on http://localhost:${PORT}`);
    });
    
  2. Replace YOUR_API_KEY with your actual OpenAI API key.

  3. Start your server: Run the following command in your terminal:

    node server.js
    
  4. Test your API: Use Postman or curl to send a POST request to http://localhost:3000/api/gpt with a JSON body like:

    { "input": "What is the capital of France?" }
    

Step 3: Building Your Frontend (Optional)

If you want a simple frontend to interact with your app, create an index.html file:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>GPT-4 App</title>
</head>
<body>
    <h1>Ask GPT-4</h1>
    <input type="text" id="userInput" placeholder="Type your question...">
    <button onclick="sendRequest()">Send</button>
    <p id="response"></p>

    <script>
        async function sendRequest() {
            const input = document.getElementById('userInput').value;
            const response = await fetch('/api/gpt', {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify({ input })
            });
            const data = await response.json();
            document.getElementById('response').innerText = data;
        }
    </script>
</body>
</html>

Just open this HTML file in your browser, and you can start asking questions!

What Could Go Wrong

  • API Errors: If you encounter errors when calling the OpenAI API, check your API key and usage limits.
  • CORS Issues: If you access the server from a different origin, set up CORS in your Express app (using the cors package).
  • Rate Limits: OpenAI has usage limits; be mindful of how many requests you’re making.

What's Next?

Now that you have a basic app running, consider enhancing it by:

  • Adding user authentication.
  • Storing conversations in a database (like Firebase or MongoDB).
  • Deploying your app using platforms like Heroku or Vercel.

Conclusion: Start Here

Building a simple app with GPT-4 can be done in under two hours if you follow this guide. While the basic setup is straightforward, the scalability and complexity depend on your future needs. Start with this foundational app, and expand as you learn more.

What We Actually Use

We use a mix of tools for building with GPT-4, including:

  • OpenAI API for AI functionalities.
  • Express for server-side handling.
  • Axios for API requests.

These tools keep our stack lightweight and efficient, perfect for quick iterations.

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

Why AI Coding Assistants Are Overrated for Beginners in 2026

Why AI Coding Assistants Are Overrated for Beginners in 2026 As a solo founder or indie hacker, it’s tempting to think that AI coding assistants are the silver bullet to jumpstart

Oct 10, 20263 min read
Ai Coding Tools

10 Common Mistakes When Using AI Coding Tools for Beginners

10 Common Mistakes When Using AI Coding Tools for Beginners As a beginner in coding, diving into AI coding tools can feel like a doubleedged sword. On one hand, these tools promise

Oct 8, 20264 min read
Ai Coding Tools

5 Common Misconceptions About AI Coding Tools and Why They Hold You Back

5 Common Misconceptions About AI Coding Tools and Why They Hold You Back As a solo founder or indie hacker, you might be considering leveraging AI coding tools to boost your produc

Oct 7, 20264 min read
Ai Coding Tools

5 Common Misconceptions About AI Coding Tools That Will Hurt Your Learning

5 Common Misconceptions About AI Coding Tools That Will Hurt Your Learning As a beginner in coding, diving into the world of AI coding tools can feel like stepping into a futuristi

Oct 6, 20263 min read
Ai Coding Tools

10 AI Coding Tools Beginners Overlook in 2026

10 AI Coding Tools Beginners Overlook in 2026 As a beginner in coding, diving into the vast ocean of AI tools can feel overwhelming. While flashy tools often grab the spotlight, ma

Oct 6, 20265 min read
Ai Coding Tools

5 Common Misconceptions About AI Coding Tools That Beginners Believe

5 Common Misconceptions About AI Coding Tools That Beginners Believe As a solo founder or indie hacker, diving into the world of AI coding tools can feel overwhelming, especially i

Oct 5, 20263 min read