API Integration for AI Coders
APIs let your app talk to the rest of the internet: pulling in weather, showing user profiles, processing payments, and more. Here's what you need to know.
What is an API?
Think of it like ordering at a restaurant
You (your app) tell the waiter (the API) what you want. The waiter goes to the kitchen (another company's computer) and brings back your food (the data). You never go into the kitchen yourself. The waiter handles it all.
Real-world examples
- 🌤️ A weather widget on your site pulls today's forecast from a weather service
- 💳 Your checkout page asks Stripe to process a credit card payment
- 📍 A store locator shows nearby locations using Google Maps data
- 🔐 Your login page checks credentials through an authentication service
The 4 Things You Can Do With Data
Every app that works with data does some combination of these four actions. When you hear developers say "CRUD", this is what they mean.
Read or fetch data
Show a list of blog posts
Add new data
Submit a new comment
Change existing data
Edit your profile name
Remove data
Delete a photo
Two Ways Data Shows Up in Your App
You don't need to understand the code behind these. Just know which approach to ask your AI tool for.
- Blog posts, product listings, profile pages
- Data that doesn't change every second
- Pages that need to show up well in Google
“Fetch the data on the server side so the page loads with everything already visible.”
- Search results, filters, live dashboards
- Data that updates based on what the user clicks
- Real-time features like chat or notifications
“Fetch the data on the client side with a loading spinner while it loads.”
What Can Go Wrong (and How to Handle It)
When your app asks for outside data, three things can happen. A good app handles all three gracefully.
“Add skeleton loading placeholders while the data is being fetched.”
“Display the results in a responsive card grid with the name, image, and description.”
“Add an error state with a friendly message and a retry button.”
What's an API Key?
Some APIs are open to anyone (like the free ones listed below). Others require an API key: a special password that identifies your app. It's like a library card: you need one to borrow books, and the library tracks who borrows what.
Free APIs to Practice With
These services let anyone pull data for free. Most need no sign-up or API key at all. Perfect for learning.
Prompts to Give Your AI Tool
Copy-paste these into Claude Code, Cursor, Bolt, or whichever AI tool you use. Replace the parts in [brackets] with your details.
“Fetch data from [API URL] and display it in a nice card grid. Show a loading animation while it's fetching, a friendly error message if it fails with a retry button, and a message if there are no results.”
“Add a search bar that filters results from [API URL] as the user types. Show a loading spinner while searching, highlight the matching text in results, and show 'No results found' when nothing matches.”
“Build a weather widget using the Open-Meteo API. Show the current temperature, weather condition with an icon, today's high and low, and a 7-day forecast in a row of small cards. Let the user type a city name to search.”
“Create a contact form with name, email, and message fields. When submitted, save the data to [your database or service]. Show a success message after sending and validation errors if fields are empty.”
“Fetch a list of [items] from [API URL] and show 10 at a time. Add a 'Load More' button at the bottom that fetches the next batch. Show how many total items there are and a loading indicator when fetching more.”
“Create a page that reads [data type] from my database and displays it in a table. Include sorting by each column, and a way to delete individual rows with a confirmation dialog.”
Real World Example: Weather Dashboard
Imagine you're building a weather app. When a user searches for a city, your app calls a weather API, shows the current temperature and conditions, and displays a 5-day forecast. You need to handle loading (while the API responds), errors (city not found), and display the data in a clean card layout.
“Build a weather dashboard that lets users search for a city. Use the OpenWeatherMap API (free tier). Show current temperature, weather condition with an icon, humidity, and wind speed in a card. Below it, show a 5-day forecast as small cards. Handle 3 states: loading (show a skeleton), error (show 'City not found'), and success. Use environment variables for the API key. Style with Tailwind CSS.”
Prompt This
Copy-paste these prompts into your AI tool to apply what you just learned.
“Connect my app to the [API name] API. I need to [describe what you want to do]. Show me how to make the API call and display the data.”
“Add error handling to my API calls. Show loading spinners while fetching, error messages when requests fail, and retry buttons.”
Common Questions
Do I need to know how to code to use APIs?
No! Your AI tool writes the code for you. You just need to know what you want ('show weather data on my homepage') and the AI handles the technical parts.
Is it free to use APIs?
Many APIs are free (like the ones listed above). Others are free up to a certain amount of use, then charge after that. Always check the pricing page before building something that will get lots of traffic.
What if the API I'm using goes down?
That's why error handling matters! Ask your AI tool to 'add error handling with a friendly message and retry button.' Your app should show a nice message instead of breaking.
Can I create my own API?
Yes! If you're using Next.js, your AI tool can create API endpoints right inside your project. Just say 'create an API route that [does whatever you need].' This is useful when you want your app to do something on the server, like save data or talk to a third-party service securely.