Day 1 of 5
⏱ ~60 minutes
Build AI Apps with JavaScript — Day 1

Node.js + Claude API: Your First AI-Powered App

Build AI-powered JavaScript applications using Node.js and the Claude API. Day 1 covers project setup, your first API call, and the basics of message handling.

Why JavaScript for AI Apps

JavaScript is the language of the web. If you want to build AI-powered web apps — chatbots, writing tools, document processors — JavaScript is the fastest path from idea to deployed product. The Claude API has a first-class Node.js SDK, and the same code runs on the frontend and backend.

Setup

Terminal
mkdir ai-js-app && cd ai-js-app
npm init -y
npm install @anthropic-ai/sdk dotenv
touch .env index.js
.env
ANTHROPIC_API_KEY=sk-ant-your-key-here
index.js — First API Call
import Anthropic from '@anthropic-ai/sdk';
import * as dotenv from 'dotenv';
dotenv.config();

const client = new Anthropic();

async function chat(userMessage) {
  const message = await client.messages.create({
    model: 'claude-opus-4-5',
    max_tokens: 1024,
    messages: [
      { role: 'user', content: userMessage }
    ]
  });
  
  return message.content[0].text;
}

// Test it
const reply = await chat('Explain async/await in JavaScript in 3 sentences.');
console.log(reply);
package.json — Add type: module
{
  "type": "module",
  "scripts": {
    "start": "node index.js"
  }
}
Run It
node index.js

Understanding the Response Object

Inspect the Full Response
const message = await client.messages.create({ ... });

console.log(message.id);              // msg_xxx
console.log(message.model);           // claude-opus-4-5
console.log(message.stop_reason);     // end_turn
console.log(message.usage.input_tokens);   // tokens used
console.log(message.usage.output_tokens);  // tokens generated
console.log(message.content[0].type);      // 'text'
console.log(message.content[0].text);      // the actual reply
Day 1 Exercise
Build a Simple CLI Question Answerer
  1. Set up the project and create index.js with the code above.
  2. Run it and verify you get a response.
  3. Modify the question and run it 3 more times.
  4. Log the full message object and read through the structure.
  5. Build a simple loop that reads questions from command line args and prints responses.

Day 1 Summary

  • Anthropic SDK installed and configured with .env for the API key.
  • client.messages.create() is the core method — model, max_tokens, messages array.
  • Response contains content array, usage stats, and stop_reason.
  • Use ES modules (type: module in package.json) for clean import syntax.
Finished this lesson?