LessonsHow to show data from an external API on your website

Mini lesson 16 of 30 · Connect data and AI

Open the ready lookup

Who is this for?

People who want current information from another service on a website. The supplied page and a coding assistant handle the browser code; you can focus on the result.

Ask the service for a value instead of typing a fixed number into the page.

Purpose of this lesson

  • Look up a real public repository.
  • Show a useful extra result with a coding assistant.
  • Distinguish no matching repository from an unavailable service.
Explanation, worked example and practice

Before you start

The supplied workshop, internet access and a local HTTP preview your coding assistant can start. This example reads public GitHub repositories without a private token; service availability/rate limits still apply.

Check your starting skills

These are useful skills, not locked course requirements. If you can already do them, start this lesson; otherwise open the linked preparation. The supplied starter files mean you do not need an existing personal website.

  1. How to build your first personal website

    Create an introduction, project section and contact route from a supplied starter.

Estimated time: 14–26 min

Estimates for the supplied starter files with prerequisite accounts and tools ready. Setup, deployment queues and delivery/DNS waits take additional time. Unauthenticated GitHub requests share an IP-based quota; repeated tests can reach it.

Understand the idea

Have your assistant start the supplied public folder over HTTP. Use the starter’s octocat / Hello-World example and activate its lookup button.

Compare the returned repository identity and count with the service result at that time. Do not hardcode an old number as if it were current.

A concrete example

A saved star count in page text cannot change when the repository changes.

Start the supplied public repository lookup page over local HTTP. Keep the existing safe text rendering and do not add a private token to browser files. Help me test a real repository, a missing one and a failed service request. Then add the documented open-item count and help compare it with the API response. Handle the code changes for me.

Files for this step

Start hereWorkshop files

Open or download the files below. When saving a project, keep the folder paths shown in the filenames.

Try it yourself

Which repository did you look up, and what result did the running page actually display?

Show a worked solution

Illustrative lookup record with an invented count: “At my test time, I requested octocat / Hello-World. The page returned octocat/Hello-World and 12 stars; its visible identity and count matched the response I inspected.” The 12 is a fictional measurement for this example, not a current fact about that repository.

Star counts can change. Your record needs the number and test time you actually observed; if the service did not answer, report that state instead of substituting this example count.

What a sound answer includes

  • Names the public owner/repository actually requested and the returned identity.
  • Reports the observed count/time or precise service failure without hardcoding an example value.
How it works (optional)

Show data from another service

Load a public repository name, description and star count from GitHub.

An API is a defined way for software to request data or an action. Here your website asks GitHub for one repository and receives JSON instead of an HTML page.

We use a public read operation so you can learn request handling without starting with authentication. The result can change between requests; it is a current observation, not a number to hardcode.

Worked example

For owner octocat and repository Hello-World, the request path is /repos/octocat/Hello-World on api.github.com. The page displays the returned full_name and stargazers_count.

GitHub: get a repository

Extra implementation exercise

What is the difference between copying a star count into HTML and fetching it when the button is pressed?

Show a worked solution

Copied HTML keeps the old value until edited. A fetch requests the current response at that moment, but needs loading and failure handling because the service or network may not respond.

  • Explains when the value is obtained.
  • Recognizes the external request can fail.

Step 1 · How to show data from an external API on your website

Check one successful response

Use an observation you can compare with the source.

Explanation, worked example and practice

Understand the idea

Ask your assistant to show the matching public API response for your test repository and verify that the page displays its intended values. Keep the test public and non-sensitive.

A request that returns an error must not be treated as a normal result. You do not need to write the request/error code to judge the visible outcome.

A concrete example

Illustrative response values: full_name = practice/example and stargazers_count = 12 should become the visible repository name practice/example and 12 stars. These are fictional values for understanding the mapping, not the current count for the supplied GitHub example.

Files for this step

Start hereWorkshop files

Open or download the files below. When saving a project, keep the folder paths shown in the filenames.

Try it yourself

Did the page result match the repository response you inspected?

Show a worked solution

Illustrative mapping, using fictional response values: a response with full_name = practice/example and stargazers_count = 12 should display practice/example and 12 stars. It should not retain the previous repository’s name or turn a missing count into zero.

For your live check, use the actual response from the repository you queried at that time. A rate-limit response contains no normal star result to display.

What a sound answer includes

  • Matches the page’s identity and star count to the same public service response.
  • Does not present an error response or missing field as a normal zero-valued result.
How it works (optional)

Build and check one request

Encode user input and inspect the response before using it.

Read public/app.js and run the supplied page over HTTP. encodeURIComponent keeps each owner/repository value in its own path segment. The host is fixed; the visitor does not supply an arbitrary endpoint.

fetch resolves for HTTP errors such as 404, so awaiting it is not enough. Check response.ok and relevant statuses, parse JSON, then verify the fields the feature needs. textContent renders external strings as text.

The short snippet below illustrates the generic HTTP guard. The complete app.js first handles 404 and restricted/rate-limited responses specifically, then checks other unsuccessful statuses, parses JSON and validates fields. Keep that order when adding a new displayed field.

Worked example

A description containing angle brackets should remain visible text, not become page markup. A 404 should display a missing/public-access message rather than an empty success card.

if (!response.ok) throw Error('Request failed');
const data = await response.json();
result.textContent = data.full_name;

