How to Build a Simple API with AI Tools in Under 2 Hours
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)
-
Install Node.js: If you haven’t already, download and install Node.js from nodejs.org.
-
Create a new project:
mkdir my-api && cd my-api npm init -y -
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.
-
Set up your
.envfile: Create a.envfile in your project root and add your OpenAI API key:OPENAI_API_KEY=your_api_key_here
Step 2: Building the API (45 minutes)
-
Create your
index.jsfile: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}`); }); -
Run your server:
node index.js -
Test your API: Use a tool like Postman or cURL to send POST requests to
http://localhost:3000/api/generatewith a JSON body:{ "prompt": "Once upon a time" }
Step 3: Deploying Your API (30 minutes)
-
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.
-
Deploying on Vercel:
- Install Vercel CLI:
npm i -g vercel - Run
verceland follow the prompts to deploy your project.
- Install Vercel CLI:
-
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
- Create a new Heroku app:
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.jsonhas 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.