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.

Already curious about the code? Make your own page. / Teacher guide

No account or API key needed. Suitable for beginners.

What you will do

  1. Send a request
  2. Follow the tool call
  3. Keep a working webpage
Your own dog-photo button

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.

Agent machine
The Dog API run as a plan and a record Top: the plan. You, a model, a tool-call check, a tool that calls dog.ceo, and an answer. Bottom: the record, one trace with a row per step. The request is live; model steps are scripted. Request You dog.ceo public API GET /api/breeds/image/random the plan (how LangGraph draws an agent) Model scripted choice Tool call? pass 1: yes pass 2: no yes Tools fetch_dog_photo result goes back no: stop Answer Reply and photo {status, message} images.dog.ceo photo: second request Back to you the record (what Langfuse would keep) Trace: one run Model 1 scripted Tool: GET dog.ceo live Model 2 scripted

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 log

Each stop leaves a card. Follow what was requested, returned and used.

  1. Fetch a dog to see the request and response here.
  1. 01Ask
  2. 02Choose
  3. 03Call
  4. 04Inspect
  5. 05Explain
  6. 06Return

School network blocking the API? A sample illustrates the response without fetching a photo.

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.

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.

  1. Save pip-dog-fetch.html on your computer.
  2. Open the saved file in your browser.
  3. Select Fetch a dog.
  4. 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.

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 lessons

Foundational lesson 04 of 12: Fetch a dog photo

Two written replies, not live ones. The first has a photo address to show. The second has none, so the program should explain the problem and offer a retry, never show a broken image.

Photo found: HTTP 200
{
  "status": "success",
  "message": "https://images.dog.ceo/breeds/beagle/example.jpg"
}
Breed not found: HTTP 404
{
  "status": "error",
  "message": "Breed not found."
}

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.

Open the teacher guide
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.