Ai Coding Tools

How to Build a Simple API with AI Tools in Under 2 Hours

By BTW Team4 min read

How to Build a Simple API with AI Tools in Under 2 Hours

Building a simple API might sound like a daunting task, especially if you’re not a seasoned developer. But in 2026, with the right AI tools at your disposal, you can whip up a functional API in under two hours—even if you’re a solo founder or indie hacker. In this guide, I’ll walk you through the process step-by-step, share specific tools that will make your life easier, and give you honest insights based on our real experiences.

Prerequisites: What You Need to Get Started

Before diving in, ensure you have the following:

  • Basic understanding of APIs: Know what an API is and how it works.
  • Node.js installed: This will be your runtime environment.
  • An account with a cloud provider: For deployment. I recommend using Vercel or Heroku.
  • Access to an AI tool: We'll use OpenAI's API for this example.

Step 1: Setting Up Your Environment (30 minutes)

  1. Install Node.js: If you haven’t already, download and install Node.js from nodejs.org.

  2. Create a new project:

    mkdir my-api && cd my-api
    npm init -y
    
  3. Install necessary packages:

    npm install express axios dotenv
    
    • Express: A web framework for Node.js.
    • Axios: For making HTTP requests.
    • Dotenv: For managing environment variables.
  4. Set up your .env file: Create a .env file in your project root and add your OpenAI API key:

    OPENAI_API_KEY=your_api_key_here
    

Step 2: Building the API (45 minutes)

  1. Create your index.js file:

    const express = require('express');
    const axios = require('axios');
    require('dotenv').config();
    
    const app = express();
    const PORT = process.env.PORT || 3000;
    
    app.use(express.json());
    
    app.post('/api/generate', async (req, res) => {
        const { prompt } = req.body;
    
        try {
            const response = await axios.post('https://api.openai.com/v1/engines/davinci/completions', {
                prompt,
                max_tokens: 100,
            }, {
                headers: {
                    'Authorization': `Bearer ${process.env.OPENAI_API_KEY}`,
                    'Content-Type': 'application/json',
                }
            });
    
            res.json(response.data);
        } catch (error) {
            res.status(500).json({ error: 'Error generating text' });
        }
    });
    
    app.listen(PORT, () => {
        console.log(`Server is running on http://localhost:${PORT}`);
    });
    
  2. Run your server:

    node index.js
    
  3. Test your API: Use a tool like Postman or cURL to send POST requests to http://localhost:3000/api/generate with a JSON body:

    {
        "prompt": "Once upon a time"
    }
    

Step 3: Deploying Your API (30 minutes)

  1. Choose your deployment platform: Vercel and Heroku are both great for deploying Node.js applications. Here’s a quick rundown:

    • Vercel: Free tier available, easy GitHub integration.
    • Heroku: Free tier available, but gets expensive with add-ons.
  2. Deploying on Vercel:

    • Install Vercel CLI: npm i -g vercel
    • Run vercel and follow the prompts to deploy your project.
  3. Deploying on Heroku:

    • Create a new Heroku app: heroku create
    • Push your code: git push heroku main
    • Set your environment variable: heroku config:set OPENAI_API_KEY=your_api_key_here

Troubleshooting: What Could Go Wrong

  • CORS Issues: If you encounter CORS errors, you might need to add middleware to handle CORS in your Express app.
  • API Key Errors: Double-check your API key and ensure it's correctly set in your environment variables.
  • Deployment Failures: Make sure your package.json has the correct start script: "start": "node index.js".

What's Next: Building on This Foundation

Now that you have a simple API running, consider these next steps:

  • Add authentication: Secure your API with authentication (e.g., JWT).
  • Expand functionality: Implement more endpoints or integrate with other APIs.
  • Monitor performance: Use tools like LogRocket or Sentry to keep track of errors in production.

Conclusion: Start Here

Building a simple API with AI tools in under two hours is not only possible but also practical for indie hackers and side project builders. Start with this foundational API, and as you become more comfortable, expand its capabilities. Remember, the key is to keep iterating and learning.

Tools We Actually Use

| Tool | Pricing | Best For | Limitations | Our Take | |----------------|----------------------------|------------------------------|---------------------------------------------|-------------------------------| | OpenAI | Free tier + $20/mo pro | Text generation | Limited tokens per request | We use it for content generation | | Express | Free | Node.js web framework | Minimal built-in features | Ideal for quick setups | | Vercel | Free tier available | Easy deployment | Limited serverless function execution time | Great for static sites | | Heroku | Free tier available | Simple app hosting | Costs can escalate with add-ons | Good for scaling if needed |

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

Cursor vs GitHub Copilot: A Deep Dive into AI Coding Assistants 2026

Cursor vs GitHub Copilot: A Deep Dive into AI Coding Assistants 2026 As indie hackers and solo founders, we often find ourselves juggling multiple roles, and coding can feel like t

Apr 19, 20264 min read
Ai Coding Tools

How to Debug JavaScript with AI Tools in Under 30 Minutes

How to Debug JavaScript with AI Tools in Under 30 Minutes Debugging JavaScript can be a frustrating experience, especially when you're racing against the clock to ship your latest

Apr 19, 20264 min read
Ai Coding Tools

The Truth: Why AI Coding Tools Don't Replace Developers

The Truth: Why AI Coding Tools Don't Replace Developers As we dive into 2026, the buzz around AI coding tools has reached a deafening crescendo. Many are touting them as the ultima

Apr 19, 20264 min read
Ai Coding Tools

How to Harness AI Coding Tools to Build Your First App in 14 Days

How to Harness AI Coding Tools to Build Your First App in 14 Days Building your first app can feel overwhelming, especially if you're not a seasoned developer. But here’s the good

Apr 19, 20266 min read
Ai Coding Tools

30-Minute Guide to Setting Up GitHub Copilot for Enhanced Coding Efficiency

30Minute Guide to Setting Up GitHub Copilot for Enhanced Coding Efficiency As indie hackers and solo founders, we often find ourselves juggling multiple projects, wearing many hats

Apr 19, 20263 min read
Ai Coding Tools

Supabase vs Firebase: Which Is Better for Real-Time Apps in 2026?

Supabase vs Firebase: Which Is Better for RealTime Apps in 2026? As a solo founder or indie hacker, choosing the right backend for your realtime application can feel overwhelming.

Apr 19, 20263 min read