An introduction to APIs and AI agents
Fetch a dog photo
and keep the code that did it.
Ask for a dog photo and watch the reply arrive. The service sends back a web address, and your browser uses it to load the picture. Then keep a small webpage of your own that makes the same request. Each run sends a live HTTPS GET request to dog.ceo, inspects the returned JSON envelope ({status, message}), and issues a second GET request to images.dog.ceo to render the image. Keep a standalone HTML file that runs the same request.
Already curious about the code? Make your own page. / Teacher guide
What you will do
- Send a request
- Follow the tool call
- Keep a working webpage
01 / Explore
Fetch a dog photo and follow the request
Live API / scripted model
Pip is the name of the scripted agent. The API request is live; Pip’s agent steps are scripted. No AI model runs here.
live: a real request. scripted: written for this lesson.
An agent is a model calling tools in a loop until a task is done. Pip's loop here is scripted: no model runs.
LangGraph and Langfuse do not run on this page.
Still view. Fetch a dog, then use Next step to go through the run.
Your photo arrives here.
Choose a dog, then select Fetch a dog.
The record
Scripted teaching logEach stop leaves a card. Follow what was requested, returned and used.
- Fetch a dog to see the request and response here.
Ready when you are
- 01Ask
- 02Choose
- 03Call
- 04Inspect
- 05Explain
- 06Return
The public API did not respond. The lesson can still continue.
School network blocking the API? A sample illustrates the response without fetching a photo.
Photos fetched this visit
Just for this visit. Refreshing clears it.
02 / Look closer
What information came back from the API?
Getting a photo address is different from seeing the photo. Your browser can open the address and display it. A model given only that address has not seen the picture itself. A tool can return an image address as text. Your browser turns that address into a photo. A model receiving only the text has not seen the image pixels.
Run the lesson to inspect a response.
03 / Change the evidence
Take away the clue.
This written example contains a breed in its address. Hide the whole address, including the filename. What can you still reasonably conclude?
Illustrated tool response
{
"status": "success",
"message": "https://images.dog.ceo/breeds/beagle/example.jpg"
}The address contains “beagle”. That supports a statement about its label, not an image-recognition claim.
04 / Make it yours
Download and edit your own dog-photo page
This single HTML file connects a button, a photo and the JavaScript fetch call so you can save it, open it in a browser and edit it locally.
Download the working HTMLRead the code
The file opens locally. Fetching photos still needs internet access to Dog CEO.
- Save
pip-dog-fetch.htmlon your computer. - Open the saved file in your browser.
- Select Fetch a dog.
- Open the file in a text editor, change its heading, save and refresh.
Look closer: find fetch, where your page asks another service for data. Then find response.ok, which checks whether the HTTP request succeeded.
Just the request
An API is a way for one program to ask another for something. Each example below makes one request. JavaScript and Python print the photo address; curl prints the full JSON reply. An API lets programs ask other programs for data. These examples make one request. JavaScript and Python print the image address; curl prints the JSON response.
JavaScript Browser console or a modern Node.js .mjs file
const response = await fetch('https://dog.ceo/api/breeds/image/random');
if (!response.ok) throw new Error('Request failed');
const dog = await response.json();
if (dog.status !== 'success') throw new Error('No dog returned');
console.log(dog.message);Python Python 3, no extra packages
import json
from urllib.request import urlopen
url = 'https://dog.ceo/api/breeds/image/random'
with urlopen(url, timeout=8) as response:
dog = json.load(response)
if dog.get('status') != 'success':
raise RuntimeError('No dog returned.')
print(dog['message'])curl A terminal with curl; use curl.exe in Windows PowerShell
curl --fail --max-time 8 https://dog.ceo/api/breeds/image/random
Explore the Dog CEO API documentation. Make one request at a time; this lesson does not need a polling loop.
Connect the ideas
Tools for building and inspecting agent workflows
The browser lesson illustrates the pieces. In a real application, tools such as these can help you connect and inspect them.
- Dog CEO
- The public API that returns the image address. Meet the API
- LangChain
- Tools for building model-powered applications, including agents that use tools. Read the introduction
- LangGraph
- Infrastructure for orchestrating stateful agent workflows. The map illustrates a workflow; it is not running LangGraph. Explore graphs
- Langfuse
- Observability tools for recording and inspecting LLM application runs. The record here is a teaching illustration, not a Langfuse trace. Explore traces
Keep asking
What does the response actually prove?
Pip brought back an address. That shows where the photo came from, not that a model understood the picture.
Now imagine thousands of answers whose sources change over time. Tracking what still holds up is the question behind my Consumption Engine research. This lesson does not run it.
Browse the lessonsFoundational lesson 04 of 12: Fetch a dog photo
Take this with you. A request is an attempt. Read the response to find out what actually happened.
Can an AI assistant honestly say it found a photo if its tool returned an error?
No. It should explain the failure and offer a useful next step. A fluent answer cannot substitute for a missing result.
Source: MDN: Using the Fetch API
This optional check-in is saved only in your browser. It records a visit, not a test result. View the course map or clear check-ins.
Bring the whole class
Teach the request-and-response cycle
How can students check what an AI answer is based on? Take the discussion further with a suggested lesson plan, printable student worksheet and answer key.
What happens when you use this lesson?
Starting a live run contacts dog.ceo and loads a photo from images.dog.ceo. Those services receive ordinary connection information such as your IP address. The teaching sample makes no API call. This lesson uses no model service, account, microphone or camera. The page is hosted by Cloudflare. The collection stays in page memory and clears on refresh. Responses to the learning questions stay in your browser.
File details on this page
Format, size and a short SHA-256 identifier, measured when this page was built.
- pip-dog-fetch.txtText, 10.7 KB, sha256 de771d9fca33