Extra implementation exercise

Why does try/catch alone not detect every unsuccessful HTTP response?

Show a worked solution

HTTP errors can still produce a normal Response object. Check status/ok and throw or handle the error before treating its body as the requested repository.

  • Knows fetch can resolve on 404/500.
  • Checks status before success rendering.

Step 2 · How to show data from an external API on your website

Try missing and unavailable states

Keep different failures understandable.

Explanation, worked example and practice

Understand the idea

Try a repository name that does not exist. With your assistant, also test a blocked/failed request in the local practice page. Keep the typed query available for correction or retry.

The page should distinguish a missing repository from a temporary service failure. Do not add a private browser token to bypass a rate limit.

A concrete example

“Repository not found” and “Service unavailable; retry” tell a visitor different next actions.

Files for this step

Start hereWorkshop files

Open or download the files below. When saving a project, keep the folder paths shown in the filenames.

Try it yourself

What did the two failure cases show, and could the visitor recover?

Show a worked solution

Illustrative failure record: “An invented repository name showed Repository not found or not publicly accessible. With the request deliberately blocked for a local failure test, the lookup showed an error; the owner/repository fields remained available and the button could be tried again.”

The blocked request is a simulation, not evidence of a real GitHub outage. A rate limit should lead to a later retry, not to putting a private account token in public page code.

What a sound answer includes

  • Records distinct results for a missing repository and an assisted failed-request test.
  • Checks that the query remains usable for correction or retry without adding a private browser token.
How it works (optional)

Make failure states distinguishable

No repository, no network and no remaining quota are different problems.

Test a real public repository, a clearly nonexistent name and browser offline mode. The UI clears the old result before a new request, disables duplicate submission and restores the button afterward.

GitHub can reject unauthenticated requests because of IP-based limits. Do not respond by embedding a private token in the browser. For sustained authenticated use, move credential use to a controlled server route and cache where appropriate.

Worked example

If a successful card stays visible below “Not found”, a visitor may mistake stale data for the new lookup. Clearing the old result makes the current request's state unambiguous.

GitHub: REST API rate limits

Extra implementation exercise

A lookup fails after many tests. Should the UI report that the repository has zero stars?

Show a worked solution

No. Zero is a real data value. Report unavailable/restricted data and inspect the status; the count is unknown until a successful response supplies it.

  • Does not turn unavailable data into zero.
  • Checks request status and quota evidence.

Step 3 · How to show data from an external API on your website

Add a useful result

Describe the new information and let the assistant connect it.

Explanation, worked example and practice

Understand the idea

Ask your assistant to add the response field open_issues_count beside the existing result and label it open issues and pull requests. This GitHub total includes both categories; it must not be relabeled only as bugs. Use the supplied reference after trying your version.

Compare the displayed value with the actual response, then repeat successful, missing and unavailable tests. You judge the meaning and observed states; the assistant handles the browser implementation.

A concrete example

A fictional open_issues_count of 5 could represent two open issues and three open pull requests. A label saying 5 bugs would claim something the field does not establish.

Files for this step

Start hereWorkshop files

Open or download the files below. When saving a project, keep the folder paths shown in the filenames.

Try it yourself

Add the open issues and pull requests total. How did its displayed value compare with the response, and what happened in the missing/unavailable tests?

What to produce: The updated lookup page and observed normal, missing and service-failure results.

Show a worked solution

Illustrative mapping: “For a fictional response with open_issues_count = 5, the page shows 5 open issues and pull requests. That total could include two issues and three pull requests; it is not a count of five bugs.”

Illustrative test record: “The normal lookup matched the response; the absent repository kept its not-found message; a blocked-request simulation kept a retryable error.” Your submission should include your live matched value and name any checks you did not run.

What a sound answer includes

  • Displays open_issues_count as open issues and pull requests, not only bugs or issues.
  • Compares the new value with a response and repeats normal, missing and unavailable states.
How it works (optional)

Your assignment: add one useful repository field

Add an issue count while preserving clear data and error boundaries.

Display open_issues_count after validating it as a finite number. Note that GitHub's repository count includes pull requests as documented; do not label it a precise count of only bug reports.

Record one successful lookup, a missing repository and an offline attempt. Include what the visitor sees during the request and after each outcome.

Worked example

A label such as “Open issues and pull requests” explains what the value measures more accurately than “Bugs”.

The complete reference walkthrough is available in reference/assignment-walkthrough.txt. Compare its steps with your own result; fill the test record from observations in your own project.

Extra implementation exercise

Extend the result with the documented open-item count and demonstrate all three states.

Show a worked solution

Add open_issues_count to the fields validated after successful status handling. Require a finite number, then label it 'Open issues and pull requests' because that GitHub repository field includes both. The full reference script preserves textContent rendering and the existing loading/error states.

Example fixture: full_name='sample/site', stargazers_count=3, open_issues_count=2 produces 'sample/site — 3 stars — 2 open issues and pull requests'. It does not claim that all two items are bugs.

Retest a successful public repository, a 404 and offline mode. Clear the previous card at the start of the next lookup; an error must not retain stale counts or replace unavailable data with zero.

  • Validates the added number and labels it accurately.
  • No stale success remains after a failed new lookup.

Was this lesson useful?

Leave a like, or share it with someone who could use it.