Assignment 8: An API-Powered Game

Due: Tuesday, 12/1/26, before class.

Goal

Build a small game where your server talks to two things outside of it: a free public API that supplies the game (words, Pokémon, cards, or trivia questions), and a MongoDB Atlas database that saves everyone's results. A player uses buttons or a form on your page, your server does the work, and the page updates.

Every piece of this is something we've done in class: fetch to our own server, routes in Express, POST with a JSON body, calling an outside API from the server, secrets in .env, saving and reading with MongoDB, and hosting on Render. This assignment puts them together.

Useful examples: Calling the Gemini API from Our Server (server calls an outside API and sends the result to the page) and the MongoDB example from class on Friday 11/20.

Pick a Game

Pick one of these three, or make up your own game using any API you want. The three APIs below are free and need no API key. If the API you choose needs a key, that's fine: keep it secret the same way as your MongoDB connection string (see requirement 3).

1. Higher or Lower (Pokémon)

Two Pokémon appear, along with a random stat, like "Which one has more speed?" or "Which one has more HP?" Every right answer adds to your streak, and every round asks about a different random stat (if they tie, either answer counts). One wrong answer ends the game, and your streak goes on the leaderboard.

2. Card Shark

Higher or lower with a real shuffled deck. You see a card. Will the next one be higher or lower? Keep going until you're wrong.

3. Trivia

Answer multiple-choice questions. Your score goes on a leaderboard that everyone shares.

Want to Make Your Own? Some APIs to Spark Ideas

Here are a few more APIs to look at. Every one of these works as of 11/17/26.

API What it gives you
Free Dictionary Definitions of any English word, or a 404 if it isn't one
TheMealDB Recipes from around the world, with photos and the country they're from
Open Library Millions of books, with covers and publication years
JokeAPI Jokes, by category (use safe-mode)
Gemini (needs a free key, like class on 11/17) An AI that can write or judge almost anything

File and Folder Setup

Make a new 08-api-game folder in your assignments folder. Folder and file names must match exactly:

csc336-fall2026/
└── assignments/
    └── 08-api-game/
        ├── README.md
        ├── .gitignore
        ├── .env              (on your computer only, never pushed)
        ├── package.json
        ├── package-lock.json
        ├── server.js
        └── public/
            ├── index.html
            ├── style.css
            └── script.js

Requirements

  1. Set up the folder
    cd into 08-api-game and run:
    npm install express mongodb dotenv
    Add "type": "module" to package.json, like Assignment 7. Make a .gitignore with these two lines:
    node_modules
    .env
  2. Set up MongoDB Atlas
    Follow along with what we did in class on 11/20: make a free account, create a Free cluster, make a database user, and copy your connection string. Put it in .env:
    MONGODB_URI=mongodb+srv://USERNAME:PASSWORD@cluster0.xxxxx.mongodb.net/
    In Atlas, under Network Access, allow access from anywhere (0.0.0.0/0). Otherwise Render can't connect.
  3. Your server calls the outside API
    The browser never calls the game's API directly. Your script.js only ever fetches your own routes, and your server fetches the outside API and sends back what the page needs. If your API needs a key, put it in .env (for example API_KEY=...), read it with process.env.API_KEY, and add it to Render's Environment tab too. Never put a key in your code, and never in script.js.
  4. The front end updates the page
    Buttons and/or a form that fetch from your server and update the page with what comes back. No page reloads.
  5. Save to MongoDB
    At least one POST route that receives JSON (express.json() and req.body) and saves it with insertOne. For example: a name and a score, or a new word and who added it.
  6. Read from MongoDB, with a filter or a sort
    At least one GET route that uses find with a filter (for example { category: "History" }) or a sort (for example highest score first), and sends the results to the page. The page shows them (a leaderboard, best runs, everyone's captions).
  7. Test it on your computer first
    Run node server.js and play your game at http://localhost:3000. Check your data in Atlas under Browse Collections.
  8. Host it on Render
    Same as Assignment 7, with Root Directory assignments/08-api-game. Your .env isn't pushed, so Render doesn't have it: in your service's Environment tab, add MONGODB_URI with the same connection string. Use const port = process.env.PORT || 3000; like before.
  9. README.md with your link
    Make a file called README.md in 08-api-game with your Render link at the top, like:
    https://yourname-api-game.onrender.com
    GitHub shows this file when someone opens your folder, which is how I'll find your game. If you couldn't get it working (on Render, or at all), say so at the top instead, and write up what you did get working, what isn't working, and what you tried to fix it. An honest write-up of a game that's partly working is much better than nothing.
  10. Code comments
    Add your own HAND-WRITTEN comments to server.js and script.js explaining what each part does.

Things That Will Trip You Up

A Note on Generative AI

Do not use ChatGPT, Copilot, Claude, or any other generative AI tool to write any part of this assignment. Whatever you do, remember that your goal should be to understand 100% of the code that you turn in.

What to Submit

Nothing to submit directly. Push your 08-api-game folder (without .env or node_modules), make sure your Render link works, and make sure your README.md has the link (or your write-up of what works and what doesn't).