Your First AI App: The Lab Comes Alive · Lesson 2 ⏱ ~40 min

Build the brain in a box: your first server.

Before the NPC gets a face, it gets a brain — a tiny program on your own machine that holds the key, talks to Claude, and answers anyone who asks nicely on port 3000. This is the part of the course that's most secretly a professional skill.

Why the game can't hold the key

Obvious question first: why can't the game just call the API itself? Because your game is a webpage, and a webpage's code is visible to anyone who opens it. Right-click, View Source, there's everything. Put your API key in the game and you haven't hidden a secret — you've published a credit card number and taped it to a window. Course 02 taught you that webpages can't rummage through your computer, and you were glad about the wall. This is the same wall from the other side: the browser is a glass room, and secrets don't live in glass rooms.

Every AI product ever shipped solves this the same way: the key lives on a server, and the app talks to the server. The server is the only thing that ever talks to Anthropic. Today you build that — not a toy version of it, the actual shape.

your game a webpage (glass room) your server 🔑 server.js — holds the key, port 3000 Claude API the socket never — the key would be public
The professional shape. The game never touches Anthropic; the server is the only key-holder.
🌿What a server actually is. Strip the mystique: a server is just a program that sits running and answers requests — the way your game loop sits running and repaints frames. Course 02 taught you a loop that draws; this is a loop that answers. The "servers" running the internet are exactly this, on computers that never sleep. Yours runs on your laptop and sleeps when it does — which is fine, and also lesson 3's favorite failure mode.

Step 1 — the brief

In your pixel-lab folder, in Claude Code:

The prompt

I want to add a tiny local backend server to this project, as a single file called server.js, using only what's built into Node — no packages to install. First check whether I have a recent version of Node, and if I don't, install it — walk me through anything you need from me.

The server should listen on port 3000 and have one main endpoint: POST /chat. It receives JSON with a "messages" array (a conversation — entries with a role and content), sends that conversation to Anthropic's Messages API using the model claude-haiku-4-5, and responds with JSON shaped like {"reply": "..."} containing just the text of Claude's reply. No streaming — one request, one complete answer. If the API returns an error, respond with a clear error message instead of crashing. Also add a GET /health endpoint that just answers "ok" so I can check the server is up.

My API key must live in a file called .env in this folder, never in the code. Add .env to .gitignore before anything else so git can never see it, and tell me exactly what to paste into .env.

Watch what happens. It may need to install Node — a free tool that runs JavaScript outside the browser; it's the standard, say yes. It will ask permission before touching things; that's the permission system doing exactly its job, same as Course 02. And when it tells you what to paste into .env, go get your key from wherever you stashed it last lesson and paste it in yourself. That's the one piece of typing Claude Code shouldn't do for you, on principle: you handle the credit card.

Step 2 — first words through the wire

Start it: node server.js in a terminal. It prints that it's listening, then… sits there. That's not stuck. That's a server. It's a loop that answers, waiting for a question.

Now ask it one. You could learn curl — the terminal's way of poking a server — or you could do the Course-02 thing and make the tool do it:

The prompt — in a second terminal, or just ask Claude Code

The server is running. Send a test message to my /chat endpoint — ask it to say hello to the pixel lab — and show me the exact command you used plus the raw response.

Simulated run — your reply will differ. It's a language model; it has moods.

Sit with that JSON for a second. A program you own just had a conversation with a frontier AI model, through a socket you now have a key to. No chat window, no product, no middleman with a logo. This is the raw material every AI company is made of.

🧮The meter just ran, for real. That exchange was roughly 50 tokens in and 30 out. At the model's pricing — $1 per million in, $5 per million out — you just spent about a fiftieth of a cent. Later, when the NPC carries a whole conversation and a head full of your projects, a message will run about a fifth of a cent. You could chat all evening for a dime. Now you know, from your own meter, why every app suddenly has AI in it.

Step 3 — prove the key is invisible

Before anything gets committed, the professional move: verify git cannot see your key.

Checkpoint — the professional part
> git status

  modified:  .gitignore
  new file:  server.js

(no .env anywhere in this list — that's the win)

See .env in that list? → STOP before committing. Tell Claude Code: ".env is showing up in git — make sure it's ignored and has never been committed." If it ever was committed, ask it to help you scrub it, then revoke the key in the console and make a new one. Ten minutes, not a disaster — but do it now, not later.

What you just did — secret in .env, fence in .gitignore, verify with git status — is the universal secrets ritual. Every deployment platform, every tutorial, every company codebase you'll ever see expects secrets in exactly this shape. You now do it by reflex.

What you just built (it isn't a toy)

Look at the three boxes in the diagram again. A frontend, a backend guarding credentials and shaping requests, a model behind an API. That's ChatGPT's website. That's every "AI-powered" feature in every SaaS product. That's the customer-support bot that annoyed you last week. The industry buzzword for what you did tonight is "integrating AI" — and when a company says it, a developer is being paid a lot of money to build the thing you just built before dinner.

💾Save point: "Commit this — the brain: server.js plus a safely ignored .env." Double-check the commit message doesn't contain the key. It won't. Check anyway. That's the habit.

You can now…

  • Run a local server and explain what one actually is — a loop that answers
  • Call a real AI model from your own program through the Messages API
  • Keep a secret out of git with .env + .gitignore — and verify it
  • Estimate what an AI feature costs per use, from your own meter
Want the course by email? Lessons in your inbox, plus new ones as I write them.

Running a business and would rather this get built for you? That's literally my job — start with the free audit.